gpt4 book ai didi

ios - 键盘上方的空白区域隐藏了文本字段

转载 作者:行者123 更新时间:2023-11-29 13:00:49 24 4
gpt4 key购买 nike

当出现键盘时尝试向上移动 ScrollView 时遇到问题。 ScrollView 在 ios7 中向上移动,但在 ios6 中它没有,并且键盘上方有额外的空白区域隐藏了屏幕上的控件

我的代码:

- (void)viewWillAppear:(BOOL)animated
{

[super viewDidAppear:animated];
keyboardIsShown = NO;



[super viewWillAppear:animated];
[[self view] endEditing:YES];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:self.view.window];
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:self.view.window];
}

- (void)keyboardWillShow:(NSNotification *)n
{
if (keyboardIsShown) {
return;
}

NSDictionary* userInfo = [n userInfo];

// get the size of the keyboard
CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

// resize the noteView
CGRect viewFrame = self.scrollView.frame;
viewFrame.size.height -= (keyboardSize.height - kTabBarHeight);

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
// The kKeyboardAnimationDuration I am using is 0.3
[UIView setAnimationDuration:kKeyboardAnimationDuration];
[self.scrollView setFrame:viewFrame];
[UIView commitAnimations];

scrollView.contentSize = formView.frame.size;


keyboardIsShown = YES;
}

这里有什么问题。请帮忙

最佳答案

问题是,您只更改了 ScrollView 的大小,但没有告诉 ScrollView 滚动到文本字段。

查看我的方法 scrollToEditingTextField 以便更好地理解。

- (void)keyboardWillShown:(NSNotification*)aNotification
{

NSDictionary* info = [aNotification userInfo];
CGSize beginSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

// keyboard will appear
if(!_keyboardShowed) {
_keyboardShowed = YES;
[UIView animateWithDuration:0.4
animations:^{
CGFloat scrollHeight =_scrollView.size.height - (IS_PORTRAIT_ORIENTATION ? beginSize.height : beginSize.width);

CGRect scrollFrame = _scrollView.frame;
scrollFrame.size.height = scrollHeight;
_scrollView.frame = scrollFrame;
}
completion:^(BOOL finished){ }];
}

[self scrollToEditingTextField];

}

- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{

NSDictionary* info = [aNotification userInfo];
CGSize endSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;

// keyboard will disappear
if(_keyboardShowed) {
CGFloat scrollHeight =_scrollView.size.height + (IS_PORTRAIT_ORIENTATION ? endSize.height : endSize.width);

CGRect scrollFrame = _scrollView.frame;
scrollFrame.size.height = scrollHeight;
_scrollView.frame = scrollFrame;
_keyboardShowed = NO;
}
}



-(void)scrollToEditingTextField {
// find which text field is currently editing
UITextField *editingTextFiled = [self editingTextField:self.thisView];

if(editingTextFiled == nil) return; // text field didnt found

CGPoint scrollPoint = [editingTextFiled convertPoint:CGPointMake(0, 0) toView:self.scrollView];
scrollPoint.y -= 70;
scrollPoint.x = 0;

[_scrollView setContentOffset:scrollPoint animated:YES];

}

-(UITextField*)editingTextField:(UIView*)view
{
if( ( [[view class] isSubclassOfClass:[UITextField class]] ||
[[view class] isSubclassOfClass:[UITextView class]] ) &&
[view isFirstResponder] ) {
return (UITextField*)view;
}
for(UIView *subview in view.subviews ) {
if([[subview class] isSubclassOfClass:[UISearchBar class]]) {
return nil;
}

if( ( [[subview class] isSubclassOfClass:[UITextField class]] ||
[[subview class] isSubclassOfClass:[UITextView class]] ) &&
[subview isFirstResponder] ) {
return (UITextField*)subview;
}
}

// recursion
for(UIView *subview in view.subviews ) {
UITextField *textField = [self editingTextField:subview];
if(textField) return textField;
}
return nil;
}

关于ios - 键盘上方的空白区域隐藏了文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19841444/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com