简体   繁体   English

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

[英]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.

相关问题 如何使用按钮更改高度限制? - How can I change constraint of height with button? 如何在Swift中更改UIAlertController中UITextField的高度? - How can I change the height of UITextField in UIAlertController in Swift? 如何快速更改UISearcherController行的高度 - How can i change the UISearcherController row's height in swift 如何更改导航栏的高度 - Swift 3 - How can I change height of Navigation Bar - Swift 3 如何在Swift的UITableView中将“编辑”按钮更改为“后退”按钮? - How can I change “edit” button to back button in UITableView in Swift? 如何更改按钮上的操作目标-Swift - How can I change the target of the action on a button - Swift 如何在Swift 3中更改UISearchBar“取消”按钮的颜色? - How can I change the color of UISearchBar “cancel” button in Swift 3? (快速)如何通过触摸按钮在pageviewcontroller中更改视图 - (Swift) how can I change view in pageviewcontroller with touching button UITableView:如何在单击按钮时动态更改单元格高度? 迅速 - UITableView: How to change cell height dynamically when a button is clicked in it? Swift 如何在Swift中以编程方式更改NavigationBar的高度? - How do I programmatically change the height of a navigationBar in Swift?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM