Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

*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 CGrect 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?

like image 351
Georges Oates Larsen Avatar asked Feb 24 '11 19:02

Georges Oates Larsen


People also ask

What is a button configuration in UIButton?

A button configuration contains all the customization options available with other methods, such as setTitle (_:for:), and can serve as a replacement for those methods. Alternatively, you can use a configuration in combination with these other methods and adopt new button behaviors and appearance without rewriting your customized UIButton code.

How do I use buttons?

When you tap a button, or select a button that has focus, the button performs any actions attached to it. You communicate the purpose of a button using a text label, an image, or both. The appearance of buttons is configurable, so you can tint buttons or format titles to match the design of your app.

What are the different types of UI buttons?

As we learn in the Button Configuration post, Apple come up with four pre-defined UIButton.Configuration, plain, gray, tinted, and filled. From top to bottom: plain (), gray (), tinted (), filled () configurations. It might look limited, but with a combination of background and foreground color, it should suffice for most of the design you have.


2 Answers

I usually do it like this:

CGRect buttonFrame = button.frame;
buttonFrame.origin.y += 10;
button.frame = buttonFrame;
like image 173
Simon Goldeen Avatar answered Sep 28 '22 08:09

Simon Goldeen


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.

like image 22
Colin Avatar answered Sep 28 '22 07:09

Colin