IOS Automatic Layout extension application: dynamically adjust the layout constant in the code

Source: Internet
Author: User

IOS Automatic Layout extension application: dynamically adjust the layout constant in the code
I. design requirements

The automatic layout technology of iOS Storyboard is a new technology developed by iOS 6, which is equivalent to adaptive technology with multi-screen resolution.

However, some complicated cases are still difficult to handle.

For example, if you want to display labels on the progress bar, it is not difficult to display the labels in various models in the display table box.

Initially set to vertical center and up to 15 pixels

This is the iPhone 4S display effect, below with the slider there is dislocation

However, the following error is displayed on the iPhone 6, but it is left empty.

However, if the offset is set to 21, another situation occurs.

The big screen mobile phone displays perfectly.

But the iPhone 4S went wrong.

So how can we solve this problem? If we can find a method that supports adjusting this constant in the Code, for example, setting it to 15 under iPhone 4S, this problem is solved when the iPhone 6 is 21.

Ii. Adjust constants in the code

In fact, iOS also provides this method to adjust. One way is to manually create this constraint constant in the Code and assign different values to different models.

Another way is to find the constraint variable in the control and adjust the value with the Code. The latter is simpler, because I can set the variable for most models on the Storyboard, in the small screen model, fine-tune it.

Both tests can be implemented. I now implement the following:

// Find the corresponding constraint constant. check whether it is firstItem or secondItem. this can be seen in Storybord. When the alignment is in the same area, the alignment 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 = attribu Te) {NSLog (@ "find! % @ ", FirstItem); return constraint ;}} 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); return constraint ;}} return nil ;}

In the creation method, determine whether the screen is an iPhone 4S small screen. If the constraints and fonts are reduced, you can add them. Here, the alignment offset is changed from 21 to 15, and the font size is reduced by 2.

-(Void) relayoutLabel {// always iPhone6/iPhone5 in the simulator --> 320x568 // iPhone4S --> 320x480 // CGSize result = [[UIScreen mainScreen] bounds]. size; if (result. height> 500) {return;} // NSLayoutConstraint * constraint for iPhone 4S or earlier; // find the constraint constant constraint = [Utils findSecondConstraint: self. banner01 secondItem: self. percent attribute: 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 = 15; // by 21 -- "15 NSLog (@" constraint % f, % d ", constraint. constant, constraint. firstAttribute); UIFont * font = [UIFont fontWithName: @ "Helvetica" size: 12]; // The font is changed from 14 -- to 12 [self. percent setFont: font];}
Iii. Final Results

The final implementation of the effect :( iPhone 4S is still a little staggered, but has the best effort, if you want to completely staggered, you can change the font size to 10)

IPhone4S

IPhone 6 Effects

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.