2014-03-13 45 views
1

我目前正在使用Masonry DSL設置我的iOS自動佈局的UITableViewCell。它的工作在iOS的7罰款,但在iOS 6中它的呼喊警告這樣的一堆碎約束:iOS自動佈局約束在iOS 6中損壞,但在iOS7中罰款

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. 
2014-03-13 14:41:07.422 clear[754:907] 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) 
(
    "<MASLayoutConstraint:0x1ed1e180 UILabel:0x1ed18ec0.centerY == UIView:0x1ed6b2b0.centerY>", 
    "<MASLayoutConstraint:0x1ed00f50 UILabel:0x1ed18ec0.centerY == UIView:0x1ed6b2b0.centerY>" 
) 

Will attempt to recover by breaking constraint 
<MASLayoutConstraint:0x1ed1e180 UILabel:0x1ed18ec0.centerY == UIView:0x1ed6b2b0.centerY> 

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. 
2014-03-13 14:41:07.451 clear[754:907] 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) 
(
    "<MASLayoutConstraint:0x1edf0010 UIView:0x1edef170.width == UIView:0x1edef170.height * 0.75>", 
    "<MASLayoutConstraint:0x1ed309f0 UIView:0x1edef170.width == UIView:0x1edef170.height * 0.75>" 
) 

Will attempt to recover by breaking constraint 
<MASLayoutConstraint:0x1edf0010 UIView:0x1edef170.width == UIView:0x1edef170.height * 0.75> 

一件事,我注意到這個現象的原因的一些限制(如上圖所示)是重複的,所以iOS版不得不放棄其中一個。下面的代碼片段。任何人都可以幫助我找出問題所在,並擺脫此警告信息?

的UITableViewCell updateConstraints

- (void)updateConstraints 
{ 
    [super updateConstraints]; 

    [self.coverImage mas_makeConstraints:^(MASConstraintMaker *make) { 
     make.left.equalTo(self.contentView).with.offset(20); 
     make.top.equalTo(self.contentView).with.offset(5); 
     make.bottom.equalTo(self.contentView).with.offset(-5); 

     make.width.equalTo(self.coverImage.mas_height).multipliedBy(0.75); 
    }]; 

    [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) { 
     make.left.equalTo(self.coverImage.mas_right).with.offset(10); 
     make.centerY.equalTo(self.contentView).priorityLow(); 
    }]; 

    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { 
     make.left.equalTo(self.containerView); 
     make.top.equalTo(self.containerView); 
    }]; 

    [self.avatarImage mas_makeConstraints:^(MASConstraintMaker *make) { 
     make.top.equalTo(self.titleLabel.mas_bottom).with.offset(5); 
     make.left.equalTo(self.containerView); 
     make.bottom.equalTo(self.containerView); 

     make.width.equalTo(@20); 
     make.height.equalTo(@20); 
    }]; 

    [self.authorLabel mas_makeConstraints:^(MASConstraintMaker *make) { 
     make.left.equalTo(self.avatarImage.mas_right).with.offset(5); 
     make.centerY.equalTo(self.avatarImage); 
    }]; 

    [self.dateLabel mas_makeConstraints:^(MASConstraintMaker *make) { 
     make.right.equalTo(self.contentView).with.offset(-10); 
     make.top.equalTo(self.contentView).with.offset(5); 
    }]; 
} 

回答

1

我不使用此lib中,但快速查找的文檔和源代碼,使其乾淨。您的UIView上的updateConstraints可以多次呼叫,通常會多次呼叫。當你看你的代碼時,你正在使用mas_makeConstraints。換句話說,當調用updateConstraints時,您再次添加這些約束。這就是這些約束條件重複的原因。您有兩種選擇...

mas_makeConstraints替換爲mas_updateConstraints。從文檔閱讀:

添加 - (NSArray的*)mas_updateConstraints:(無效(^)(MASConstraintMaker *))塊,將更新,如果有可能存在的制約因素,否則會加重他們。這使得在UIView - (void)updateConstraints方法中更容易使用Masonry,該方法是Apple推薦添加/更新約束的地方。

或者,你可以在你阻止你的MASConstraintMaker對象上設置updateExistingYES

應該解決你的問題。

+0

工程就像一個魔術!嗯,不完全像魔術,因爲你的解釋對我來說很清楚。非常感謝 ;) – Andree