在视觉格式语言 (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]];
或者,如果您打算@"V:[footerView(100)]-(-100)-|"
用调用替换 VFL constraintWithItem
,它将是:
[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]];
我不能说为什么你的 Masonary 实现不起作用,因为我不熟悉它。但我会尝试上述方法之一并确保。
坦率地说,你描述了设置 web view 的高度,我想知道你是设置 web view 的高度约束(好)还是只是调整了frame.size.height
值(坏)。但是你还没有分享你如何设置网络视图高度的细节,所以我不能进一步评论。
无论如何,如果您遇到基于约束的问题,有两种有用的诊断技术。如果它仍然不起作用,我会在模拟器上运行应用程序,暂停它,然后在(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]
您应该会看到如下内容:
(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>>
看看你的各种结果frame
值,看看发生了什么。这可以帮助您诊断解决方案。您没有为我们提供足够的信息来诊断问题(您告诉我们页脚不起作用,但您没有告诉我们您的约束最终将其放置在哪里)。如果您执行上述命令,您可以准确地确定约束最终放置页脚的位置,然后找出如何从那里解决问题。
请注意,您会看到 myheaderView
和footerView
在这两个列表中显示为HeaderView
和FooterView
类,而不是UIView
. 在诊断约束问题时,如果关键视图有自己的类,这很有用,所以我定义UIView
了没有实现的子类,但是如果我将这些唯一的子类用于我的页眉和页脚视图,则更容易解释上述结果(lldb)
命令。
@interface HeaderView : UIView
@end
@implementation HeaderView
@end
@interface FooterView : UIView
@end
@implementation FooterView
@end