超级视图完成加载时调整子视图的大小 [英] Resize subview when superview finishes loading

查看:92
本文介绍了超级视图完成加载时调整子视图的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有无数类似的问题,要么全部导致使用灵活的高度/宽度,要么将translatesAutoresizingMaskIntoConstraints设置为false.

I know theres countless similar questions on this that either all result in using flexible height/width or setting translatesAutoresizingMaskIntoConstraints to false.

我使用创建的扩展名添加了一个视图:

I have add a view using an extension I created:

extension UIView {
    func addView(storyboard: String, viewIdentier: String) {
        let story = UIStoryboard(name: storyboard, bundle: nil)
        let subview = story.instantiateViewController(withIdentifier: viewIdentifier)
        subview.view.frame = self.bounds
        self.addSubview(subview.view)
    }
}

如果我使用它来初始化视图,则在ViewDidAppear中一切正常.但是,如果视图中的约束确实已加载,则约束到处都是,因为包含该视图的contrainView具有尚未加载的自己的约束.

If I use this to initialise a view, in the ViewDidAppear everything works fine. But if its in the view did load then the constraints are all over the place because the contrainView that contains the view has its own constraints that are not yet loaded.

我目前这样使用:

@IBOutlet weak var container: UIView!

override func viewDidLoad() {
    container.addView(storyboard: "Modules", viewIdentifier: "subview")
}

由于某些原因,我不想在ViewDidAppear中初始化视图.有什么办法可以解决这个问题,使其按预期工作吗?还是在加载超级视图后重新加载子视图约束?

I don't want to initialise the view in the ViewDidAppear because of reasons. Is there any way of fixing this so it works as expected? Or reload the subview constraints after the superview has loaded?

我也真的尝试过使用其他解决方案.我无法完成这项工作,因此非常感谢您的帮助.

Also I've really tried using other solutions. I can't make this work so would really appreciate some help.

推荐答案

没有办法以您的代码建议的硬编码方式进行操作.您正在基于 self.bounds 设置子视图的框架.但是在 viewDidLoad 中,我们尚不知道 self.bounds 的最终值.还为时过早.

There is no way to do things in the hard-coded manner your code proposes. You are setting the frame of the subview based on self.bounds. But in viewDidLoad, we do not yet know the final value for self.bounds. It is too soon.

在进行布局后,即在 viewDidLayoutSubviews 中,适当的操作取决于已知的布局值.请注意,在应用程序的整个生命周期中,可以多次调用此方法.您不想每次都添加子视图,因此请使用Bool标志来确保仅添加一次子视图.您可能需要在随后调整超级视图的大小时,在 viewDidLayoutSubviews 中执行其他操作,以便对子视图进行适当的调整.

The appropriate place to do something that depends on knowing layout values is after layout has taken place, namely in viewDidLayoutSubviews. Be aware that this can be called many times in the course of the app's lifetime; you don't want to add the subview again every time that happens, so use a Bool flag to make sure that you add the subview only once. You might, on subsequent resizes of the superview, need to do something else in viewDidLayoutSubviews in order to make appropriate adjustments to the subview.

但是做这种事情的正确方法是添加子视图(可能在 viewDidLoad 中)并赋予它约束,以便此后,相对于其父视图 ,无论何时以及如何调整父视图的大小,其框架都将保持正确.在您的问题中,您似乎似乎拒绝这种方法,但这仍然是正确的做法,您应该放弃对此的抵制.

But the correct way to do this kind of thing is to add the subview (possibly in viewDidLoad) and give it constraints so that its frame will henceforth remain correct relative to its superview regardless of when and how the superview is resized. You seem, in your question, to reject that kind of approach out of hand, but it is, nevertheless, the right thing to do and you should drop your resistance to it.

这篇关于超级视图完成加载时调整子视图的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆