简体   繁体   English

如何在iOS中更改视图的顶部填充?

[英]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. 我想在切换按钮时将视图的顶部填充更改为80。 How would this be done? 怎么做? Basically I want there to a space of 80 between the top of the view and the text. 基本上,我希望视图顶部和文本之间有80的间距。

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. 您可以将其连接到代码中的NSLayoutConstraint,然后在按下按钮时将constraint.constant设置为新值。

@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];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM