gpt4 book ai didi

ios - 使用 AutoLayout addSubview 到 UITableView 后崩溃

转载 作者:行者123 更新时间:2023-12-01 19:01:41 25 4
gpt4 key购买 nike

当我使用 AutoLayout 技术将 subview 添加到 TableView 时,我的代码崩溃了,这是我的代码:

@interface GNViewController () {
UITableView *_tvResults;
UIView *_view;
}

@end

@implementation GNViewController



- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor blueColor];

_tvResults = [UITableView new];
_tvResults.backgroundColor = [UIColor greenColor];
[self.view addSubview:_tvResults];

_view = [UIView new];
_view.backgroundColor = [UIColor redColor];
[_tvResults addSubview:_view];

_tvResults.translatesAutoresizingMaskIntoConstraints = NO;
_view.translatesAutoresizingMaskIntoConstraints = NO;

NSArray *cs = [NSLayoutConstraint constraintsWithVisualFormat:@"|[_tvResults]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_tvResults)];
[self.view addConstraints:cs];

cs = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_tvResults]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_tvResults)];
[self.view addConstraints:cs];



cs = [NSLayoutConstraint constraintsWithVisualFormat:@"|[_view]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_view)];
[_tvResults addConstraints:cs];

cs = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_view]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_view)];
[_tvResults addConstraints:cs];
}

@end

崩溃消息显示:

2014-03-14 22:45:06.801 asdjasjdasdasd[3758:a0b] * -[UITableView layoutSublayersOfLayer:],/SourceCache/UIKit_Sim/UIKit-2903.2/UIView.m:8536 中的断言失败
2014-03-14 22:45:06.805 asdjasjdasdasd[3758:a0b] *
由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“执行 -layoutSubviews 后仍需要自动布局。 UITableView 的 -layoutSubviews 实现需要调用 super.'

任何人请帮助我,在此先感谢!

最佳答案

问题是您直接将 subview 添加到 UITableView 并试图将其包含在 TableView 的约束中。这些都是坏主意,尤其是第二个会导致您的崩溃。 TableView 不是为这类事情设置的。

如果您能解释您实际尝试做的事情,这将有所帮助。如果你想在表格 View 的顶部放置一些东西以便它覆盖它(尽管你为什么要这样做让我很生气),让它成为 self.view 的另一个 subview 并通过约束将其固定到 self.view .让 TableView 自生自灭。

编辑:从评论中,我了解到当您在没有自动布局的情况下这样做时,这很有效。然后在没有自动布局的情况下继续这样做!问题源于您尝试在表格 View 内部强加自动布局,该 View 不使用自动布局并且不喜欢那样。让表格 View 本身通过自动布局调整大小/定位但不在其 subview 上使用自动布局没有问题。只需回到您之前执行此操作的方式,它就会照常工作。

关于ios - 使用 AutoLayout addSubview 到 UITableView 后崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22408283/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com