相关疑难解决方法(0)

创建基于autolayout的指标视图

我有一个可重复使用的视图,我将使用UITableViewCell'sUICollectionViewCell,并需要获取其尺寸tableView:heightForRowAtIndexPath:.一些子视图里面有东西,layoutSubviews所以我不能打电话systemLayoutForContentSize:,而我的计划是:

  1. 实例化指标视图.
  2. 设置大小以包括所需的宽度.
  3. 用数据填充它.
  4. 更新约束/布局子视图.
  5. 抓取视图的高度或内部"大小调整"视图.

我遇到的问题是,我不能强制视图布局而不将其插入视图并等待runloop.

我提炼了一个相当无聊的例子.这是View.xib.子视图未对齐以突出显示视图永远不会布局到基线位置:

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)

iphone uikit ios autolayout

6
推荐指数
1
解决办法
1248
查看次数

标签 统计

autolayout ×1

ios ×1

iphone ×1

uikit ×1