Ios7 设置位置、高度、宽度自动布局?

Ios7 设置位置、高度、宽度自动布局?,ios7,uibutton,autolayout,Ios7,Uibutton,Autolayout,我刚刚切换到自动布局,在使用之前: [Btn setFrame:CGRectMake(165,164,135,35)]; 但我知道,对于自动布局,您不使用设置帧。我知道应该是这样的: self.constraintToAnimate = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toIt

我刚刚切换到自动布局,在使用之前:

[Btn setFrame:CGRectMake(165,164,135,35)];
但我知道,对于自动布局,您不使用设置帧。我知道应该是这样的:

self.constraintToAnimate = [NSLayoutConstraint constraintWithItem:label
     attribute:NSLayoutAttributeTop
     relatedBy:NSLayoutRelationEqual
        toItem:self.view
     attribute:NSLayoutAttributeBottom
    multiplier:0.25
      constant:0.0];
这是:

[self.scrollView addConstraints:[NSLayoutConstraint
                                       constraintsWithVisualFormat:@"V:|-[Btn(==300)]-|"
                                       options:NSLayoutFormatDirectionLeadingToTrailing
                                       metrics:nil
                                       views:NSDictionaryOfVariableBindings(Btn)]];
但我不确定它们各自的作用,也不确定constraintsWithVisualFormat字符串的含义/工作原理

如果您能告诉我如何设置Btn的位置、高度和宽度,我将不胜感激


提前感谢。

好的,我必须推断一下您试图实现的布局,因为您没有明确指定它。我假设您有一个UIScrollView,其中有一个按钮作为子视图,使事情复杂化

由于您不熟悉约束,让我们从一个更简单的案例开始,这是一个UIButton,它是UIView的子视图。UIButton已经知道其宽度和高度应该基于其内容,因此您只需要指定其x和y位置(2个约束)。我个人更喜欢等式形式的书写约束,而不是视觉格式,所以我将使用它。你想要的是:

button.x = button.superview.x*1 + 0;
button.y = button.superview.x*1 + 0;
.x是一个
前导
约束(前导从左起),而.y是一个
顶部
约束(从顶部起)

接下来,由于约束位于子视图与其superview之间,因此将约束添加到superview

[button.superview addConstraint:[NSLayoutConstraint constraintWithItem:button  attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:button.superview attribute:NSLayoutAttributeTop multiplier:1.0 constant:164.0]];

[button.superview addConstraint:[NSLayoutConstraint constraintWithItem:button  attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:button.superview attribute:NSLayoutAttributeLeading multiplier:1.0 constant:165.0]];
这会将按钮定位在(164165),按钮的宽度和高度来自按钮的固有大小(基于其图像/文本)

这是一个简单的例子。现在让我们转到button.superview是UIScrollView的场景。这更为复杂,因为您需要以不同的思维方式使用UIScrollview来考虑约束,即将固定窗口大小(框架)视为无限大的景观(contentSize)

假设您有一个UIView-->UIScrollView-->UIButton,其中UIView有一个子scrollview,而子scrollview又有一个UIButton作为子视图。一步一个脚印

1.)UIView-->UIScrollView

在这对上写入的约束位于UIView的框架和UIScrollView的框架之间。这是非常容易的,因为框架是固定大小的。假设您希望UIScrollView是它所嵌入的UIView的完整大小。您可以编写4个约束来指定它的X、Y、宽度和高度。有很多方法可以做到这一点,当然,我会列出一个

scrollview.x (scrollview.frame.x) = scrollview.superview.x*1 + 0;
scrollview.y (scrollview.frame.y) = scrollview.superview.y*1 + 0;
scrollview.width (scrollview.frame.width) = scrollview.superview.width*1 + 0;
scrollview.height (scrollview.frame.height) = scrollview.superview.height*1 + 0;
在约束条件下,这将是:

 // Let's constrain the scrollview to stick to the top and left of it's superview
[scrollView.superview addConstraint:[NSLayoutConstraint constraintWithItem:scrollView  attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:scrollView.superview attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];

[scrollView.superview addConstraint:[NSLayoutConstraint constraintWithItem:scrollView  attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:scrollView.superview attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0]];

// Let's constrain the scrollview to make its width and height equal to its superview
[scrollView.superview addConstraint:[NSLayoutConstraint constraintWithItem:scrollView  attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:scrollView.superview attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0]];

[scrollView.superview addConstraint:[NSLayoutConstraint constraintWithItem:scrollView  attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:scrollView.superview attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0]];
好了,现在scrollview知道了如何定位它的框架。这是最简单的部分

2.)将按钮置于滚动视图内

[scrollView addConstraint:[NSLayoutConstraint constraintWithItem:contentView  attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];

[scrollView addConstraint:[NSLayoutConstraint constraintWithItem:contentView  attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0]];

[scrollView addConstraint:[NSLayoutConstraint constraintWithItem:contentView  attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]];

[scrollView addConstraint:[NSLayoutConstraint constraintWithItem:contentView  attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0]];
好的,现在你必须改变你的想法。由于scrollView是按钮的父级,因此不能约束到scrollView的,而必须约束到scrollView的内容视图。理论上这是一个无限平面

那么,如何在无限平面中约束视图呢?UIScrollView中没有这样的设置,所以我们被卡住了吗

不,我们需要做的是为UIScrollView定义一个内容视图。让我们将另一个视图拖动到UIScrollView中,并将按钮放置在该视图中。我们的视图层次结构现在应该是这样的

UIView(topView)-->UIScrollView-->UIView(contentView)-->UIButton(全部嵌套)

现在我们需要设置约束来定义contentView的大小。假设contentView与topView的大小相同。让我们添加约束使其成为现实。以下4个约束说明内容视图是scrollview的平面

[scrollView addConstraint:[NSLayoutConstraint constraintWithItem:contentView  attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];

[scrollView addConstraint:[NSLayoutConstraint constraintWithItem:contentView  attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0]];

[scrollView addConstraint:[NSLayoutConstraint constraintWithItem:contentView  attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]];

[scrollView addConstraint:[NSLayoutConstraint constraintWithItem:contentView  attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0]];
但这还不够,因为我们说scrollview基本上是一个进入无限平面的窗口。因此,我们需要在contentView上增加两个约束来指定它的宽度和高度。让我们让它们与顶层视图相同。请注意,我们正在设置contentView和topView之间的约束,完全绕过了ScrollView

// You could make the constant parameter non-zero if you wanted it to be bigger/smaller (+/-) than the view
[topView addConstraint:[NSLayoutConstraint constraintWithItem:contentView  attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:topView attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0]];

[topView addConstraint:[NSLayoutConstraint constraintWithItem:contentView  attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:topView attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0]];
现在我们终于可以在按钮上定义约束了,它可以按如下所示编写,这样您就可以按照最上面的定义编写,因为contentView==button.superview

[contentView addConstraint:[NSLayoutConstraint constraintWithItem:button  attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:contentView attribute:NSLayoutAttributeTop multiplier:1.0 constant:164.0]];

[contentView addConstraint:[NSLayoutConstraint constraintWithItem:button  attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:contentView attribute:NSLayoutAttributeLeading multiplier:1.0 constant:165.0]];
现在我们应该正确设置所有约束条件。要强制布局,只需调用
[topView layoutifneed]
,希望这是清楚的,如果您有任何问题,请告诉我。我可能错过了什么,因为我不得不把这句话从我的头顶上写下来:)