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

查看:806
本文介绍了从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.

在此功能之前,用户应手动设置每个视图控制器的大小。所以视图控制器以一定的大小保存,用于 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 不使用故事板中定义的大小,并为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,它将布局每个视图t附加任何约束(注意自动布局和边距约束现在在同一视图中兼容\ 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 ,因为两者都不是em> autolayout 和 autoresizing mask 适用于图层属性。

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天全站免登陆