开发者

*modify* UIbutton position (Iphone SDK)

I'm playing around with UIbuttons, just to get a feel on what can really be done with them. I have only one problem so far:

How do I modify the position of a UIButton?

- (IBAction)buttonClicked:(id)sender
{
   UIButton *senderB = sender;

   CGPoint position = senderB.frame.origin;
   CGSize size = senderB.frame.size;
   senderB.frame = CGRectMake(position.x,position.y + 10,size.width,size.height);
}

The above works just fine, however, creating a new CGr开发者_如何学运维ect for every time I want to simply change one seems rather inefficient to me.

Is there any way for me to directly set the values of senderB.frame.origin.x, etc?


I usually do it like this:

CGRect buttonFrame = button.frame;
buttonFrame.origin.y += 10;
button.frame = buttonFrame;


Nope. Notice that 'someview.frame' returns a CGRect by value, not by reference or pointer or whatever. That's why you get the 'Lvalue required' error.

However, setting the frame like you're doing is plenty fast.


The frame property is read only. What you can do is copying the current frame with

CGRect btFrame = senderB.frame; 
btFrame.origin.x += 10;
senderB.frame = btFrame;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜