iOS8上自动布局引脚编程相对布局保证金 [英] iOS8 Auto layout programatically pin to relative layout margin
问题描述
我有一个UI元素( UISwitch
其实,不过没有关系真的)有在Interface Builder固定到上海华前沿和后空间。约束看起来像这样在X code 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.
现在我想添加在code另一种观点认为,这也将有固定它前沿和后空间是上海华缘,所以同样 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
酒店使用这种约束没有影响,因此它显然是不固定的保证金,而是直接到上海华。
This works, but layoutMargins
property has no effect using this constraint, so it is obviously not pinned to margin, but directly to superview.
所以我的问题是:
如何到管脚UI元素的空间用视觉形式语言code保证金?或者,如果没有可能,如何与针constraintWithItem:属性:relatedBy:toItem:属性:事半功倍:常数:
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屋!