我有一个可重复使用的视图,我将使用UITableViewCell's和UICollectionViewCell,并需要获取其尺寸tableView:heightForRowAtIndexPath:.一些子视图里面有东西,layoutSubviews所以我不能打电话systemLayoutForContentSize:,而我的计划是:
我遇到的问题是,我不能强制视图布局而不将其插入视图并等待runloop.
我提炼了一个相当无聊的例子.这是View.xib.子视图未对齐以突出显示视图永远不会布局到基线位置:

在主线程上我打电话:
UIView *view = [[UINib nibWithNibName:@"View" bundle:nil] instantiateWithOwner:nil options:nil][0];
NSLog(@"Subviews: %@", view.subviews);
view.frame = CGRectMake(0, 0, 200, 200);
[view updateConstraints];
[view layoutSubviews];
NSLog(@"Subviews: %@", view.subviews);
[self.view addSubview:view];
[view updateConstraints];
[view layoutSubviews];
NSLog(@"Subviews: %@", view.subviews);
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"Subviews: %@", view.subviews);
});
Run Code Online (Sandbox Code Playgroud)
我得到以下视图信息:
1) "<UIView: 0x8bad9e0; frame = (50 50; 220 468); autoresize = W+H; layer = <CALayer: 0x8be0070>>"
2) "<UIView: 0x8bad9e0; frame = …Run Code Online (Sandbox Code Playgroud)