本文介绍了iOS版:自动布局VS autoresizingMask - 调整整个视图来填补?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这是使用自动布局建立了一个故事板。在这一故事板,我在几个地点嵌入UIViewController子类(ButtonGridViewController),其中每一个的大小不同。 ButtonGridViewController的观点是在厦门国际银行定义的。

I've got a storyboard which is built using Auto Layout. Within that storyboard, I'm embedding a UIViewController subclass ("ButtonGridViewController") in several locations, each of which is a different size. ButtonGridViewController's view is defined in a xib.

我需要的是对ButtonGridViewController的看法的全部简单地扩展到填补我将它嵌入在观与旧的支柱 - 和弹簧的方法,这是微不足道的 - 只设置了所有子视图在两个方向来调整,瞧,这块蛋糕。

What I need is for the entirety of the ButtonGridViewController's view to simply scale-to-fill the view I'm embedding it in. With the old struts-and-springs method, this was trivial -- just set all the subviews to resize in both directions, and voila, piece of cake.

我如何做到使用约束是一回事吗?对于它的价值,在厦门国际银行只包含一个主视图,这是长方形的,并且有4子视图 - 每一个按钮 - 排列在一个2x2格。我想要的一切,包括按键和间距,缩放和/或拉伸,以填补它会到视图。

How do I accomplish the same thing using constraints? For what it's worth, the xib just contains a main view, which is rectangular, and has 4 subviews - each a button - arranged in a 2x2 grid. I want everything, including the buttons AND spacing, to scale and/or stretch to fill the view it's going into.

谢谢!

推荐答案

要使用约束完成同样的事情,你需要设置前置,顶部和底部的空间给上海华0见下图:

To accomplish the same thing using constraints you need to set the leading, trailing, top and bottom space to the superview to 0. See below:

//load the ButtonGridViewController from a xib
[[NSBundle mainBundle] loadNibNamed:@"..." owner:self options:nil];

//get the view add it
[self.view addSubView:self.myGridView];

//turn off springs and struts
[self.view setTranslatesAutoresizingMaskIntoConstraints:NO];

//add constraints to fill parent view
NSArray *arr;

//horizontal constraints
arr = [NSLayoutConstraint constraintsWithVisualFormat:@"|[vw]|"
                                                options:0
                                                metrics:nil
                                              views:@{@"vw":self.myGridView}];
[self.view addConstraints:arr];

//vertical constraints
arr = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[vw]|"
                                                options:0
                                                metrics:nil
                                                  views:@{@"vw":self.myGridView}];
[self.view addConstraints:arr];

这篇关于iOS版:自动布局VS autoresizingMask - 调整整个视图来填补?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 03:33