简体   繁体   English

如何以编程方式自动使父视图适合其子视图?

[英]How to automatically fit the parent view to its child views programmatically?

I'm new to IOS development and I'm trying to make one of my parent view become automatically fit to its child views. 我是IOS开发的新手,我正在尝试使自己的父视图之一自动适应其子视图。

在此处输入图片说明

For example the picture above have a red circle which indicates a blank space I'm going to adjust the text upward so it fulfill the spaces and I also want that the container of all the text will automatically adjust its height to fit for its content. 例如,上面的图片有一个红色圆圈,表示一个空白,我将向上调整文本以使其充满空间,并且我还希望所有文本的容器都将自动调整其高度以适合其内容。

Thanks. 谢谢。

You need to override the willMoveToSuperView and didMoveToSuperView 您需要覆盖willMoveToSuperViewdidMoveToSuperView

Check the answer here iPhone/iOS: Will there be called any method if a UIView is added as a subview 在这里检查答案iPhone / iOS:如果将UIView添加为子视图,将调用任何方法吗?

You can then adjust the view according to its super view. 然后,您可以根据其超级视图调整视图。 However, using auto layouts is a much preferred way. 但是,使用自动布局是一种更可取的方法。

You Should do one thing. 你应该做一件事。
First alloc your parent view and give it a frame then, when your child view will alloc and it has a frame then set your parent view's frame one's again. 首先分配您的父视图并为其提供一个框架,然后,当您的子视图将分配并具有一个框架时,再重新设置您的父视图的框架。

So, when your child view will appear then your parent view's frame will automatically set. 因此,当您的子视图出现时,父视图的框架将自动设置。

Hope this helps... 希望这可以帮助...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM