Objective c 在UITableviewCell自动布局中获取UIView高度

Objective c 在UITableviewCell自动布局中获取UIView高度,objective-c,uitableview,autolayout,Objective C,Uitableview,Autolayout,我想根据cellForRowAtIndexPath方法中的设备获取UIViewA的实际宽度,并且“UIViewA”位于自定义UITableViewCell中。我在UITableViewCell中使用自动布局 自定义UITableViewCell中的我的UIViewA,UITableViewCell宽度=384 我的UtablViewWidth=284。 所以我需要UIVIEW在CellForRowatineXpath的宽度是284 请指导我如何在cellforrowatinexpath获取UIV

我想根据
cellForRowAtIndexPath
方法中的设备获取
UIViewA
的实际宽度,并且“UIViewA”位于自定义
UITableViewCell
中。我在
UITableViewCell
中使用自动布局


自定义UITableViewCell中的我的UIViewA,UITableViewCell宽度=384 我的UtablViewWidth=284。 所以我需要UIVIEW在CellForRowatineXpath的宽度是284

请指导我如何在
cellforrowatinexpath
获取
UIView
的宽度,因为我想在
UIView
中添加动态内容

怎么样

cell.UIViewA.frame.size.width


将其添加到单元格中的视图创建中

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
将有正确的框架和边界。

不,我试过了,我得到了故事板的大小。我想知道设备的大小。意味着如果在IPhone 5中,与iPhone6相比,我的尺寸变小了。情节提要UIView width是384,与iPhone8相关。现在,我想了解iPhone5s的尺寸,并且在IPhone5中UIView width肯定小于384,以了解设备尺寸:let size=UIScreen.main.bounds let width=screenSize.width let height=screenSize.heights我不需要设备尺寸。我想根据device wise.cell.UIViewA.frame.size.width获取UIView大小这在IPhone5中为384,在iPhone8中为384,这取决于要获取高度的位置。我的UIViewA在自定义UITableViewCell中,UITableViewCell宽度=384,我的UITableVIEWWidth=284。因此,我需要UIViewA在单元格处的宽度为284,用于RowAtIndexPath
view.superview.LayoutifNeed()
,然后获取高度。谢谢@TheTiger,但我的UIViewA不在自定义UITableViewCell中工作,UITableViewCell width=384,我的UITableViewWidth=284。所以我需要UIVIEW在CellForRowatineXpath的宽度是284,在这之后您的自动布局还没有设置。
单元格的宽度将等于
表格的宽度。
所以
self.view.layoutIfNeed()
tbl.reloadData()
CellForRowatineXpath:
自定义单元格.layoutIfNeed()
然后在自定义UITableViewCell中获取height.my UIViewA,UITableViewCell Width=384和my UITableViewWidth=284。所以我需要UIViewA宽度是284在cellForRowAtIndexPath我使用你的代码,但不工作
[myView setNeedsLayout];
[myView layoutIfNeeded];