[英]How can I change the height of a button with Swift?
Depending on the height of the screen, I would like to adjust the height of a button in a view. 根据屏幕的高度,我想调整视图中按钮的高度。 What is the easiest way to do this in Swift?
在Swift中最简单的方法是什么?
I tried it in this way and also with CGRectMake()
but nothing changed: 我用这种方式尝试了它,也用
CGRectMake()
但没有改变:
self.myButton.frame.size.height = self.myButton.frame.size.height*scrOpt
How can I "update" the frame? 如何“更新”框架?
The reason why you see no changes may be because you're using AutoLayout, and the button has some constraints applied to it, and you need to change the height constraint to accomplish what you want. 您没有看到任何更改的原因可能是因为您正在使用AutoLayout,并且该按钮应用了一些约束,您需要更改高度约束以完成您想要的操作。
Edited : Changing frame properties directly seems to be possible in Swift but was not possible in Objective C. 编辑 :在Swift中直接更改框架属性似乎是可能的,但在Objective C中是不可能的。
If you are using auto layout, you need to update its height constraint, else update its frame 如果使用自动布局,则需要更新其高度约束,否则更新其框架
NSLog(@"%@",NSStringFromCGRect(self.myButton.frame));
NSLog(@"%f",scrOpt);
self.myButton.frame = CGRectMake(self.myButton.frame.origin.x, self.myButton.frame.origin.y, self.myButton.frame.size.width, self.myButton.frame.size.height*scrOpt)
NSLog(@"%@",NSStringFromCGRect(self.myButton.frame));
Edited check this and see what is Print NSLog
编辑检查这一点,看看什么是打印
NSLog
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.