I'm trying to resize a UIButton in my iPhone app. I have a UIButton synthesized and when I call the following code, it moves o开发者_如何转开发n the screen, but the width & height of the button never change.
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. If I change the x or y, however, it will move on the screen.
Any ideas?
The code above should not compile; you're trying to assign a CGRect to a 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. If the option is set then the command does not work.
Part of the problem is that you are modifying the returned value of an accessor method, rather than using the 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.
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 =
.
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. Try width of 300 and height of 300 just for kicks.
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.
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)
Shouldn't this be:
button.frame = CGRectMake(104, 68, 158, 70);
i.e. 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. From 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;
To change button size using Swift 3 this is the code:
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. 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
}
精彩评论