如何以编程方式创建布局约束 [英] How to Create layout constraints programmatically
本文介绍了如何以编程方式创建布局约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在通用应用程序的底部显示一个视图,并在我的视图中动态添加此视图。我想显示这个视图在底部每次像iAd。在两个方向。如何为此添加约束。请建议。
感谢
解决方案
您需要以下约束来设置屏幕底部。
- 领先约束< 宽度
- < - >
- 高度约束
pre>UIView * subView = bottomView;
UIView * parent = self.view;
subView.translatesAutoresizingMaskIntoConstraints = NO;
// Trailing
NSLayoutConstraint * trailing = [NSLayoutConstraint
constraintWithItem:subView
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:parent
attribute:NSLayoutAttributeTrailing
multiplier:1.0f
常数:0.f];
//前导
NSLayoutConstraint * leading = [NSLayoutConstraint
constraintWithItem:subView
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:parent
attribute:NSLayoutAttributeLeading
multiplier:1.0f
constant:0.f];
// Bottom
NSLayoutConstraint * bottom = [NSLayoutConstraint
constraintWithItem:subView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:parent
attribute:NSLayoutAttributeBottom
multiplier:1.0f
常数:0.f];
//与AdHeight一样的SubView的固定高度
NSLayoutConstraint * height = [NSLayoutConstraint
constraintWithItem:subView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:0
常数:ADHeight];
[parent addConstraint:trailing];
[parent addConstraint:bottom];
[parent addConstraint:leading];
[subView addConstraint:height];
希望这有助。
I am displaying a view in the bottom of the universal application and adding this view dynamically in my view. I want to show this view in bottom every time like iAd. in both orientation. How can I add constraints for this. Please suggest.
Thanks
解决方案To fix a view to the bottom of the screen you need following constraints to set.
- Leading Constraint with respect of Parent View for - X
- Trailing Constraint with respect of Parent View for - Width
- Bottom Constraint with respect of Parent View for - Y
- Height Constraint attached to self for - Height.
Lets add.
UIView *subView=bottomView; UIView *parent=self.view; subView.translatesAutoresizingMaskIntoConstraints = NO; //Trailing NSLayoutConstraint *trailing =[NSLayoutConstraint constraintWithItem:subView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:parent attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:0.f]; //Leading NSLayoutConstraint *leading = [NSLayoutConstraint constraintWithItem:subView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:parent attribute:NSLayoutAttributeLeading multiplier:1.0f constant:0.f]; //Bottom NSLayoutConstraint *bottom =[NSLayoutConstraint constraintWithItem:subView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:parent attribute:NSLayoutAttributeBottom multiplier:1.0f constant:0.f]; //Height to be fixed for SubView same as AdHeight NSLayoutConstraint *height = [NSLayoutConstraint constraintWithItem:subView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0 constant:ADHeight]; [parent addConstraint:trailing]; [parent addConstraint:bottom]; [parent addConstraint:leading]; [subView addConstraint:height];
Hope this helps.
Cheers.
这篇关于如何以编程方式创建布局约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
的父视图 X
查看全文