简体   繁体   中英

How do you change the top-padding for a view in iOS?

I want to change the top padding of a view to 80 when a button is toggled. How would this be done? Basically I want there to a space of 80 between the top of the view and the text.

If you are using autolayout, then the easiest thing to do is to create a constraint for the distance between the text and the top of the view. You can connect it to a NSLayoutConstraint in code, and set the constraint.constant to the new value when the button is pressed.

@property (nonatomic, weak) IBOutlet NSLayoutConstraint *paddingConstraint;


// Somewhere after the button press
self.paddingConstraint.constant = 80;

Oh, and you will probably need to update the layout by calling this -

[self layoutIfNeeded];

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