iOS8自动布局以编程方式固定到相对布局边距 [英] iOS8 Auto layout programmatically pin to relative layout margin

查看:147
本文介绍了iOS8自动布局以编程方式固定到相对布局边距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UI元素(实际上是UISwitch,但并不重要),该元素具有固定在Interface Builder中的superview的前导和尾随空间.约束在Xcode 6中看起来像这样:

I have an UI element (UISwitch actually, but does not matter really) that has both leading and trailing space pinned to superview in Interface Builder. The constraint looks like this in Xcode 6:

前导空间的约束实际上是相同的.约束的值为 42.0点.

这正是我想要的,因为对于不同的设备,我可以更改UIView上的layoutMargins属性,并且约束将正确运行,以增加视图之间的边距.

This is exactly what I want, because for different devices I can change layoutMargins property on UIView and the constraints will work correctly, to increase margin between views.

现在,我想在代码中添加另一个视图,该视图还将前导空间和尾随空间固定到其超级视图边距,因此设置为超级视图的相同layoutMargins将起作用.

Now I want to add another view in code that would also have both leading and trailing space pinned to it's superview margin, so the same layoutMargins set to superview will work.

我使用具有以下语法的视觉格式语言固定了视图:

I pinned the view using visual format language with the following syntax:

NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|-42.0-[separatorView]-42.0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(self.contentView, separatorView)];

[self.contentView addConstraints:constraints];
[self.contentView setNeedsUpdateConstraints];

这有效,但是layoutMargins属性使用此约束无效,因此显然它不是固定在边距上,而是直接固定在superview上.

This works, but layoutMargins property has no effect using this constraint, so it is obviously not pinned to margin, but directly to superview.

所以我的问题是:

如何使用视觉格式语言将UI元素空间固定在代码的空白处?或者,如果不可能的话,如何使用constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant: API?

How to pin UI element spaces to margin in code using visual format language? Or if not possible, how to pin with constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant: API?

推荐答案

在iOS8中,视觉格式语言已更新,因此"|-"或-|"将默认使用由超级视图的layoutMargins属性定义的间距.

In iOS8, the visual format language has been updated so that "|-" or "-|" will default to using a spacing defined by the superview's layoutMargins property.

因此,使用视觉格式语言的答案如下:

So the answer using visual format language is as follows:

// programmatically set the layoutMargins, only if
// you want non-default values and they are not already set in IB!
self.contentView.layoutMargins = UIEdgeInsetsMake(0,42,0,42); // set left and right margins to 42

// assume: seperatorView is already a subview of self.contentView

// separatorView will use the constraints because we write "-" between it and the superview edge
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|-[separatorView]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(separatorView)];
[self.contentView addConstraints:constraints];

如果在通过直接API创建约束时要引用布局边距,则可以使用仅iOS8的新布局属性:

If you want to refer to the layout margins when creating the constraints via the direct API, then you use the new iOS8 only layout attributes:

NSMutableArray * constraints = [NSMutableArray array]; 
[constraints addObject:[NSLayoutConstraint constraintWithItem:self.contentView 
     attribute:NSLayoutAttributeLeftMargin 
     relatedBy:NSLayoutRelationEqual 
     toItem:separatorView
     attribute:NSLayoutAttributeLeft
     multiplier:1.0
     constant:0]];
[constraints addObject:[NSLayoutConstraint constraintWithItem:self.contentView 
     attribute:NSLayoutAttributeRightMargin 
     relatedBy:NSLayoutRelationEqual 
     toItem:separatorView
     attribute:NSLayoutAttributeRight
     multiplier:1.0
     constant:0]];
[self.contentView addConstraints:constraints];

这篇关于iOS8自动布局以编程方式固定到相对布局边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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