由于 Xcode 8 和 iOS10,viewDidLayoutSubviews 上的视图大小不正确 [英] Since Xcode 8 and iOS10, views are not sized properly on viewDidLayoutSubviews

查看:25
本文介绍了由于 Xcode 8 和 iOS10,viewDidLayoutSubviews 上的视图大小不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎在 Xcode 8 上,在 viewDidLoad 上,所有 viewcontroller 子视图的大小都相同,为 1000x1000.奇怪的事情,但好吧,viewDidLoad 从来都不是正确调整视图大小的好地方.

It seems that with Xcode 8, on viewDidLoad, all viewcontroller subviews have the same size of 1000x1000. Strange thing, but okay, viewDidLoad has never been the better place to correctly size the views.

但是 viewDidLayoutSubviews 是!

在我当前的项目中,我尝试打印按钮的大小:

And on my current project, I try to print the size of a button:

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

    NSLog(@"%@", self.myButton);
}

日志显示 myButton 的大小为 (1000x1000)!然后,如果我登录按钮单击,例如,日志显示正常大小.

The log shows a size of (1000x1000) for myButton! Then if I log on a button click, for example, the log shows a normal size.

我正在使用自动布局.

这是一个错误吗?

推荐答案

现在,Interface Builder 允许用户动态更改故事板中每个视图控制器的大小,以模拟特定设备的大小.

Now, Interface Builder lets the user change dynamically the size of every view controllers in storyboard, to simulate the size of a certain device.

在此功能之前,用户应该手动设置每个视图控制器的大小.所以view controller保存了一定的大小,在initWithCoder中用来设置初始帧.

Before this functionality, the user should set manually each view controller size. So the view controller was saved with a certain size, which was used in initWithCoder to set the initial frame.

现在,似乎initWithCoder 没有使用storyboard 中定义的大小,而是为viewcontroller 视图定义了一个1000x1000 像素的大小.它的所有子视图.

Now, it seems that initWithCoder do not use the size defined in storyboard, and define a 1000x1000 px size for the viewcontroller view & all its subviews.

这不是问题,因为视图应该始终使用以下任一布局解决方案:

This is not a problem, because views should always use either of these layout solutions:

  • 自动布局,所有约束都会正确布局你的视图

  • autolayout, and all the constraints will layout correctly your views

autoresizingMask,它将布局每个没有附加任何约束的视图(注意自动布局和边距约束现在在同一视图中兼容 o/!)

autoresizingMask, which will layout each view which doesn't have any constraint attached to (note autolayout and margin constraints are now compatible in the same view o/ !)

但是对于与视图层相关的所有布局内容,例如 cornerRadius,这一个问题,因为 autolayoutautoresizing遮罩适用于图层属性.

But this is a problem for all layout stuff related to the view layer, like cornerRadius, since neither autolayout nor autoresizing mask applies to layer properties.

要回答这个问题,常用的方法是在控制器中使用viewDidLayoutSubviews,如果在视图中使用layoutSubview.此时(不要忘记调用它们的 super 相关方法),您很确定所有布局都已完成!

To answer this problem, the common way is to use viewDidLayoutSubviews if you are in the controller, or layoutSubview if you are in a view. At this point (don't forget to call their super relative methods), you are pretty sure that all layout stuff has been done!

很好确定吗?嗯……不完全是,我已经说过,这就是我问这个问题的原因,在某些情况下,视图在此方法上仍然具有 1000x1000 大小.我认为我自己的问题没有答案.提供有关它的最大信息:

Pretty sure? Hum... not totally, I've remarked, and that's why I asked this question, in some cases the view still has its 1000x1000 size on this method. I think there is no answer to my own question. To give the maximum information about it:

1- 仅在布局单元格时发生!在 UITableViewCell &UICollectionViewCell 子类,layoutSubview 不会在 子视图正确布局后被调用.

1- it happends only when laying out cells! In UITableViewCell & UICollectionViewCell subclasses, layoutSubview won't be called after subviews would be correctly layed out.

2- 正如@EugenDimboiu 所说(如果对您有用,请点赞他的答案),在未布局的子视图上调用 [myView layoutIfNeeded] 将及时正确地对其进行布局.

2- As @EugenDimboiu remarked (please upvote his answer if useful for you), calling [myView layoutIfNeeded] on the not-layed out subview will layout it correctly just in time.

- (void)layoutSubviews {
    [super layoutSubviews];
    NSLog (self.myLabel); // 1000x1000 size 
    [self.myLabel layoutIfNeeded];
    NSLog (self.myLabel); // normal size
}

3- 在我看来,这绝对是一个错误.我已将其提交给雷达(ID 28562874).

3- To my opinion, this is definitely a bug. I've submitted it to radar (id 28562874).

PS:我不是英语母语,所以如果我的语法需要更正,请随时编辑我的帖子;)

PS: I'm not english native, so feel free to edit my post if my grammar should be corrected ;)

PS2:如果您有更好的解决方案,请不要再写其他答案.我将移动已接受的答案.

PS2: If you have any better solution, feel free not write another answer. I'll move the accepted answer.

这篇关于由于 Xcode 8 和 iOS10,viewDidLayoutSubviews 上的视图大小不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆