Iphone 自动布局混乱:带有WebView和页眉/页脚视图的ScrollView的约束
为了说明我的问题,我创建了以下图形: 红场周围的黑色边框应该是iPhone屏幕。在内部,我们有一个红色的Iphone 自动布局混乱:带有WebView和页眉/页脚视图的ScrollView的约束,iphone,ios,objective-c,nslayoutconstraint,Iphone,Ios,Objective C,Nslayoutconstraint,为了说明我的问题,我创建了以下图形: 红场周围的黑色边框应该是iPhone屏幕。在内部,我们有一个红色的UIScrollView,它比屏幕高。事实上,它与上面显示为黄色的UIWebView一样高。UIWebView是UIScrollView的子视图,滚动被禁用。此外,我们有两个蓝色视图,我喜欢称之为页眉和页脚视图。它们也是UIScrollView的子视图,但不在可滚动范围内 我现在的问题是:如何为此配置自动布局约束 我已经使用检索了UIWebView高度,然后相应地更新了UIScrollVie
UIScrollView
,它比屏幕高。事实上,它与上面显示为黄色的UIWebView
一样高。UIWebView
是UIScrollView
的子视图,滚动被禁用。此外,我们有两个蓝色视图,我喜欢称之为页眉和页脚视图。它们也是UIScrollView
的子视图,但不在可滚动范围内
我现在的问题是:如何为此配置自动布局约束
我已经使用检索了UIWebView
高度,然后相应地更新了UIScrollView
的contentSize
。但是,这并不是一个好的解决方案,因为UIWebView
的宽度不会对UIScrollView
的宽度变化做出反应
我还成功地设置了标题视图的布局约束,如下所示(使用):
但是,这样设置页脚视图的布局不起作用:
[changeView makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(scrollView.bottom)
make.left.equalTo(@0);
make.width.equalTo(scrollView.width);
make.height.equalTo(@100);
}];
我觉得这不会太难,如果有人对自动布局更熟悉一些,我会非常感激。谢谢 在视觉格式语言(VFL)中,约束条件是:
[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(-100)-[headerView(100)]" options:0 metrics:nil views:views]];
[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[footerView(100)]-(-100)-|" options:0 metrics:nil views:views]];
[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[headerView]|" options:0 metrics:nil views:views]];
[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[footerView]|" options:0 metrics:nil views:views]];
[footerView addConstraint:[NSLayoutConstraint constraintWithItem:footerView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:100.0]];
[scrollView addConstraint:[NSLayoutConstraint constraintWithItem:footerView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:scrollView
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:100.0]];
或者,如果要将@“V:[footerView(100)]-(-100)-“
VFL替换为constraintWithItem
调用,则它将是:
[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(-100)-[headerView(100)]" options:0 metrics:nil views:views]];
[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[footerView(100)]-(-100)-|" options:0 metrics:nil views:views]];
[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[headerView]|" options:0 metrics:nil views:views]];
[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[footerView]|" options:0 metrics:nil views:views]];
[footerView addConstraint:[NSLayoutConstraint constraintWithItem:footerView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:100.0]];
[scrollView addConstraint:[NSLayoutConstraint constraintWithItem:footerView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:scrollView
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:100.0]];
我不能说你的Masorial实现为什么不起作用,因为我不熟悉它。但我会试试上面的一个,并确保
坦率地说,您描述了如何设置web视图的高度,我想知道您是否设置了web视图的高度约束(好),还是只是调整了frame.size.height
值(坏)。但是您还没有分享如何设置web视图高度的详细信息,因此我无法进一步发表评论
无论如何,如果您有基于约束的问题,有两种有用的诊断技术。如果它仍然不工作,我会在模拟器上运行应用程序,暂停它,然后在(lldb)
提示符下输入:
po [[UIWindow keyWindow] _autolayoutTrace]
您应该看到如下内容:
(lldb) po [[UIWindow keyWindow] _autolayoutTrace]
$0 = 0x0715d6e0
*<UIWindow:0x758aae0>
| *<UIView:0x71630f0>
| | *<UIScrollView:0x716a370>
| | | *<HeaderView:0x716b860>
| | | *<FooterView:0x716bbb0>
| | | *<UIWebView:0x716c2b0>
| | | | <_UIWebViewScrollView:0x7176a30>
| | | | | ...
| | | | | <UIWebBrowserView:0x797fe00>
| | | <UIImageView:0x75e1360>
| | | <UIImageView:0x75e2a60>
(lldb) po [[UIWindow keyWindow] recursiveDescription]
$1 = 0x071c0ea0 <UIWindow: 0x758aae0; frame = (0 0; 320 568); autoresize = W+H; layer = <UIWindowLayer: 0x758b770>>
| <UIView: 0x71630f0; frame = (0 20; 320 548); autoresize = RM+BM; layer = <CALayer: 0x7163180>>
| | <UIScrollView: 0x716a370; frame = (0 0; 320 548); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x716b1d0>; layer = <CALayer: 0x716ad20>; contentOffset: {0, 16}>
| | | <HeaderView: 0x716b860; frame = (0 -100; 320 100); layer = <CALayer: 0x716b910>>
| | | <FooterView: 0x716bbb0; frame = (0 564; 320 100); layer = <CALayer: 0x716bee0>>
| | | <UIWebView: 0x716c2b0; frame = (0 8; 320 548); layer = <CALayer: 0x716c360>>
| | | | <_UIWebViewScrollView: 0x7176a30; frame = (0 0; 320 548); clipsToBounds = YES; autoresize = H; gestureRecognizers = <NSArray: 0x7177000>; layer = <CALayer: 0x7176c80>; contentOffset: {0, 0}>
| | | | | ...
| | | | | <UIWebBrowserView: 0x797fe00; frame = (0 0; 1024 1754); gestureRecognizers = <NSArray: 0x7173260>; layer = <UIWebLayer: 0x716d7f0>>
| | | | | | ...
| | | <UIImageView: 0x75e1360; frame = (1 540; 318 7); alpha = 0; opaque = NO; autoresize = TM; userInteractionEnabled = NO; layer = <CALayer: 0x75e2980>>
| | | <UIImageView: 0x75e2a60; frame = (312 32.5; 7 530.5); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x75e2b00>>
您应该看到如下内容:
(lldb) po [[UIWindow keyWindow] _autolayoutTrace]
$0 = 0x0715d6e0
*<UIWindow:0x758aae0>
| *<UIView:0x71630f0>
| | *<UIScrollView:0x716a370>
| | | *<HeaderView:0x716b860>
| | | *<FooterView:0x716bbb0>
| | | *<UIWebView:0x716c2b0>
| | | | <_UIWebViewScrollView:0x7176a30>
| | | | | ...
| | | | | <UIWebBrowserView:0x797fe00>
| | | <UIImageView:0x75e1360>
| | | <UIImageView:0x75e2a60>
(lldb) po [[UIWindow keyWindow] recursiveDescription]
$1 = 0x071c0ea0 <UIWindow: 0x758aae0; frame = (0 0; 320 568); autoresize = W+H; layer = <UIWindowLayer: 0x758b770>>
| <UIView: 0x71630f0; frame = (0 20; 320 548); autoresize = RM+BM; layer = <CALayer: 0x7163180>>
| | <UIScrollView: 0x716a370; frame = (0 0; 320 548); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x716b1d0>; layer = <CALayer: 0x716ad20>; contentOffset: {0, 16}>
| | | <HeaderView: 0x716b860; frame = (0 -100; 320 100); layer = <CALayer: 0x716b910>>
| | | <FooterView: 0x716bbb0; frame = (0 564; 320 100); layer = <CALayer: 0x716bee0>>
| | | <UIWebView: 0x716c2b0; frame = (0 8; 320 548); layer = <CALayer: 0x716c360>>
| | | | <_UIWebViewScrollView: 0x7176a30; frame = (0 0; 320 548); clipsToBounds = YES; autoresize = H; gestureRecognizers = <NSArray: 0x7177000>; layer = <CALayer: 0x7176c80>; contentOffset: {0, 0}>
| | | | | ...
| | | | | <UIWebBrowserView: 0x797fe00; frame = (0 0; 1024 1754); gestureRecognizers = <NSArray: 0x7173260>; layer = <UIWebLayer: 0x716d7f0>>
| | | | | | ...
| | | <UIImageView: 0x75e1360; frame = (1 540; 318 7); alpha = 0; opaque = NO; autoresize = TM; userInteractionEnabled = NO; layer = <CALayer: 0x75e2980>>
| | | <UIImageView: 0x75e2a60; frame = (312 32.5; 7 530.5); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x75e2b00>>
谢谢你的回答,我现在已经设置好了。非常感谢调试命令,它们非常有用。