简体   繁体   English

iOS:setFrame不再使用viewDidLoad或viewWillAppear

[英]iOS: setFrame no longer working from viewDidLoad or viewWillAppear

I prefer to disable autoresizesSubviews and to use setFrame to place all of my subviews. 我更喜欢禁用autoresizesSubviews并使用setFrame来放置我的所有子视图。 As of iOS 6, things seem to have changed a lot. 从iOS 6开始,事情似乎发生了很大变化。 When I call setFrame on a view in viewDidLoad , there is no effect. 当我在viewDidLoad的视图上调用setFrame时,没有任何效果。 I tried it from viewWillAppear ; 我是从viewWillAppear尝试过的; same thing. 一样。

A setFrame call will work in willAnimateRotationToInterfaceOrientation , but that is not called initially like it was in iOS 5. setFrame调用将在willAnimateRotationToInterfaceOrientationwillAnimateRotationToInterfaceOrientation ,但最初不会像在iOS 5中那样调用。

Can someone clarify please where I am expected to layout my views from? 有人可以澄清我希望从哪里布置我的观点?

I guess you want to layout your views from UIViewController. 我想你想要从UIViewController布局你的视图。 Have you tried performing your layout tasks in viewDidLayoutSubviews : 您是否尝试在viewDidLayoutSubviews中执行布局任务:

- (void)viewDidLayoutSubviews

According to Apple's documentation: 根据Apple的文档:

Your view controller can override this method to make changes after the view lays out its subviews. 在视图布局其子视图后,视图控制器可以覆盖此方法以进行更改。

Since your view itself does not do layout its subviews (you do not use autosize, autolayout or layoutSubViews:) you can do the layout tasks in this method. 由于您的视图本身没有布局其子视图(您不使用自动调整大小,自动布局或layoutSubViews :),您可以在此方法中执行布局任务。

Nevertheless, the elegant way would be to use a custom parent UIView and perform all the layout there, overriding UIView's layoutSubViews: (unless you add/removes views dinamically). 然而,优雅的方法是使用自定义父UIView并在那里执行所有布局,覆盖UIView的layoutSubViews:除非你以dinamically方式添加/删除视图)。 Quote from Apple's documentation on "How View Controllers Participate in the View Layout Process" : 引用Apple关于“视图控制器如何参与视图布局过程”的文档:

Ideally, the views themselves perform all of the necessary work to reposition themselves, without requiring the view controller to participate in the process at all. 理想情况下,视图本身执行所有必要的工作来重新定位自己,而不需要视图控制器完全参与该过程。 However, if the view controller adds and removes views dynamically, a static layout in Interface Builder may not be possible. 但是,如果视图控制器动态添加和删除视图,则可能无法在Interface Builder中使用静态布局。 In this case, the view controller is a good place to control the process, because often the views themselves only have a limited picture of the other views in the scene. 在这种情况下,视图控制器是控制过程的好地方,因为视图本身通常仅具有场景中其他视图的有限图片。

You can use setFrame in viewDidLoad if you uncheck the Autolayout option in Interface Builder. 如果取消选中Interface Builder中的Autolayout选项,则可以在viewDidLoad使用setFrame If you need to use auto layout, You need to perform your layout tasks in viewDidLayoutSubviews: . 如果需要使用自动布局,则需要在viewDidLayoutSubviews:执行布局任务viewDidLayoutSubviews:

在Interface Builder中取消选中Autolayout

Regarding "Asking the views to lay themselves out doesn't make sense to me. Views don't have authority to say where they should go. That's the role of the controller, isn't it?". 关于“询问自己的意见对我来说没有意义。意见无权说出他们应该去哪里。那是控制者的角色,不是吗?”。 I think the answer is often negative. 我认为答案通常是否定的。 It's the parent view (not view controller) which usually plays the role of laying out it's own subviews. 这是父视图(不是视图控制器),它通常扮演着布置自己的子视图的角色。 It does so by utilizing autoresizingMask or autolayout (iOS 6 only). 它通过使用autoresizingMask或autolayout(仅限iOS 6)来实现。 Or You can make layout programmatically by overriding -layoutSubviews method of the parent view. 或者,您可以通过覆盖父视图的-layoutSubviews方法以编程方式进行布局。

Sure, like Imre mentioned, controller can participate in the layout process as well by overriding - (void)viewDidLayoutSubviews . 当然,就像Imre提到的那样,控制器也可以通过覆盖- (void)viewDidLayoutSubviews参与布局过程。 I often use this approach when I don't want to subclass the top level view of the view controller for keep things simple. 当我不想将视图控制器的顶级视图子类化以保持简单时,我经常使用这种方法。

Finally, I found this post super useful. 最后,我发现这篇文章超级有用。 Even with some minor errors, the post provides a big picture of view layout process. 即使有一些小错误,该帖子提供了视图布局过程的大图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM