How can I change the height of a button with Swift?

随声附和 提交于 2020-01-14 11:54:24

问题


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?

I tried it in this way and also with CGRectMake() but nothing changed:

self.myButton.frame.size.height = self.myButton.frame.size.height*scrOpt

How can I "update" the frame?


回答1:


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.

Edited: Changing frame properties directly seems to be possible in Swift but was not possible in Objective C.




回答2:


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



来源:https://stackoverflow.com/questions/30026390/how-can-i-change-the-height-of-a-button-with-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!