xcode 当自动布局约束在视图控制器生命周期中设置帧时?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36125101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:38:40  来源:igfitidea点击:

When the autolayout constraints set frames during view controller life cycle?

iosobjective-cxcodeios-autolayout

提问by Hiren Prajapati

I have used autolayout constraints from storyboard. However in some cases, I want to calculate dynamic height of subview. I code this in viewDidAppear(), it works fine because this method is called after all view frames are set by layout constraints. The problem here is that I can see the frame set by constraints for half a second. And then the code reframes the view.

我使用了故事板中的自动布局约束。但是在某些情况下,我想计算子视图的动态高度。我在 viewDidAppear() 中对此进行了编码,它工作正常,因为在通过布局约束设置所有视图框架之后调用此方法。这里的问题是我可以看到约束设置的框架半秒。然后代码重新构建视图。

I came to know about viewDidLayout() which is called after constraints has set the frame so I can change. But it doesn't work. It is like this method is called before constraints are used.

我开始了解 viewDidLayout() ,它在约束设置框架后调用,以便我可以更改。但它不起作用。就像在使用约束之前调用此方法一样。

采纳答案by GoGreen

The viewDidAppearmethod is called at the end of the view life cycle. So if you change the constraints here, it will always be visible.

viewDidAppear方法在视图生命周期结束时调用。因此,如果您在此处更改约束,它将始终可见。

If the change you want to do is a one time event, then you may do so in the method viewWillAppear. But this won't always help because the view drawing may not always be finished by this time. So a better option is to place the code in viewWillLayoutSubviewsor viewDidLayoutSubviews.

如果您想要进行的更改是一次性事件,那么您可以在方法中进行viewWillAppear。但这并不总是有帮助,因为此时视图绘制可能并不总是完成。所以更好的选择是将代码放在viewWillLayoutSubviewsor 中viewDidLayoutSubviews

NOTE:viewWillLayoutSubviewsor viewDidLayoutSubviewswill be called multiple times in a view's life cycle, such as orientation change or moving in and out of another view or any frame change for that matter.
If the code change you want to put here is a one time event, then please make sure to use flags.

注意:viewWillLayoutSubviewsorviewDidLayoutSubviews将在视图的生命周期中多次调用,例如方向更改或移入和移出另一个视图或与此相关的任何框架更改。
如果您想在这里放置的代码更改是一次性事件,那么请确保使用标志。

eg:-

例如:-

- (void)viewWillLayoutSubviews {    
    if(firstTime) {
        // put your constraint change code here.
    }
}

Hope this helps! :)

希望这可以帮助!:)

回答by Anil Kukadeja

As name suggests, viewDidLayoutSubviews is called when the view of your viewController has just finished its laying out and you can assume that at that dynamic height your views are in their correct places/frames according to your autolayout constraints.

顾名思义, viewDidLayoutSubviews 在您的 viewController 的视图刚刚完成布局时被调用,您可以假设在该动态高度,您的视图根据您的自动布局约束位于正确的位置/框架中。

Swift :

    override func viewDidLayoutSubviews() {
       // Set your constraint here
    }

Objective C :

   -(void)viewDidLayoutSubviews {
   // Set your constraint here
   }

回答by Sauvik Dolui

I am not sure what you are actually trying to do in viewDidLayoutSubviews(). I always use to customise view by modifying layout constraint values overriding this method.

我不确定你实际上想要做什么 viewDidLayoutSubviews()。我总是通过修改覆盖此方法的布局约束值来自定义视图。

    // Called to notify the view controller that its view has just laid out its subviews
    override func viewDidLayoutSubviews() {
        //Write your code here, any constraint modification etc.
       super.viewDidLayoutSubviews()
    }