如何在ios中逐步添加子视图和子视图按钮标签的布局?
Posted
技术标签:
【中文标题】如何在ios中逐步添加子视图和子视图按钮标签的布局?【英文标题】:How to add layout for subview and subview button label step by step in ios? 【发布时间】:2015-08-21 10:08:39 【问题描述】:如何为视图控制器的子视图添加约束 简单解释一下
【问题讨论】:
请详细说明您的问题 【参考方案1】:// create a new view
self.containerView = [UIView new];
// before adding constraints always add respective view to superview
[self.view addSubview:self.containerView];
[self.containerView setTranslatesAutoresizingMaskIntoConstraints:false];
// add container on newly added view
NSLayoutConstraint *topConstraintContainer = [NSLayoutConstraint constraintWithItem:self.containerView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.topLayoutGuide attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
NSLayoutConstraint *leadingConstraintContainer = [NSLayoutConstraint constraintWithItem:self.containerView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.containerView.superview attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0];
NSLayoutConstraint *trailingConstraintContainer = [NSLayoutConstraint constraintWithItem:self.containerView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.containerView.superview attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0];
NSLayoutConstraint *bottomConstraintContainer = [NSLayoutConstraint constraintWithItem:self.containerView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.bottomLayoutGuide attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
// add constraint to superview of respective view
[self.view addConstraints:@[topConstraintContainer,leadingConstraintContainer,trailingConstraintContainer,bottomConstraintContainer]];
有很多可能性,但这是主要流程 还有可用的第三方库,可以更轻松地添加约束,例如 PureLayout
【讨论】:
以上是关于如何在ios中逐步添加子视图和子视图按钮标签的布局?的主要内容,如果未能解决你的问题,请参考以下文章