Search code examples
iosobjective-ciphoneautolayout

I want to apply Auto Layout to UILabel like flexible height & width


I applied flexible height & width to my UILabel and now I want to use that type of functionality by using Auto Layout.

I am able to increase height and width of UILabel as Device width and height, but X and Y is not changed according to Device width and Height.

Ex:-

Device :- iPhone 7
UILabel :-
        X      : 20
        Y      : 120
        Width  : 300
        Height : 30


Device :- iPhone SE
UILabel :-
        X      : 20 
        Y      : 120
        Width  : 245
        Height : 25

Now problem is that Size of UILabel decrease with aspect ratio but X & Y coordinate was not changed. According to flexible height & width that will set like 17 & 102, but in auto layout it was not change.

Constraint :

Constraint Image


Solution

  • You have fixed X and Y Position for your label (Which is not device specific)

    If you need to achieve this you need to set X and Y according to Centre of your view

    I have create to set centre X (For Y Pos )

    enter image description here

    enter image description here

    And How I have set centre of Y Pos

    enter image description here

    EXPLAINATION

    The centre of UIView according to self.view is 318.5 and required space from top is 20 so final multiplier is 20:318.5

    as every view has different centre For Iphone SE centre would be around 17

    Don't Forgot to set First Item to Label.Top as we need 20 from TOP

    OUTPUT:

    enter image description here

    Hope it is clear to you :)