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

Depending on the height of the screen, I would like to adjust the height of the button in the view. What's the easiest way to do it in Swift?

I tried it this way and also with CGRectMake()

, but nothing changed:

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

      

How can I "update" a frame?

+3


source to share


2 answers


The reason you don't see the changes might be because you are 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 possible in Swift, but it is not possible in Objective C.

+5


source


If you are using auto layout you need to update its height limit, otherwise 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));

      



Edit this check and see what Print is NSLog

0


source







All Articles