一个用于添加子视图且无需考虑设置 NSLayoutConstraint 的即插即用解决方案。
一个 UIView 的子类,它为其父视图的内部边界创建约束。
将 BCAutoLayoutView.h 和 BCAutoLayoutView.m 复制到您的目录中。
最小实现
// In some ViewController files...
// create a UIView with 100x100 size
BCAutoLayoutView *view = [[BCAutoLayoutView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
// view will be added with Top(0), Bottom(0), Leading(0), Trailing(0) constraints to superview
[self.view addSubview:view];
使用
- (void)setSpacingWithSuperView:(BCAutoLayoutSpace)space;
将约束设置到 BCAutoLayoutView
// this can be called after the view is setup and at the time with the view is already added in the superview
// once called, the view will be added with Top(10), Bottom(20), Leading(30), Trailing(40) constraints
[view setSpacingWithSuperView:BCAutoLayoutMakeSpacing(10, 20, 30, 40)];