简体   繁体   English

无法更改UIButton的大小

[英]Can't change the size of a UIButton

This seems such a simple thing, I can't understand why it's going wrong. 这看起来很简单,我无法理解为什么会出错。 I'm trying to change the size of a UIButton from the touchUpInside handler of another. 我正在尝试从另一个的touchUpInside处理程序更改UIButton的大小。 To repro, I've created a new (iOS 6) project in Xcode, and dragged two buttons to the xib file that's created by default. 为了重新编写,我在Xcode中创建了一个新的(iOS 6)项目,并将两个按钮拖到默认创建的xib文件中。 The touchUpInside handler for button1 is shown below. button1touchUpInside处理程序如下所示。 If I just include the statement that sets the origin.x of button2 , I see button2 move as expected. 如果我只包含设置button2origin.x的语句,我会看到button2按预期移动。 However, the statement that sets the size.width of the button doesn't work. 但是,设置按钮的size.width的语句不起作用。 In fact, if I include that statement the origin doesn't change either. 事实上,如果我包含该陈述,原点也不会改变。

I can resize the button fine in the xib designer and the only property I've set (other than 我可以在xib设计器中调整按钮的大小,这是我设置的唯一属性(除了

- (IBAction)buttonPressed:(id)sender {
    CGRect f = self.button2.frame;
    f.origin.x += 10;
    // this doesn't work, including it prevents even the origin change from working
    f.size.width += 10; 
    self.button2.frame = f;
}

How can I successfully change the size of the button? 如何成功更改按钮的大小?

Thanks! 谢谢!

For sure you are using AutoLayout, simply what happen here is iOS AutoLayout mechanism tries to set your layout automatically. 当然,你正在使用AutoLayout,这里发生的事情是iOS AutoLayout机制试图自动设置你的布局。 try your code by turning off AutoLayout. 通过关闭AutoLayout来尝试您的代码。 hope you know how to turn off the auto layout in xcode, if not let me know. 希望你知道如何关闭xcode中的自动布局,如果没有让我知道。

Happy coding!!! 快乐的编码!

EDIT: 编辑:

AutoLayout comes with iOS 6, thats why your code worked with older version(iOS 5). AutoLayout附带iOS 6,这就是为什么你的代码适用于旧版本(iOS 5)的原因。 btw as Nick said you can turn off AutoLayout in the file inspector in the xcode. 顺便说一下Nick说你可以在xcode的文件检查器中关闭AutoLayout。

See attached screenshot. 请参见附件截图。

在此输入图像描述

Try : 试试:

CGRect buttonFrame = button.frame;
buttonFrame.size = CGSizeMake(150, 70);
button.frame = buttonFrame;

Work with the bounds: 使用边界:

[self.button setBounds:CGRectMake(0, 0, 400, 400)];

Works whether or not auto layout is on! 无论是否自动布局,都可以使用! As always, make sure your Outlet is properly set up so that you can talk to the button. 与往常一样,确保您的插座设置正确,以便您可以与按钮通话。

You could do this in auto layout as well. 您也可以在自动布局中执行此操作。 Remove the width and height constraints and add in the new ones, but if you don't have a width or height constraint on your button (probably wise) then just set the bounds and unless you need to reposition your button it should just work as auto layout will automatically update the constraints when you change the buttons bounds. 删除宽度和高度约束并添加新的约束,但如果您的按钮上没有宽度或高度约束(可能是明智的),那么只需设置边界,除非您需要重新定位按钮,它应该只是作为更改按钮边界时,自动布局将自动更新约束。

BTW, you should never be touching a buttons frame unless you're using it to position the object in a manual way. 顺便说一句,除非你用它来手动定位物体,否则你永远不应该触摸按钮框架。 Bounds are for sizing, frame is for positioning. 边界用于定尺寸,框架用于定位。

Disabling the auto-layout may not be the solution you want. 禁用自动布局可能不是您想要的解决方案。

One possibility is that you set the button attributed title before and now just changing the title font size won't work. 一种可能性是您之前设置按钮属性标题,现在只是更改标题字体大小将无法正常工作。 You need to setAttributedTitle again 你需要再次setAttributedTitle

If you are trying to change a UIButton text length, you will be restricted by the default length and when it is too long a line break will be implemented (ie Truncate Tail or three dots). 如果您尝试更改UIButton文本长度,您将受到默认长度的限制,当它太长时,将实现换行(即截断尾部或三个点)。 Change the default size by: 更改默认大小:

yourButton.setTitle("yourString", for: UIControlState.normal)

This will work for most cases but not all UIControlState s 这适用于大多数情况,但不适用于所有UIControlState

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM