【发布时间】:2014-11-12 22:53:40
【问题描述】:
我有一个简单的UIScrollView,其中包含 2 个UITextFields,还有一段对我来说没有任何意义的代码,但它正在工作。
这是屏幕(我使用的是自动布局):
代码如下:
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)keyboardWillShow:(NSNotification*) notif
{
NSDictionary *info = [notif userInfo];
CGRect keyboardRect = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
keyboardRect = [self.view convertRect:keyboardRect fromView:nil];
UIEdgeInsets contentInset = self.scrollView.contentInset;
contentInset.bottom = keyboardRect.size.height;
self.scrollView.contentInset = contentInset;
}
- (void) keyboardWillHide:(NSNotification*) notif
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
_scrollView.contentInset = contentInsets;
}
我希望将滚动视图向上移动 352 点(键盘的高度),因为这是我将 contentInset.bottom 设置为的值,并且选择哪个 UITextField 并不重要,因为高度键盘总是一样的。
但是结果是它向上滚动,所以作为第一响应者的UITextField 不会被键盘覆盖。因此,在底部UITextField 的情况下滚动更多,在顶部的情况下滚动更少。这看起来当然比我预期的结果要好得多,但我不知道为什么它会起作用。有人解释一下吗?
【问题讨论】:
标签: ios uiscrollview