2013-10-16 107 views
0

我有一個視圖控制器的根視圖以下汽車佈局限制:自動佈局問題

(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> 
) 

當我添加了高度約束(最後一個),我得到一個自動版式曖昧佈局錯誤:

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的與下面的代碼的子類:

@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:這個問題似乎並不涉及到筆尖更換伎倆,即使我評論的awakeAfterUsingCoder方法,做同樣的事情。

+0

如果你不添加高度約束?你是否有任何代碼約束(提供你的問題),還是你在Interface Builder中設置它們? – Vinzzz

+0

我更新了問題,添加了高度約束的代碼。所有其他約束條件都在自動佈局中配置(高度之一也在自動佈局中配置,新的xcode 5佔位符選項爲「在構建時刪除」) – LiohAu

+1

查看您現有的約束條件,您已經有效地定義了「@」V:| [HeaderView] [RadialGradientView] |「'。但是你正在試圖添加另一個表示'HeaderView:0x9e6a1b0.height == UIView:0x9e69040.height + 35'顯然不能滿足。我們可以退後一步嗎?你能描述一下你想要實現的UI嗎? – Rob

回答

2

你誤解了這條線是如何工作的:

heightConstraint = [NSLayoutConstraint constraintWithItem: self attribute: NSLayoutAttributeHeight relatedBy: NSLayoutRelationEqual toItem: superview attribute:NSLayoutAttributeHeight multiplier: 1.0 constant: 35.0]; 

這是什麼東西做的是使self等於superview高度,乘以1,加35。這意味着你問子視圖比超級觀點高35分,並且在其他地方,要求子視圖適合超級觀點。這是行不通的。

要限制是爲了一個特定的高度,你需要使用這樣的事情:

heightConstraint = [NSLayoutConstraint constraintWithItem: self attribute: NSLayoutAttributeHeight relatedBy: NSLayoutRelationEqual toItem: nil attribute:NSLayoutAttributenNotAnAttribute multiplier: 1.0 constant: 35.0]; 

這只是限制着眼於一個特定的高度。

+0

感謝您的解釋,我在另一篇文章中發現了這行代碼,我認爲我複製了一個錯誤,我認爲這是適當的語法來定義視圖的高度:(非常感謝。 – LiohAu