【发布时间】:2017-10-13 01:06:59
【问题描述】:
我在 Objective-C 的 UIScrollview 中遇到了 iOS 的问题。
我拖动下面照片的 UI 自动布局。
当我点击文本字段时,键盘会显示,我需要滚动视图。
看起来没问题,但是当我点击完成按钮并隐藏键盘时,
我再次单击文本字段,键盘将显示,但我无法再次滚动视图。
其他问题就像上面的照片,
当我在 tableview 上滚动时,tableview 无法滚动,
它是滚动外部滚动视图,我如何在tableview上设置触摸,它是滚动tableview。
我的键盘事件代码如下,我已经在 GitHub 上上传了这个页面的完整代码和自动布局项目: this problem link
有人可以帮我吗?非常感谢。
- (void)viewDidLoad {
[super viewDidLoad];
[self registerForKeyboardNotifications];
self.tf.inputAccessoryView = self.keyboardToolbarVw;
}
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
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, 0.0);
self.sv.contentInset = contentInsets;
self.sv.scrollIndicatorInsets = contentInsets;
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, self.view.frame.origin) ) {
CGPoint scrollPoint = CGPointMake(0.0, self.view.frame.origin.y-kbSize.height);
[self.sv setContentOffset:scrollPoint animated:YES];
}
}
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
self.sv.contentInset = contentInsets;
self.sv.scrollIndicatorInsets = contentInsets;
}
- (IBAction)keyboardToolbarDoenBtnAction:(UIButton *)sender {
[self.tf resignFirstResponder];
}
【问题讨论】:
-
你有一个很棒的调试器。调试!在您的
keyboardWasShown和keyboardWillBeHidden上放置断点并查看它们何时被调用。是否仅在您期望它们会被调用时才调用它们?每次更改滚动视图时,还要查看分配给滚动视图contentInset的值。每次都有意义吗? -
我得到了 NSDictionary* info = [aNotification userInfo]; CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; kbSize.height 两次为零。为什么?@@
-
我将 UIKeyboardFrameBeginUserInfoKey 替换为 UIKeyboardFrameEndUserInfoKey。它可以两次获得价值。第一个问题解决了。但是第二个是怎么触摸tableview才能滚动呢?谢谢。
标签: ios objective-c uiscrollview