UIScrollView和Autolayout:ContentOffset正在改变,但内容视图不会移动

时间:2014-10-23 02:27:46

标签: uiscrollview autolayout nslayoutconstraint contentoffset

我一直在尝试在UIScrollView上实现自动布局,但一直在努力。以下是我的主视图的说明,从上到下:导航栏 - >滚动视图 - > UITextField,所有这些都在屏幕上水平伸展。以下是这三种观点的约束条件:

//Vertical constraints; this appears to be working fine
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-navHeight-[_inputScrollView][_inputField(inputFieldHeight)]|" options:0 metrics:metricsDict views:viewsDict]];


//Horizontal Constraints; these also appear to be working fine
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_inputScrollView]|" options:0 metrics:metricsDict views:viewsDict]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_inputField]|" options:0 metrics:metricsDict views:viewsDict]];

接下来,正如无数的UIScrollView / autolayout教程所示,我在UIScrollView中添加了一个子视图,作为许多其他子视图的内容视图,这些子视图将在运行时动态添加。因此,当应用程序启动时,内容视图中将不会有任何内容。以下是我的内容视图的约束:

//Left constraint, pinning the content view to the left edge of the screen
NSLayoutConstraint *leftConstraint = [NSLayoutConstraint constraintWithItem:_contentView
                                                                  attribute:NSLayoutAttributeLeading
                                                                  relatedBy:0
                                                                     toItem:self.view
                                                                  attribute:NSLayoutAttributeLeading
                                                                 multiplier:1.0
                                                                   constant:0];
[self.view addConstraint:leftConstraint];

//Right constraint, pinning the content view to the left edge of the screen
NSLayoutConstraint *rightConstraint = [NSLayoutConstraint constraintWithItem:_contentView
                                                                   attribute:NSLayoutAttributeTrailing
                                                                   relatedBy:0
                                                                      toItem:self.view
                                                                   attribute:NSLayoutAttributeTrailing
                                                                  multiplier:1.0
                                                                    constant:0];
[self.view addConstraint:rightConstraint];

//Top constraint, setting the top of the content view
//to be offset from the top of the main view
//by the height of the navigation bar
NSLayoutConstraint *topConstraint = [NSLayoutConstraint constraintWithItem:_contentView
                                                                   attribute:NSLayoutAttributeTop
                                                                   relatedBy:0
                                                                      toItem:self.view
                                                                   attribute:NSLayoutAttributeTop
                                                                  multiplier:1.0
                                                                    constant:navHeight.floatValue];
[self.view addConstraint:topConstraint];

//Bottom constraint, setting the bottom of the content view 
//to be offset from the bottom of the main view by the 
//height of the text field.
NSLayoutConstraint *bottomConstraint = [NSLayoutConstraint constraintWithItem:_contentView
                                                                 attribute:NSLayoutAttributeBottom
                                                                 relatedBy:0
                                                                    toItem:self.view
                                                                 attribute:NSLayoutAttributeBottom
                                                                multiplier:1.0
                                                                     constant:-inputFieldHeight.floatValue];
[self.view addConstraint:bottomConstraint];

布局这些视图后,我的内容视图的高度等于滚动视图的高度。我将alwaysBounceVertical属性设置为YES,所以我希望看到一些滚动。当我在屏幕上滚动时,滚动视图的内容偏移确实发生了变化!我实现了scrollViewDidScroll并将contentOffset记录到屏幕上。但是,内容视图根本不会移动。我将滚动视图的背景颜色设置为红色,将内容视图的背景颜色设置为黑色。当contentOffset发生变化时,您仍然看不到任何红色的scrollview。内容视图是滚动视图的子视图,那么为什么世界上它的框架不会改变?!任何帮助都会如此如此如此受到如此赞赏。谢谢!

1 个答案:

答案 0 :(得分:1)

我必须通过以下方式设置约束:

1)通过添加顶部,底部,左侧和右侧约束(例如,内容视图的顶部=滚动视图的顶部),将内容视图绑定到滚动视图。 2)为内容视图定义显式高度和宽度约束。在我的情况下,在大多数情况下,这些约束的常量应该基于内容视图的子视图的高度/宽度。

实施此设计后,滚动工作正常。