简体   繁体   中英

Define the size of a UILabel programmatically when view appears

It seems like autolayout overrides commands in 'ViewDidLoad'.

Target : I want to define the size of a label programmatically to reflect the importance of my label. I DON'T want the label to fit the size of the text. I don't want neither the text of the label to fit the size of the label.

I code in Swift and Xcode 6.

When I use in 'ViewDidLoad'

        team1ScoreLabel.frame.size.width=400

it has no effect.

When I copy and paste it in a button action function, it resizes as I wish the size of the label. So the line of code is OK.

To me, it means autolayout constraints does not allow the label to be resize in 'ViewDidLoad' method. I have tried with no success in 'ViewDidAppear'

Any idea how to resize the label size when the view appears ? I don't want to disable autolayout because I'm using it a lot for other constraints.

You can't move views around by changing their frames when auto-layout is in effect. When any of the layout methods get called, the constraints on the views will move the views back based on their rules.

As PaulW11 says in his comment, you want to create constraints and connect them to IBOutlets and then change the values of the constraints in your code. Note that viewDidLoad is probably too soon, since at that point the view controller's content view hasn't been adjusted for the size and orientation of the screen. You probably want to do your adjustments in viewWillAppear.

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