问题
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