在code创建自动布局约束topLayoutGuide和bottomLayoutGuide [英] Creating Auto Layout constraints to topLayoutGuide and bottomLayoutGuide in code

查看:1352
本文介绍了在code创建自动布局约束topLayoutGuide和bottomLayoutGuide的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

苹果公司<一href=\"https://developer.apple.com/library/ios/documentation/uikit/reference/UIViewController_Class/index.html#//apple_ref/occ/instp/UIViewController/topLayoutGuide\">documentation在视图和布局指南之一之间建立自动布局的限制仅显示了一个例子<一个href=\"https://developer.apple.com/library/mac/documentation/userexperience/conceptual/AutolayoutPG/Articles/formatLanguage.html\">VFL.

Apple's documentation on creating Auto Layout constraints between a view and one of the layout guides only shows an example using VFL.

有没有办法通过编程的创建这些约束,而不 VFL(使用<一个href=\"https://developer.apple.com/library/mac/documentation/AppKit/Reference/NSLayoutConstraint_Class/index.html#//apple_ref/occ/clm/NSLayoutConstraint/constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:\"><$c$c>NSLayoutConstraint's其他API 或类似)?

Is there any way to create these constraints programmatically without VFL (using NSLayoutConstraint's other API or similar)?

(注:我专门询问有关code这样做,不是在Interface Builder而且我不想计算的长度的指导。设置为一个约束的静态常量,我希望有一个约束,其中更改布局引导长度会自动导致约束视图来调整位置。)

(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.)

推荐答案

对于的UIButton 要放在下面的的UIViewController 20分。 topLayoutGuide 创建 NSLayoutConstraint 像这样:

For a UIButton that you want to place 20 points below the UIViewController.topLayoutGuide you create the NSLayoutConstraint like so:

[NSLayoutConstraint constraintWithItem:self.button
                             attribute:NSLayoutAttributeTop
                             relatedBy:NSLayoutRelationEqual
                                toItem:self.topLayoutGuide
                             attribute:NSLayoutAttributeBottom
                            multiplier:1.0
                              constant:20.0];

与iOS 9,你还可以创建 NSLayoutConstraint 是这样的:

[self.button.topAnchor constraintEqualToAnchor:self.topLayoutGuide.bottomAnchor
                                      constant:20.0];

这篇关于在code创建自动布局约束topLayoutGuide和bottomLayoutGuide的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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