前幾天寫了一個關(guān)于調(diào)試歧義LAYOUT的辦法瓜贾,最近又發(fā)現(xiàn)了一個可能更好一點的其他辦法,當確定在某個控制器(viewController)內(nèi)部的約束布局有歧義時萄传,可以用如下的方法調(diào)試望侈。
1.在控制器的@implementation 部分上面加上下面的代碼
@interface UIWindow (AutoLayoutDebug) + (UIWindow *)keyWindow;
- (NSString *)_autolayoutTrace;
@end
2.在 @implementation 部分內(nèi)部增加下面的方法
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSLog(@"%@", [[UIWindow keyWindow] _autolayoutTrace]); }
- (void)didRotateFromInterfaceOrientation: (UIInterfaceOrientation)fromInterfaceOrientation
{
[super didRotateFromInterfaceOrientation:
fromInterfaceOrientation];
NSLog(@"%@", [[UIWindow keyWindow] _autolayoutTrace]); }
程序運行時龄句,如果切換到當前頁面則會出現(xiàn)下面的打印
*<UIWindow:0x8a56ff0>
| *<UIView:0x7516d90>
| |
| |
| |
| |
*<UIRoundedRectButton:0x8a57e50> - AMBIGUOUS LAYOUT | <UIGroupTableViewCellBackground:0x8a58810>
| <UIImageView:0x7517070>
| <UIButtonLabel:0x7517e00>
其中<UIRoundedRectButton:0x8a57e50> - AMBIGUOUS LAYOUT就是存在約束歧義的控件
發(fā)生歧義約束時,就是系統(tǒng)的自動布局控制(當且這么叫??)不知道應(yīng)該按照哪種方式來對你的控件進行布局浩姥,下面的這個大招將告訴你出現(xiàn)歧義的情況
3.給出現(xiàn)問題的button添加如下方法(自己應(yīng)該能知道哪個控件的約束是有問題的吧)
[button1 addTarget:button1 action:@selector(exerciseAmbiguityInLayout)
forControlEvents:UIControlEventTouchUpInside];
當完成上面的步驟當你運行程序挑随,并且單擊按鈕時,按鈕會根據(jù)有歧義的約束進行循環(huán)布局及刻,很神奇吧镀裤。
這個方法對其他類型的控件的效力請自試。這個方法只能在調(diào)試的時候使用缴饭,如果發(fā)布版本暑劝,一定要去掉這些代碼。
希望我的文章對你有幫助颗搂,努力担猛,堅持,與君共勉丢氢。