Ios scrollview有问题

Ios scrollview有问题,ios,objective-c,uiscrollview,viewcontroller,Ios,Objective C,Uiscrollview,Viewcontroller,我对滚动视图有一个奇怪的问题。我目前在一个ViewController上有4个文本字段和4个标签,当键盘弹出时,它会阻止查看4对文本字段和标签中的3对。我添加了一个ScrollView,但当我添加时,我突然无法单击文本字段并弹出键盘。有没有解决视图障碍的想法或替代方法?听起来你只是在所有标签和文本字段的顶部添加了一个滚动视图。您需要添加所有文本字段和标签作为scrollview的子视图,它们应该被拖动到scrollview中。您必须设置适当的约束,以使scrollview根据其内容的大小具有正确

我对滚动视图有一个奇怪的问题。我目前在一个ViewController上有4个文本字段和4个标签,当键盘弹出时,它会阻止查看4对文本字段和标签中的3对。我添加了一个ScrollView,但当我添加时,我突然无法单击文本字段并弹出键盘。有没有解决视图障碍的想法或替代方法?

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

您的新视图可能位于文本字段顶部,因此当您单击时,实际上是在视图中而不是在文本字段中单击。这在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];
    }
}
有很多关于它的帖子,一个很好的例子是当键盘存在时如何使UITextField向上移动


我希望您能提供帮助。

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

[self.scrollView addSubView:textField1];
[self.scrollView addSubView:label1];
[self.scrollView addSubView:textField2];
[self.scrollView addSubView:label1];
// ...
然后,不要忘记通常在initWithFrame中设置scrollView框架,或者更好地在LayoutSubview中设置scrollView框架。您还需要设置scrollView的contentSizeproperty。表示滚动视图中的内容有多大

self.scrollView.frame = self.view.bounds;
self.scrollView.contentSize = self.view.bounds.size;
在显示的视图中,注册键盘通知:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShowNotification:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHideNotification:) name:UIKeyboardWillHideNotification object:nil];
- (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;
}
在视图中注销这些通知将消失:

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

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShowNotification:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHideNotification:) name:UIKeyboardWillHideNotification object:nil];
- (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滚动显示所有内容。

谢谢您的帮助!我添加了所有合适的对象作为子视图并添加了约束,但它仍然不会滚动。你还有其他建议吗?谢谢!我查阅了你提供的链接,学到了很多,但我仍然有一些问题。我想我以后可以解决这个问题,因为使用新的XCode,在scrollview中添加所有内容和使用入站属性来移动scrollview会更容易,您甚至不需要预兆动画。非常感谢!你帮了我很大的忙