cannot update view.layer.frame in viewDidLoad?

前端 未结 5 1920
予麋鹿
予麋鹿 2021-02-19 18:59

I am trying to execute following code in viewDidLoad method of my single view controller project:

self.view.layer.frame = CGRectInset(self.view.layer.frame, 20,          


        
5条回答
  •  一向
    一向 (楼主)
    2021-02-19 19:45

    The viewDidLoad method is too early to set your view's frame because something else is changing the frame later.

    For example, if this is your root view controller, then the UIWindow object will set the view's frame when it adds the view to itself as a subview. The viewDidLoad method runs as soon as loadView returns, before any outside object (like the UIWindow) can get its hands on the view.

    You can test this by using a custom view (if you're not already) and overriding setFrame: in the custom view class:

    - (void)setFrame:(CGRect)frame {
        [super setFrame:frame];
    }
    

    Put a breakpoint in that method and you'll see that the view's frame gets set some time after viewDidLoad returns.

提交回复
热议问题