移动到另一个文本字段时,为啥 UIScrollView 会重新调整大小 - iOS iPhone

Posted

技术标签:

【中文标题】移动到另一个文本字段时,为啥 UIScrollView 会重新调整大小 - iOS iPhone【英文标题】:Why does UIScrollView resize back up when moving to another textfield - iOS iPhone移动到另一个文本字段时,为什么 UIScrollView 会重新调整大小 - iOS iPhone 【发布时间】:2013-05-03 08:01:51 【问题描述】:

这可能不是一个容易回答的问题......

我在屏幕上有 8 个文本字段,它们位于滚动视图中。当我选择第一个字段时,键盘出现并且滚动视图缩小。然后我可以上下滚动。

当我选择另一个文本字段时,我不能再滚动了。

我已经使用textFieldDidBeginEditing 检查了滚动视图的高度,它肯定会重新长出来。

我还注册了所有 4 个键盘通知,但在移动到新文本字段期间没有一个被触发。

任何指针将不胜感激:)

- (void)viewDidLoad

[super viewDidLoad];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillShow:)
                                             name:UIKeyboardWillShowNotification
                                           object:self.view.window];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillHide:)
                                             name:UIKeyboardWillHideNotification
                                           object:self.view.window];


keyboardIsShown = NO;

//Additional Code    



- (void)viewDidUnload 

[[NSNotificationCenter defaultCenter] removeObserver:self
                                                name:UIKeyboardWillShowNotification
                                              object:nil];

[[NSNotificationCenter defaultCenter] removeObserver:self
                                                name:UIKeyboardWillHideNotification
                                              object:nil];




- (void)keyboardWillHide:(NSNotification *)n


NSDictionary* userInfo = [n userInfo];

CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

CGRect viewFrame = self.scrollView.frame;

viewFrame.size.height += (keyboardSize.height - self.toolBar.frame.size.height);

[self.scrollView setFrame:viewFrame];

keyboardIsShown = NO;


- (void)keyboardWillShow:(NSNotification *)n

if (keyboardIsShown) 
    return;


NSDictionary* userInfo = [n userInfo];

CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

CGRect viewFrame = self.scrollView.frame;

viewFrame.size.height -= (keyboardSize.height - self.toolBar.frame.size.height);

scrollViewHeight = viewFrame.size.height;

[self.scrollView setFrame:viewFrame];

keyboardIsShown = YES;

用于在文本字段之间移动:

-(void)textFieldDidBeginEditing:(UITextField *)textField

[[self saveButton] setEnabled:YES];



-(BOOL)textFieldShouldReturn:(UITextField *)textField

if (textField == self.label1) 
    [self.label2 becomeFirstResponder];
 else if (textField == self.label2) 
    [self.label3 becomeFirstResponder];
 else if (textField == self.label3) 
    [self.label4 becomeFirstResponder];
 else if (textField == self.label4) 
    [self.label5 becomeFirstResponder];
 else if (textField == self.label5) 
    [self.label6 becomeFirstResponder];
 else if (textField == self.label6) 
    [self.label7 becomeFirstResponder];
 else if (textField == self.label7) 
    [self.label8 becomeFirstResponder];
 else if (textField == self.label8) 
    [textField resignFirstResponder];

return YES;

【问题讨论】:

将您的文本字段中的所有委托方法编码为通知方法 对不起,Sunny,我不明白你的意思? 【参考方案1】:

经过大量挖掘,我找到了以下代码,我现在已经对其进行了调整和测试:

- (void)viewDidLoad

[super viewDidLoad];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWasShown:)
                                             name:UIKeyboardDidShowNotification
                                           object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillBeHidden:)
                                             name:UIKeyboardWillHideNotification
                                           object:nil];


keyboardIsShown = NO;


//Additional code


- (void)viewDidUnload 

[[NSNotificationCenter defaultCenter] removeObserver:self
                                                name:UIKeyboardDidShowNotification
                                              object:nil];

[[NSNotificationCenter defaultCenter] removeObserver:self
                                                name:UIKeyboardWillHideNotification
                                              object:nil];


- (void)keyboardWasShown:(NSNotification*)aNotification

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

UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, (kbSize.height -    self.toolBar.frame.size.height), 0.0);
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;

// If active text field is hidden by keyboard, scroll it so it's visible
// Your application might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= (kbSize.height + (self.toolBar.frame.size.height*2));
if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) 
    CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y-kbSize.height);
    [self.scrollView setContentOffset:scrollPoint animated:YES];



// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification

UIEdgeInsets contentInsets = UIEdgeInsetsZero;
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;


-(void)textFieldDidBeginEditing:(UITextField *)textField

activeField = textField;


- (void)textFieldDidEndEditing:(UITextField *)textField

activeField = nil;

【讨论】:

【参考方案2】:

在这类任务中我使用的是BSKeyboardControls

有很好的示例项目和Usage description

【讨论】:

针对不相关问题的公然插件

以上是关于移动到另一个文本字段时,为啥 UIScrollView 会重新调整大小 - iOS iPhone的主要内容,如果未能解决你的问题,请参考以下文章

当用户以编程方式单击swift ios中的删除按钮时,如何将光标从一个文本字段自动移动到另一个文本字段?

如何在swift ios中自动将光标从一个文本字段移动到另一个文本字段?

如何在swift3 ios中使用键盘返回键将一个文本字段移动到另一个文本字段

将文本输入到另一个输入字段时清除输入字段

将焦点从一个组件转移到另一个组件

当键盘从自己的文本字段切换到另一个文本字段时,搜索栏右键“取消”被禁用