简体   繁体   English

如何更改 UIButton 的大小

[英]How to change size of UIButton

I'm trying to resize a UIButton in my iPhone app.我正在尝试在我的 iPhone 应用程序中调整 UIButton 的大小。 I have a UIButton synthesized and when I call the following code, it moves on the screen, but the width & height of the button never change.我合成了一个 UIButton,当我调用以下代码时,它会在屏幕上移动,但按钮的宽度和高度永远不会改变。

button.frame.size = CGRectMake(104, 68, 158, 70);

For example, when I change the height (70) to 40, the height of the button does not change.例如,当我将高度(70)更改为 40 时,按钮的高度不会改变。 If I change the x or y, however, it will move on the screen.但是,如果我更改 x 或 y,它将在屏幕上移动。

Any ideas?有任何想法吗?

The code above should not compile;上面的代码不应该编译; you're trying to assign a CGRect to a CGSize.您正在尝试将 CGRect 分配给 CGSize。 The problem comes in the misleading nature of properties.问题在于属性的误导性。 What you're doing above is akin to this:您在上面所做的类似于:

[button frame].size = CGSizeMake(150, 70);

Obviously, this won't actually change the size.显然,这实际上不会改变大小。 What you need to do is grab the frame (or bounds), modify it, and then set it.您需要做的是抓取框架(或边界),修改它,然后设置它。 Try this:尝试这个:

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

To change the size of the Button you do:要更改按钮的大小,您可以:

[button setFrame:CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height)];

example例子

[button setFrame:CGRectMake(173, 269, 130, 44)];

Make sure that the check box "Use Autolayout" is not set in the Xib file or Storyboard.确保未在 Xib 文件或 Storyboard 中设置复选框“使用自动布局”。 If the option is set then the command does not work.如果设置了该选项,则该命令不起作用。

Works without problems, as long autolayout and sizeclasses are deactivated.工作没有问题,只要自动布局和大小类被停用。 In my test the storyboard has just a button with his action send to this method:在我的测试中,故事板只有一个按钮,他的动作发送到这个方法:

- (IBAction)action:(UIButton *)button {
    button.frame = CGRectMake(0, 0, 158, 70);
}

Check the properties of your storyboard.检查故事板的属性。

在此处输入图片说明

Part of the problem is that you are modifying the returned value of an accessor method, rather than using the setter.部分问题在于您正在修改访问器方法的返回值,而不是使用 setter。

button.frame is deceptive, it is dot notation which means [button frame] which returns a struct CGRect , which you then modify, and the changes quietly get discarded. button.frame 具有欺骗性,它是点符号,这意味着[button frame]返回一个struct CGRect ,然后您对其进行修改,并且更改会悄悄地被丢弃。

In order to modify the actual frame you will need instead to use the setter method [button setFrame:] for which the dot notation is still button.frame but this time as an lvalue, so button.frame = not button.frame.something = .为了修改实际框架,您需要使用 setter 方法 [button setFrame:] ,其中点表示法仍然是button.frame但这次作为左值,所以button.frame = not button.frame.something = .

If you're still getting the movement behaviour, part of the cause of that could be that your new X and Y values don't correspond to the old ones, and maybe through some error your new size values are the same as the old ones.如果您仍然出现移动行为,部分原因可能是您的新 X 和 Y 值与旧值不对应,并且可能由于某些错误,您的新大小值与旧值相同. Try width of 300 and height of 300 just for kicks.尝试 300 的宽度和 300 的高度只是为了踢。

CGRect rect=CGRectMake(104, 68, 158, 70);

[self.button setBounds:rect];

(button needs to be declared as property in the .h , and synthesized in the .m) (按钮需要在 .h 中声明为属性,并在 .m 中合成)

Shouldn't this be:这不应该是:

button.frame = CGRectMake(104, 68, 158, 70);

ie you're creating the rect correctly, but you're assigning it to the size element of the CGRect called frame, not to the whole CGRect.即,您正确地创建了矩形,但是您将它分配给名为 frame 的 CGRect 的大小元素,而不是整个 CGRect。 From CGGeometry.h:来自 CGGeometry.h:

struct CGPoint {
   CGFloat x;
   CGFloat y;
};
struct CGSize {
   CGFloat width;
   CGFloat height;
};
struct CGRect {
   CGPoint origin;
   CGSize size;
};

Or more explicitely, you could write:或者更明确地说,你可以写:

button.frame.size.width=158;
button.frame.size.height=70;
button.frame.origin.x=104;
button.frame.origin.y=68;

要使用 Swift 3 更改按钮大小,代码如下:

 myButton.frame = CGRect(x: 104, y: 68, width: 158, height: 70);

If you are using autolayout, you can create a width constraint on your button.如果您使用自动布局,则可以在按钮上创建宽度约束。 You connect a reference to this to a variable NSLayoutConstraint in you code.您将对此的引用连接到代码中的变量 NSLayoutConstraint。 In the situation where you need to change the width of the button, you update the constant value of the width constraint in your code.在需要更改按钮宽度的情况下,您可以更新代码中宽度约束的常量值。

    @IBOutlet weak var buttonWidthConstraint: NSLayoutConstraint!

    if myConditionIsSatisfied {
        buttonWidthConstraint.constant = newWidth
    }

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

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