简体   繁体   English

如何使用自动布局根据其子视图调整父视图的高度

[英]How to adjust the parent view's height according to its subviews using auto layout

I have cutomized a view, whose subviews's height will dynamically change(I'm changing the height by changing the constraint). 我已经对一个视图进行了裁剪,其子视图的高度将动态更改(我通过更改约束来更改高度)。 My intention is to let the view's height adjust to its subviews' height, so I won't care for anything, and that's what autolayout is for. 我的意图是让视图的高度调整为其子视图的高度,因此我将不需要任何东西,这就是自动布局的目的。

In practise, there is no way to add any constraints to the root view in IB. 实际上,无法在IB的根视图中添加任何约束。 And I found I can still change the frame of the root view, which appears to be able to solve my current problem. 我发现我仍然可以更改根视图的框架,这似乎能够解决我当前的问题。 But I don't think it's recommended to compound auto layouts and frames. 但是我不建议您将自动布局和框架混合使用。

So, any other suggestions? 那么,还有其他建议吗?

It's perfectly fine to have some views using the old system of layout (ie, autoresizing and frame setting) while other views are using Auto Layout directly. 最好使用旧版式的系统(即自动调整大小和框架设置)来创建一些视图,而其他视图直接使用自动版式。 The important thing to remember is to set the translatesAutoresizingMaskIntoConstraints property to NO on views instantiated in code that are using Auto Layout. 要记住的重要事情是,在使用“自动布局”的代码中实例化的视图上,将translatesAutoresizingMaskIntoConstraints属性设置为“ NO ”。

However, you can't use Auto Layout on a view controller's root view. 但是,不能在视图控制器的根视图上使用“自动布局”。

Using Auto Layout, subviews can dictate the size of their superview–but this won't work if the superview is using autoresizing and frame setting. 使用“自动布局”,子视图可以决定其超级视图的大小,但是如果超级视图使用自动调整大小和框架设置,则此功能将无效。

I don't know if the root view you're working with is the root view of a scene's main view controller or the root view of a child view controller. 我不知道您使用的根视图是场景主视图控制器的根视图还是子视图控制器的根视图。 If the former, I don't recommend adjusting the root view's frame in iOS 7. Just let the root view fill the whole screen (which is its default behavior). 如果是前者,我不建议在iOS 7中调整根视图的框架。只需让根视图填充整个屏幕(这是其默认行为)。

Subviews have their size dictated by their superview, not the other way around. 子视图的大小取决于其父视图,而不是相反。 For your special case, I would make the subview delegate its height changes back to the superview. 对于您的特殊情况,我将使子视图委托其高度更改回到超级视图。 While you could then adjust the rect of the superview (and avoid changing the subview's height altogether), I would do this: 然后,您可以调整超级视图的矩形(并避免完全更改子视图的高度),我可以这样做:

  1. Create a subview of your view controller that behaves as your new resizing parent. 创建一个视图控制器的子视图,该子视图的行为与新的调整大小父级相同。 The viewController.view can then remain fullscreen. 然后,viewController.view可以保持全屏显示。 Your view hierarchy would then be viewController.view->Resizing Parent->Resizing Child. 您的视图层次结构将是viewController.view-> Resizing Parent-> Resizing Child。
  2. Create an IBOutlet to the constraint you want to modify. 为您要修改的约束创建一个IBOutlet。
  3. Create a protocol for the child view to send resize callbacks to its parent. 为子视图创建协议,以将调整大小的回调发送到其父视图。
  4. Update the parent's constraint constant when it receives the callback. 当父级收到回调时,更新其约束常量。

You shouldn't have to modify the height of the child view, because the constraints will do that for you. 您不必修改子视图的高度,因为约束将为您完成此操作。

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

相关问题 如何使用自动布局根据其子视图调整父视图的高度? - How to adjust the parent view's height according to its subviews using auto layout? 如何根据子视图的高度设置堆栈视图的高度? - How to set stack view height according to its subviews height? 如何使UICollectionViewCell适合其子视图(使用自动布局) - How to make UICollectionViewCell fit its subviews (using Auto Layout) 如何在不添加/删除约束的情况下使用AutoLayout根据父级高度调整子视图高度的大小 - How to resize subviews height according to the parent height using AutoLayout without adding/removing constrains 在iOS中使用自动布局,如何将标签放置在其包含视图的高度的1/3处? - Using auto layout in iOS, how can I position a label at 1/3 of the height of its containing view? 视图的自动布局可隐藏子视图 - Auto Layout of view hides subviews 删除一个视图的布局约束,但保留其子视图? - Remove one view's layout constraints but keep its subviews'? 使用自动布局调整UINavigationBar的高度 - Adjust UINavigationBar height with Auto Layout 在Appcelerator iOS中父视图布局为垂直时调整高度 - Adjust height when parent view layout is vertical in appcelerator ios 使用自动布局时,在init上的子视图中查看控制器动画? - View controller animating in subviews on init when using auto layout?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM