简体   繁体   中英

Auto Layout : Adjust width base on related view

I'm migrating from Springs and Struts to Auto Layout as the arriving of iOS7. It is fine until I want to achieve the following,

I have a search form looks like following image. The UITextField width can be changed in some circumstances. When, the width of the textfield change, the width of the search button will be changed too to maintain the margin between each other and their superview.

In the day of Springs and Struts, I had to calculate the frame of search button by myself when I change the width of textfield. But, with Auto Layout, can this be done automatically so that I just have to change the size of textfield and no need to do the math myself?

在此处输入图片说明

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

Thanks

PS I also have another question but not related to SOF, can I disable the changing of editor focus when I align, pin or arrange something in XCode. It's kinda annoying when I want to add multiple Auto Layout Constraints to an object.

Just gave a try, worked well for me.

//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]];

Here view is an object of UIView which is the superview of searchField and button.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM