我将从Springs和Struts迁移到Auto Layout作为iOS7的到来。在我想要实现以下目标之前一切正常,
我的搜索表单如下图所示。在某些情况下可以更改UITextField宽度。当文本字段的宽度发生变化时,搜索按钮的宽度也会发生变化,以保持彼此之间的边距和超视图。
在Springs和Struts的那一天,当我改变文本字段的宽度时,我必须自己计算搜索按钮的框架。但是,使用自动布局,这可以自动完成,这样我只需要改变文本字段的大小而不需要自己做数学吗?
Container
- Constraint width
Textfield
- Constraint Bottom, Top, Leading space to super view
- Constraint Trailing space to button
- fix width (will be adjust later)
Button
- Constraint Bottom, Top, trailing space to super view
- Constraint Leading space to textfield
由于
P.S。我还有另一个问题,但与SOF无关,当我在XCode中对齐,固定或排列某些内容时,是否可以禁用更改编辑器焦点。当我想向对象添加多个自动布局约束时,这有点烦人。
答案 0 :(得分:1)
刚试了一下,对我来说效果很好。
//Parent view constraint
NSLayoutConstraint *viewTopCon = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0];
NSLayoutConstraint *viewBottomCon = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:70.0];
NSLayoutConstraint *viewLeftCon = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.0];
NSLayoutConstraint *viewRightCon = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:10.0];
//Text field constraint
NSLayoutConstraint *textTopCon = [NSLayoutConstraint constraintWithItem:text attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeTop multiplier:1.0 constant:10.0];
NSLayoutConstraint *textBottomCon = [NSLayoutConstraint constraintWithItem:text attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-10.0];
NSLayoutConstraint *textLeftCon = [NSLayoutConstraint constraintWithItem:text attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:20.0];
NSLayoutConstraint *textRightCon = [NSLayoutConstraint constraintWithItem:text attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:btn attribute:NSLayoutAttributeLeft multiplier:1.0 constant:-20.0];
//Button constraint
NSLayoutConstraint *btnTopCon = [NSLayoutConstraint constraintWithItem:btn attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeTop multiplier:1.0 constant:10.0];
NSLayoutConstraint *btnBottomCon = [NSLayoutConstraint constraintWithItem:btn attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-10.0];
NSLayoutConstraint *btnRightCon = [NSLayoutConstraint constraintWithItem:btn attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeRight multiplier:1.0 constant:-30.0];
[self.view addConstraints:@[viewBottomCon,viewLeftCon,viewRightCon,viewTopCon]];
[view addConstraints:@[textBottomCon,textLeftCon,textRightCon,textTopCon]];
[view addConstraints:@[btnBottomCon,btnRightCon,btnTopCon]];
这里的视图是UIView的一个对象,它是searchField和button的超级视图。