首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自动布局问题

自动布局问题
EN

Stack Overflow用户
提问于 2013-10-16 14:19:45
回答 1查看 609关注 0票数 0

在视图控制器的根视图上有以下自动布局约束:

代码语言:javascript
复制
(lldb) po [superview constraints]
<__NSArrayM 0x9e65ba0>(
<NSLayoutConstraint:0x9e67d10 H:[HeaderView:0x9e6a1b0]-(0)-|   (Names: '|':UIView:0x9e69040 )>,
<NSLayoutConstraint:0x9e67ce0 H:|-(0)-[HeaderView:0x9e6a1b0]   (Names: '|':UIView:0x9e69040 )>,
<NSLayoutConstraint:0x9e67cb0 V:|-(0)-[HeaderView:0x9e6a1b0]   (Names: '|':UIView:0x9e69040 )>,
<NSLayoutConstraint:0x9e67c80 H:|-(0)-[RadialGradientView:0x9e685c0]   (Names: '|':UIView:0x9e69040 )>,
<NSLayoutConstraint:0x9e67c50 V:[RadialGradientView:0x9e685c0]-(0)-|   (Names: '|':UIView:0x9e69040 )>,
<NSLayoutConstraint:0x9e67c20 H:[RadialGradientView:0x9e685c0]-(0)-|   (Names: '|':UIView:0x9e69040 )>,
<NSLayoutConstraint:0x9e67bf0 V:[HeaderView:0x9e6a1b0]-(0)-[RadialGradientView:0x9e685c0]>,
<NSLayoutConstraint:0x10c89830 HeaderView:0x9e6a1b0.height == UIView:0x9e69040.height + 35>
)

当我添加高度约束(最后一个)时,我得到一个自动收费模糊布局错误:

代码语言:javascript
复制
2013-10-16 16:18:43.121 Application[26038:a0b] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0xb016da0 V:|-(0)-[HeaderView:0xb0192a0]   (Names: '|':UIView:0xb018130 )>",
    "<NSLayoutConstraint:0xb016d40 V:[RadialGradientView:0xb0176b0]-(0)-|   (Names: '|':UIView:0xb018130 )>",
    "<NSLayoutConstraint:0xb016ce0 V:[HeaderView:0xb0192a0]-(0)-[RadialGradientView:0xb0176b0]>",
    "<NSLayoutConstraint:0xb00c3f0 HeaderView:0xb0192a0.height == UIView:0xb018130.height + 35>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0xb016ce0 V:[HeaderView:0xb0192a0]-(0)-[RadialGradientView:0xb0176b0]>

Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

RadialGradientView是UIView的子类,它只覆盖drawRect:方法。HeaderView也是UIView的子类,具有以下代码:

代码语言:javascript
复制
@implementation HeaderView
{
    NSLayoutConstraint  * heightConstraint;
}

static UINib * headerViewNib = nil;

- (id)awakeAfterUsingCoder:(NSCoder *)aDecoder
{
    if (![[self subviews] count])
    {
        if (headerViewNib == nil)
            headerViewNib = [UINib frameworkNibWithNibName: @"HeaderView"];

        SMDFHeaderView * headerView = [[headerViewNib instantiateWithOwner: nil options: nil] lastObject];

        [headerView setTranslatesAutoresizingMaskIntoConstraints: NO];
        [self removeConstraints: [self constraints]];

        return headerView;
    }
    return self;
}

- (void)layoutSubviews
{
    if (!heightConstraint)
        [self setNeedsUpdateConstraints];

    [super layoutSubviews];
}

- (void)updateConstraints
{
    if (!heightConstraint)
    {
        UIView * superview = [self superview];
        heightConstraint = [NSLayoutConstraint constraintWithItem: self attribute: NSLayoutAttributeHeight relatedBy: NSLayoutRelationEqual toItem: superview attribute:NSLayoutAttributeHeight multiplier: 1.0 constant: 35.0];
        [superview addConstraint: heightConstraint];
    }

    [super updateConstraints];
}

/* Tried that, but it does not change anything.
- (CGSize)intrinsicContentSize
{
    return CGSizeMake([[super superview] frame].size.width, 35);
}*/

@end

PS :这个问题似乎与nib替换技巧无关,即使我评论了awakeAfterUsingCoder方法,它也会做同样的事情。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-16 22:12:36

您误解了这一行的工作方式:

代码语言:javascript
复制
heightConstraint = [NSLayoutConstraint constraintWithItem: self attribute: NSLayoutAttributeHeight relatedBy: NSLayoutRelationEqual toItem: superview attribute:NSLayoutAttributeHeight multiplier: 1.0 constant: 35.0];

这使得self等于superview的高度,乘以1,再加上35。这意味着您要求子视图比superview高35分,在其他地方也要求子视图与superview相匹配。那是行不通的。

要将视图约束到特定的高度,您需要使用如下内容:

代码语言:javascript
复制
heightConstraint = [NSLayoutConstraint constraintWithItem: self attribute: NSLayoutAttributeHeight relatedBy: NSLayoutRelationEqual toItem: nil attribute:NSLayoutAttributenNotAnAttribute multiplier: 1.0 constant: 35.0];

这只是将视图限制在特定的高度上。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19405915

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档