我发现了很多关于这个的问题,但他们都使用了 initWithFrame。有没有办法严格使用自动布局来做到这一点?
我试过将附件 View 设置为按钮并向单元格添加约束,将按钮添加为附件 View 的 subview 并向附件 View 添加约束。这是我最有希望的尝试:
button.translatesAutoresizingMaskIntoConstraints = NO;
cell.accessoryView.translatesAutoresizingMaskIntoConstraints = NO;
[cell.accessoryView addSubview:button];
NSDictionary *varDict = NSDictionaryOfVariableBindings(button);
[cell.accessoryView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[button]|" options:0 metrics:nil views:varDict]];
[cell.accessoryView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[button]|" options:0 metrics:nil views:varDict]];
它一直告诉我它无法解析约束,因为相关 View 没有父 View 。我调用了 addSubview!怎么回事?
最佳答案
您可能不想为 cell.accessoryView
禁用 translatesAutoresizingMaskIntoConstraints
,因为您希望保留 accessoryView
的布局在 cell
中,对吗?如果是,你应该删除这一行:
cell.accessoryView.translatesAutoresizingMaskIntoConstraints = NO;
关于ios - 仅使用自动布局(无 initwithframe),为 UITableViewCell 配置 accessoryview 按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26812465/