I. Design Requirements
iOS Storyboard Auto-layout technology is the new technology that comes out of iOS 6, which is equivalent to multi-screen resolution adaptive technology.
But some of the complexities are still difficult to handle.
For example, there is an interface requirement, the progress bar shows the label, this demand is not difficult, difficult is influential in the Display table box, in various models display normal.
Initially set to vertically center up 15 pixels
This is the iphone 4S display effect, the following with the slider and dislocation
But under iphone 6, there's a mismatch underneath, but there's too much left.
However, if you set the offset to 21. Another situation occurs.
The big screen phone displays perfectly.
But the iphone 4S is misplaced.
So how do we solve this problem, if we can find a way, in the code to support the adjustment of some of the constants, such as in the iphone 4S I set it to 15, under the Iphone6 height of 21, this problem solved.
Two. Adjusting constants in the code
In fact, iOS also provides a way to tweak it by manually creating this constraint constant in code, and then assigning different models.
Another way is to find its constraint variable in the control, adjust the value with code, the latter is simpler, because I can set the variable for most models in storyboard, and then on the small screen model, fine-tuning.
Both tests can be implemented, and I now implement the latter one:
///find the corresponding constraint constants, pay attention to distinguish between FirstItem, or SecondItem. This is in the Storybord can see the explanation, than the area of the alignment, the alignment of the control is SecondItem.+ (Nslayoutconstraint *) Findfirstconstraint: (UIView*) Parentview FirstItem: (ID) FirstItem attribute: (nslayoutattribute) attribute{ for(Nslayoutconstraint *constraint in Parentview. Constraints) {Constraint. Priority, constraint. SecondItem);if((Constraint. FirstItem= = FirstItem) && (constraint. FirstAttribute= = attribute)) {NSLog(@"find! %@", FirstItem);returnConstraint } }return Nil;} + (Nslayoutconstraint *) Findsecondconstraint: (UIView*) Parentview SecondItem: (ID) SecondItem attribute: (nslayoutattribute) attribute{ for(Nslayoutconstraint *constraint in Parentview. Constraints) {if((Constraint. SecondItem= = SecondItem) && (constraint. Secondattribute= = attribute)) {NSLog(@"find! %@", SecondItem);returnConstraint } }return Nil;}
In the creation method, determine whether it is a iphone4s small screen, if the constraint constants and fonts are reduced so that you can join. Here the alignment offset is changed from 21 to 15, and the font size is reduced by number 2nd.
-(void) relayoutlabel{//Simulator always iphone6/iphone5--> 320x568 //iphone4s-320x480 // cgsizeresult = [[UIScreen mainscreen] bounds]. Size;if(Result. Height> -) {return; }//iphone version 4S or lessNslayoutconstraint *constraint;//Find constraint Constantsconstraint = [Utils findsecondconstraint: Self. Banner01SecondItem: Self. PercentAttribute:nslayoutattributecentery];// //// Vertical Center //constraint = [Nslayoutconstraint //ConstraintWithItem:self.banner01 //Attribute:nslayoutattributecentery //Relatedby:nslayoutrelationequal //ToItem:self.percent //Attribute:nslayoutattributecentery //multiplier:1.0f //constant:15.0f]; //[Self.banner01 addconstraint:constraint];Constraint. Constant= the;//by 21--" NSLog(@"Constraint%f,%d", constraint. Constant, constraint. FirstAttribute);Uifont* Font = [Uifontfontwithname:@"Helvetica"Size A];//font changed from 14--to "[ Self. PercentSetfont:font];}
Three. Final effect
The final result: (The IPhone 4S is a bit staggered, but has been the biggest effort, if you want to completely stagger, you can change the font size to 10)
Iphone4s
IPhone 6 Effects
IOS Auto Layout Extension app: Dynamically adjust layout constants in code