Since Xcode 8 and iOS10, views are not sized properly on viewDidLayoutSubviews

前端 未结 13 2219
北海茫月
北海茫月 2020-11-29 16:48

It seems that with Xcode 8, on viewDidLoad, all viewcontroller subviews have the same size of 1000x1000. Strange thing, but okay, viewDidLoad has n

13条回答
  •  鱼传尺愫
    2020-11-29 17:38

    If you need to do something based on your view's frame - override layoutSubviews and call layoutIfNeeded

        override func layoutSubviews() {
        super.layoutSubviews()
    
        yourView.layoutIfNeeded()
        setGradientForYourView()
    }
    

    I had the issue with viewDidLayoutSubviews returning the wrong frame for my view, for which I needed to add a gradient. And only layoutIfNeeded did the right thing :)

提交回复
热议问题