【发布时间】:2017-08-09 00:03:15
【问题描述】:
我有一个UIView,它作为子视图添加到UIScrollView,UIView 包含UITextField 和UITextView,以避免键盘隐藏我为keyboardWasShown 和@987654328 注册的字段@通知,我写了这段代码
- (void)keyboardWasShown:(NSNotification *)notification
{
isKeyboardUp = YES;
// Step 1: Get the size of the keyboard.
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
// Step 2: Adjust the bottom content inset of your scroll view by the keyboard height.
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0);
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;
CGRect TextFieldFrame;
if(activeTextField)
{
TextFieldFrame = [activeTextField.superview convertRect:activeTextField.frame toView:self.view];
}
else
{
TextFieldFrame = [activeTextView.superview convertRect:activeTextView.frame toView:self.view];
}
[self.scrollView scrollRectToVisible:TextFieldFrame animated:YES];
}
- (void) keyboardWillHide:(NSNotification *)notification
{
isKeyboardUp = NO;
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;
}
这里的activeTextField 和activeTextView 是当前使用的文本字段,这在大多数情况下都很好用,但在少数情况下会失败,如果实施有什么问题,有人可以提出建议。
【问题讨论】:
-
我不打算使用任何第三方框架。
-
您是否将委托设置为所有 UITextFields。
-
是的,我已经很小心了
标签: ios objective-c uiscrollview keyboard uitextfield