Tuesday, September 24, 2013

CGSizeMake usage advice

It is required to define a UIButton, the first single set its height and width, and then displayed on the screen a certain point.

I do not know how to use CGSizeMake. My code is as follows.

UIButton * b = [UIButton buttonWithType: UIControlStateNormal];
b.size = CGSizeMake (100, 100) ;/ / error
phrase

Reply:
[B setFrame: CGRectMake (0,0,100,100)];
Reply:
Back upstairs, this is not still set this control center yet? My mind is
1 first set this control's Width Height
2 In another method to obtain this control, then use a method to set this control center display.

So, I asked two functions, one is individually set the width of the control's height api, the other is a control set to the x, y coordinates of the point.

In fact, the CGRectMake apart, first set the height width, after which the logic in setting up the center.
Reply:
b.frame.size = CGSizeMake (100,100);
Reply:
frame.size is read-only bars

You can not set up the first center ah, such as: [b setFrame: CGRectMake (b.frame.origin.x, b.frame.origin.y, 100,100)];
This center unchanged, only change the size

Step change if only the center, it does not change the size, so
[B setFrame: CGRectMake (50,50, b.frame.size.width, b.frame.size.height)];
Reply:
 
/ / Set Size
- (Void) setSizeOfButton: (UIButton *) btn withSize: (CGSize) newSize
{
btn.frame = CGRectMake (btn.origin.x, btn.origin.y, newSize.width, newSize.height);
}

/ / Set the center point
- (Void) setCenterOfButton: (UIButton *) btn withCenterPoint: (CGPoint) centerPoint
{
btn.center = centerPoint;
}

No comments:

Post a Comment