ios - 为什么关闭 translatesAutoResizingMasks 会导致自动布局崩溃并提示我需要调用 [super layoutSubviews]?

标签 ios objective-c uitableview

我有一个 UITableView,其 header 在 UIBuilder 中定义。我遇到了自动调整大小蒙版与我的约束冲突的错误,所以我开始四处寻找,直到找到原因。

不幸的是,修复似乎是导致崩溃的原因。当我以编程方式关闭 setTranslatesResizingMasks 时,不会发生布局冲突错误(要么是因为它已修复,要么是因为它从来没有机会发生)而是我遇到了崩溃:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after executing -layoutSubviews. UITableView's implementation of -layoutSubviews needs to call super.'

我尝试了一些其他线程(如 this one)关于使用方法调配来“修补”UITableViewCell 的建议(我继续并在我做的时候做了 UITableVIew),但它没有帮助。

编辑:

下面是一些可以实现的示例代码。因为我需要能够在运行时改变 View ,所以它必须通过代码创建,而不是 Storyboard。

请注意,如果我在自己的 View 中添加表头,一切正常;当我尝试将它嵌入到表格的标题 View 中时,事情就开始崩溃了。我要么关闭 tableHeader View 的 translateAutoresizingMaskIntoConstraints 以避免冲突,要么我在使用自动布局时遇到冲突(它打破的约束导致我的一些控件神秘地消失)。

self.palletTagField=[[UITextField alloc] init];
[self.palletTagField setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.palletTagField setDelegate:self];
[self.palletTagField setBorderStyle:UITextBorderStyleRoundedRect];

UIButton *addButton=[UIButton buttonWithType:UIButtonTypeContactAdd];
[addButton addTarget:self
              action:@selector(addPalletTagButtonPressed)
    forControlEvents:UIControlEventTouchUpInside];
[addButton setTranslatesAutoresizingMaskIntoConstraints:NO];

UIView *tableHeader=[[UIView alloc] init];
[tableHeader setTranslatesAutoresizingMaskIntoConstraints:NO];//Problem line
[tableHeader addSubview:self.palletTagField];
[tableHeader addSubview:addButton];
[tableHeader addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-[field]-[button]-|"
                                                                    options:NSLayoutFormatAlignAllCenterY
                                                                    metrics:nil
                                                                      views:@{@"field":self.palletTagField,
                                                                              @"button":addButton}]];
[tableHeader addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[field]-|"
                                                                    options:kNilOptions
                                                                    metrics:nil
                                                                      views:@{@"field": self.palletTagField}]];

UITableView *palletTable=[[UITableView alloc] init];
[palletTable registerClass:[UITableViewCell class] forCellReuseIdentifier:@"palletTagCell"];
[palletTable setEditing:YES];
self.palletTagTable=palletTable;
palletTable.tableHeaderView=tableHeader;
[palletTable setTranslatesAutoresizingMaskIntoConstraints:NO];
[palletTable setDataSource:self];
[palletTable setDelegate:self];

[self.contentView addSubview:palletTable];

[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[pallets]-|"
                                                                         options:kNilOptions
                                                                         metrics:nil
                                                                           views:@{@"pallets":palletTable}]];
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[last]-[pallets(>=400)]"
                                                                         options:kNilOptions
                                                                         metrics:nil
                                                                           views:@{@"last": lastObject,
                                                                                   @"pallets":palletTable}]];

调试器的示例输出:

[ANONYMIZED][31422:70b] 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:0x8f77810 H:|-(NSSpace(20))-[UITextField:0x8f6ad70]   (Names: '|':UIView:0x8f77650 )>",
    "<NSLayoutConstraint:0x8f77860 H:[UITextField:0x8f6ad70]-(NSSpace(8))-[UIButton:0x8f77500]>",
    "<NSLayoutConstraint:0x8f778d0 H:[UIButton:0x8f77500]-(NSSpace(20))-|   (Names: '|':UIView:0x8f77650 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x8fad160 h=--& v=--& H:[UIView:0x8f77650(0)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x8f77860 H:[UITextField:0x8f6ad70]-(NSSpace(8))-[UIButton:0x8f77500]>

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.
[ANONYMIZED][31422:70b] 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:0x8f77940 V:|-(NSSpace(20))-[UITextField:0x8f6ad70]   (Names: '|':UIView:0x8f77650 )>",
    "<NSLayoutConstraint:0x8f77980 V:[UITextField:0x8f6ad70]-(NSSpace(20))-|   (Names: '|':UIView:0x8f77650 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x8fad1c0 h=--& v=--& V:[UIView:0x8f77650(0)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x8f77980 V:[UITextField:0x8f6ad70]-(NSSpace(20))-|   (Names: '|':UIView:0x8f77650 )>

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.

最佳答案

此线程可能重复:Why am I getting a "Auto Layout still required after executing -layoutSubviews" error every time my app launches now?

将“translatesAutoresizingMaskIntoConstraints”设置为 YES 会影响将 View 的自动调整大小约束转换为布局引擎可以满足的布局约束。

这里的问题是您指定不应发生这种情况,而是创建了一堆约束来描述 View 的布局。由于 UITableView 不会创建这些约束,因此“仍然需要”自动布局。

发布破坏的初始约束以及一些详细说明布局约束设置的代码可能对您有用。

关于ios - 为什么关闭 translatesAutoResizingMasks 会导致自动布局崩溃并提示我需要调用 [super layoutSubviews]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19061697/

相关文章:

ios - 单元格中的标签文本不完整或延迟

iphone - 有没有一种聪明的方法来显示 iPhone OSStatus 错误值?

swift - UICollection View 与 TableView 错误索引超出范围为什么

swift - 动态 TableView 不显示来自 API 调用的带有 JSON 数据的文本

ios - Swift 3 iMessage 扩展不打开 URL

ios 11 UITableViewHeaderFooterView 无法在动画中正确滚动

ios - RestKit valueTransformer 未被调用

ios - viewDidLoad 上的 View 导出为零

swift - 快速手动调用 editActionsForRowAtIndexPath

javascript - Firebase 注销用户所有 session