如何使用 Swift 更改按钮的高度?

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

根据屏幕的高度,我想调整视图中按钮的高度。在 Swift 中执行此操作的最简单方法是什么?

我以这种方式尝试过,也尝试过 CGRectMake() 但没有任何改变:

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

如何"update"框架?

您看不到任何变化的原因可能是因为您正在使用 AutoLayout,并且按钮应用了一些约束,您需要更改高度约束才能完成您想要的。

已编辑:在 Swift 中似乎可以直接更改框架属性,但在 Objective C 中是不可能的。

如果您使用自动布局,则需要更新其高度限制,否则更新其框架

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));

已编辑检查此内容,看看什么是打印 NSLog