我需要在视图中找到子视图垂直对齐中间,而不使用编程方式使用约束硬编码顶部值150。我希望实现以下目标:
以下是我的代码,请指教。
import UIKit
class MainViewController: UIViewController {
var viewInner:UIView = UIView()
var viewOuter:UIView = UIView()
override func viewDidLoad() {
super.viewDidLoad()
viewInner.backgroundColor = UIColor.redColor()
viewOuter.backgroundColor = UIColor.purpleColor()
viewOuter.frame = CGRectMake(100, 100, 400, 400)
viewInner.setTranslatesAutoresizingMaskIntoConstraints(false)
let viewsDictionary = ["viewInner":viewInner]
viewOuter.addSubview(viewInner)
self.view.addSubview(viewOuter)
//Add Constraint
var constOuterH = NSLayoutConstraint.constraintsWithVisualFormat("H:|-10-[viewInner(>=200)]-10-|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDictionary)
var constOuterV = NSLayoutConstraint.constraintsWithVisualFormat("V:|-150-[viewInner(100)]", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDictionary)
viewOuter.addConstraints(constOuterH)
viewOuter.addConstraints(constOuterV)
}
}
答案 0 :(得分:7)
您需要使用添加约束的非可视格式化方式:
[self.view addConstraint:[NSlayoutConstraint constraintWithItem:self.viewOuter attribute:NSLayoutAttributeCenterY relateBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY .......]];
使中心对齐。
以上类型的"关系约束" (这是我个人称之为的)是我倾向于用来指定两个视图之间的关系,例如中心对齐,相对定位。
另一方面,视觉格式化语言对于从我自己的观察中将视图固定到彼此更有用。这是一个演示应用:
@interface ViewController : UIViewController
@property (nonatomic, strong) UIView *outerView;
@property (nonatomic, strong) UIView *innerView;
@property (nonatomic, strong) NSLayoutConstraint *outerViewHeightConstraint;
@end
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self initViews];
[self initConstraints];
// change height of outerView after 3 seconds
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self changeHeight];
});
}
-(void)changeHeight
{
self.outerViewHeightConstraint.constant = 150;
[UIView animateWithDuration:1.0 animations:^{
[self.innerView layoutIfNeeded];
[self.view layoutIfNeeded];
}];
}
-(void)initViews
{
self.outerView = [[UIView alloc] init];
self.outerView.backgroundColor = [UIColor purpleColor];
self.innerView = [[UIView alloc] init];
self.innerView.backgroundColor = [UIColor redColor];
[self.outerView addSubview:self.innerView];
[self.view addSubview:self.outerView];
}
-(void)initConstraints
{
self.outerView.translatesAutoresizingMaskIntoConstraints = NO;
self.innerView.translatesAutoresizingMaskIntoConstraints = NO;
id views = @{
@"outerView": self.outerView,
@"innerView": self.innerView
};
// outer view constraints
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[outerView(300)]" options:0 metrics:nil views:views]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.outerView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.outerView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];
// give outerView a default height e.g. 300
// note we can animate the height of outerview later using this var
self.outerViewHeightConstraint = [NSLayoutConstraint constraintWithItem:self.outerView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:300.0];
[self.view addConstraint:self.outerViewHeightConstraint];
// inner view constraints
[self.outerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[innerView]-10-|" options:0 metrics:nil views:views]];
[self.outerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[innerView(50)]" options:0 metrics:nil views:views]];
[self.outerView addConstraint:[NSLayoutConstraint constraintWithItem:self.innerView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.outerView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]];
[self.outerView addConstraint:[NSLayoutConstraint constraintWithItem:self.innerView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.outerView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];
}
运行应用程序3秒钟后,您会看到外部视图(紫色视图)高度缩小,而红色内部视图在紫色视图中保持居中并保持其高度。
您也可以旋转视图。