ios 在代码中为 topLayoutGuide 和 bottomLayoutGuide 创建自动布局约束
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19174451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating Auto Layout constraints to topLayoutGuide and bottomLayoutGuide in code
提问by smileyborg
Apple's documentationon creating Auto Layout constraints between a view and one of the layout guides only shows an example using VFL.
Apple关于在视图和布局指南之一之间创建自动布局约束的文档仅显示了使用VFL的示例。
Is there any way to create these constraints programmatically withoutVFL (using NSLayoutConstraint
's other APIor similar)?
有没有办法在没有VFL 的情况下以编程方式创建这些约束(使用NSLayoutConstraint
的其他 API或类似API)?
(Note: I'm specifically asking about doing this in code, not in Interface Builder. And I don't want the calculated length
of the guide set as a static constant on a constraint, I want a constraint where changes to the layout guide length would automatically cause the constrained view to adjust position.)
(注意:我特别要求在代码中执行此操作,而不是在 Interface Builder 中。而且我不希望length
将指南的计算设置为约束上的静态常量,我想要一个约束,其中更改布局指南长度会自动导致受约束的视图调整位置。)
回答by Jamie McDaniel
For a UIButton
that you want to place 20 points below the UIViewController.topLayoutGuide
you create the NSLayoutConstraint
like so:
对于UIButton
你想在下面放置 20 个点的a ,你可以像这样UIViewController.topLayoutGuide
创建NSLayoutConstraint
:
[NSLayoutConstraint constraintWithItem:self.button
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.topLayoutGuide
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:20.0];
With iOS 9 you can also create the NSLayoutConstraint
this way:
使用 iOS 9,您还可以通过NSLayoutConstraint
以下方式创建:
[self.button.topAnchor constraintEqualToAnchor:self.topLayoutGuide.bottomAnchor
constant:20.0];
回答by 0x6A75616E
To supplement @JamieMcDaniel's answer, the Swift + iOS9 version would be:
为了补充@JamieMcDaniel 的回答,Swift + iOS9 版本将是:
self.button.topAnchor
.constraintEqualToAnchor( self.topLayoutGuide.bottomAnchor ).active = true
Don't forget the .active = true
part as otherwise the constraint doesn't kick in automatically.
不要忘记该.active = true
部分,否则约束不会自动生效。
回答by Alex Y
Just an addition to @Jamie McDaniel, in case it's not immediately obvious, you need to add the constraint that he suggests to create:
只是对@Jamie McDaniel 的补充,以防它不是很明显,您需要添加他建议创建的约束:
NSLayoutConstraint *buttonTopConstraint = [NSLayoutConstraint constraintWithItem:self.button
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.topLayoutGuide
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:20.0];
[self.view addConstraint:buttonTopConstraint];
回答by Andrea
This is a gistI've created, you are supposed to embed all your subviews into a tank view(container view) added into a xib, it removes tank view-superview xib constraints and adds an upper constraints to topLayoutGuide giving an iOS6 look. It could be interesting for what you want to achieve.
这是我创建的一个要点,您应该将所有子视图嵌入到添加到 xib 的坦克视图(容器视图)中,它删除了坦克视图超级视图 xib 约束并向 topLayoutGuide 添加了上限约束,从而提供了 iOS6 外观。对于您想要实现的目标,这可能很有趣。
//This should be added before the layout of the view
- (void) adaptToTopLayoutGuide {
//Check if we can get the top layoutguide
if (![self respondsToSelector:@selector(topLayoutGuide)]) {
return;
}
//tankView is a contaner view
NSArray * array = [self.tankView referencingConstraintsInSuperviews]; //<--For this method get the Autolayout Demistified Book Sample made by Erica Sadun
[self.view removeConstraints:array];
NSArray * constraintsVertical = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[topLayoutGuide]-0-[tankView]|" options:0 metrics:nil views:@{@"tankView": self.tankView, @"topLayoutGuide":self.topLayoutGuide}];
[self.view addConstraints:constraintsVertical];
NSArray * constraintsHorizontal = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[tankView]|" options:0 metrics:nil views:@{@"tankView": self.tankView}];
[self.view addConstraints:constraintsHorizontal];
}