我想根据设备cellForRowAtIndexPath
方法获取UIViewA
的实际宽度,并且"UIViewA"在自定义UITableViewCell
中。我在UITableViewCell
中使用自动布局。
my UIViewA in in Custom UITableViewCell, UITableviewCell Width = 384和我的 UITablVIewWidth = 284。所以我需要 UIViewA 宽度是 284 在单元格为行在索引路径
请指导我如何在cellForRowAtIndexPath
获得UIView
宽度,因为我想在UIView
中添加动态内容。
?
cell.UIViewA.frame.size.width
或
cell.UIViewA.bounds
将其添加到单元格中的视图创建中
UIView *backgroundView = [UIView new];
[backgroundView setTranslatesAutoresizingMaskIntoConstraints:NO];
[backgroundView setBackgroundColor:[UIColor grayColor]];
backgroundView.layer.cornerRadius = 5;
[self addSubview:backgroundView];
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(backgroundView);
[NSLayoutConstraint activateConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[backgroundView]|"
options:0
metrics:nil
views:viewsDictionary]];
[NSLayoutConstraint activateConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[backgroundView]|"
options:0
metrics:nil
views:viewsDictionary]];
现在,您可以打印视图框并获取宽度。
通常,当您需要查阅视图的框架或边界并使用自动布局定位视图时,请首先执行以下操作:
[myView setNeedsLayout];
[myView layoutIfNeeded];
现在myView
将有其正确的框架和边界。