Laying out & sizing of subviews in a UIViewController

前端 未结 2 1191
醉酒成梦
醉酒成梦 2020-12-07 16:17

I have an app with with a UITabController and each tab is a UINavigationController. The root of one of my UINavigationControllers is a

相关标签:
2条回答
  • 2020-12-07 17:05

    You can do your layout logic inside the viewWillLayoutSubviews of the UIViewController.

    -(void)viewWillLayoutSubviews{
        [super viewWillLayoutSubviews];
        // Your layout logic here
    }
    

    DOC: Called just before the view controller's view's layoutSubviews method is invoked. Subclasses can implement as necessary. The default is a nop.

    0 讨论(0)
  • 2020-12-07 17:06

    autoresizesSubviews should be set on your parent view, while autoresizingMask should be set on the child views - this is the mistake I made so you could, too.

    In loadView you should size your subviews to fit whatever size of parent view is at the moment, and then later on when parent view is resized from 460 to 367 pixels your sub-views will be resized as well, according to your mask settings above.

    If that fails, there is nothing wrong in setting the view size within viewWillAppear - the performance impact of doing it every time is negligible.

    If nothing else works, there is always layoutSubviews: - there you could do manual layout if you have to, it's invoked when system believes layout may have to change. there is also setNeedsLayout: that I sometimes invoke from viewWillRotate:/viewDidRotate: etc. But really this shouldn't be needed and autoresize should be good enough.

    EDIT: Yes, to implement custom layout logic in layoutSubviews as I mention above one would need to subclass UIView.

    0 讨论(0)
提交回复
热议问题