Various restrictions for 3.5-inch and 4-inch

My mark is 4 inches from the 50pxbottom of the screen .

Now I want to use restrictions so that this shortcut is located 30pxat the bottom of the screen 3.5 inches . Is this possible with auto-layout and constraints?

When I set the lower limit to 30px, the label is also placed 30pxdown on the 4-inch screen . Basically, I want to reduce the space between my objects so that everything matches the 3.5 inch screen .

+4
source share
3 answers

If you need conditional constraints, you need to add constraints manually (at least the lower constraint, but I do not recommend using both codes and IB for constraints).

Add a test for the device and set a lower value based on this:

if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
    CGSize result = [[UIScreen mainScreen] bounds].size;
    if(result.height == 480) {
        distanceToBottom = 30; // pre-iPhone 5
    }

    if(result.height == 568) {
        distanceToBottom = 50; // iPhone 5
    }
}
0
source

If you really want it to be proportional to the size of the screen, you can make the restriction proportional:

// this constraint positions the label at the bottom of its superview
NSLayoutConstraint *bottomConstraint = [NSLayoutConstraint constraintWithItem:label 
                                                                    attribute:NSLayoutAttributeBottom
                                                                    relatedBy:NSLayoutRelationEqual
                                                                       toItem:label.superview
                                                                    attribute:NSLayoutAttributeBottom
                                                                   multiplier:1
                                                                     constant:0];

// move it up by 6% of the superview height
bottomConstraint.constant = -(label.superview.bounds.size.height * .06);

A few notes:

1) You will need to set this restriction constant when the view is laid out, therefore it label.superviewhas a height. updateConstraintsis a good place to do this.

2) , , layout.superview , , . , . , .

, @Handsomeguy :

bottomConstraint.constant = -([UIScreen mainScreen].bounds.size.height * .06);
0

- "" . , , , . , , . : " , ", .

,

With these values, I indicate that the bottom bottom of the label is always 95% of the lower bottom level (Btw, "bottom" is another way of saying "maximum Y value inside the frame", the same as' y '+' height " )

The recorded "lower" label values ​​for different devices:

"3.5" = 457   (427 + 30)
"4"   = 540.5 (510.5 + 30)
"4.7" = 634.5 (604.5 + 30)
"5.5" = 700.3 (670.3 + 30)
0
source

Source: https://habr.com/ru/post/1534156/


All Articles