![](/img/trans.png)
[英]How to make a UIScrollView auto scroll when a UITextField becomes a first responder
[英]Why UIScrollView does not scroll up when UITextView becomes first responder?
当用户点击按钮在UIScrollView
上添加UITextView
时, UITextView
成为第一响应者。 实际上包含UITextView
或UITextField
UIView 添加在UIScrollView
。 当键盘出现时,scrollview 不会向上滚动,但是UITextField
使它在成为第一响应者时向上滚动。 我注册了UIKeyboardDidShowNotification
来向上滚动。 下面是我的代码
- (void)keyboardDidShow:(NSNotification *)notification {
CGRect keyboardEndFrame;
[(notification.userInfo)[UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame];
NSInteger height = CGRectGetHeight(keyboardEndFrame);
NSInteger lastObj = arrayTotalObjects.count - 1;
UIView *lastView = [arrayTotalObjects objectAtIndex:lastObj];
if(!(lastView.frame.origin.y > height))
return;
UIEdgeInsets inset = originalInset;
inset.bottom += height;
NSLog(@"inset: %f", inset.bottom);
[UIView animateWithDuration:0.3f animations:^{
scrlView.contentInset = inset;
}completion:^(BOOL finished) {
NSLog(@"scrlView inset bottom: %f", scrlView.contentInset.bottom);
}];
}
在这种情况下,lastView 将是包含 UITextView 的 UIView。
此代码适用于UITextField
但不适用于UITextView
。 这怎么可能? 是因为UITextView
是UIScrollView
子类吗?
如果有人看到这个问题,答案是 set UITextView scrolling enabled
false
在lawicko
的回答中,我得到了一个提示。 subclass of UIScrollView
。
它对我有用。
简而言之,您的问题的答案是肯定的,这种行为是由 UITextView 是 UIScrollView 的子类引起的。 您可以在此处找到更多详细信息以及如何禁用它的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.