滚动视图的问题

Trouble with scrollview

ScrollView 有一个奇怪的问题。我目前在一个 ViewController 上有 4 个 TextFields 和 4 个标签,当键盘弹出时,它挡住了 4 对 TextFields 和标签中的 3 个的视图。我添加了一个 ScrollView,但是当我添加时,我突然无法单击 TextFields 并弹出键盘。有什么解决视线障碍的想法或替代方法吗?

听起来您只是在所有标签和文本字段之上放置了一个滚动视图。您需要将所有文本字段和标签添加为滚动视图的子视图,它们应该被拖到其中。您必须设置适当的约束,并使滚动视图根据其内容具有正确的内容大小。然后,您需要更新在键盘出现或消失时影响滚动视图内容大小的约束,它不只是自动工作。

您的新视图可能位于文本字段的顶部,因此当您单击时,您实际上是在视图中单击,而不是在文本字段中单击。这在 XCode 的调试视图中很容易看出。 关于键盘覆盖测试字段的问题,要解决此问题,您必须分配给这两个通知

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

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

并实施代码,以便在键盘弹出时将您的字段移开,并在键盘消失时返回到正常位置。类似于:

-(void)keyboardWillShow {
    // Animate the current view out of the way
    if (self.view.frame.origin.y >= 0)
    {
        [self setViewMovedUp:YES];
    }
    else if (self.view.frame.origin.y < 0)
    {
        [self setViewMovedUp:NO];
    }
}

-(void)keyboardWillHide {
    if (self.view.frame.origin.y >= 0)
    {
        [self setViewMovedUp:YES];
    }
    else if (self.view.frame.origin.y < 0)
    {
        [self setViewMovedUp:NO];
    }
}

关于它的帖子很多,一个很好的例子是 "How to make a UITextField move up when keyboard is present" 吃了 How to make a UITextField move up when keyboard is present?

希望对你有所帮助。

首先,您必须添加所有 UITextFields 和 UILabels UIScrollView 中:

[self.scrollView addSubView:textField1];
[self.scrollView addSubView:label1];
[self.scrollView addSubView:textField2];
[self.scrollView addSubView:label1];
// ...

然后,不要忘记设置您的 scrollView frame(通常在 initWithFramelayoutSubviews 中更好)。您还需要设置滚动视图的 contentSize属性。那表示那个scrollView里面的内容有多大。

self.scrollView.frame = self.view.bounds;
self.scrollView.contentSize = self.view.bounds.size;

在您的 viewDidAppear 中注册键盘通知:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShowNotification:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHideNotification:) name:UIKeyboardWillHideNotification object:nil];

取消注册 viewWillDisappear 中的那些通知:

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

最后,实施您在注册通知时作为选择器提供的 keyboardWillShowNotification:keyboardWillHideNotification:

- (void)keyboardWillShowNotification:(NSNotification *)notification {
    CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    UIEdgeInsets insets = self.scrollView.contentInset;
    insets.bottom = keyboardFrame.size.height;
    self.scrollView.contentInset = insets;
}

- (void)keyboardWillHideNotification:(NSNotification *)notification {
    self.scrollView.contentInset = UIEdgeInsetsZero;
}

这样,当显示键盘时,您将能够使 UIScrollView 滚动显示所有内容。