UIButton Changing Position
Asked Answered
A

6

8

I have a button set up in IB. I have an IBOutlet set up and the onscreen object linked to it. Is there a way to programmatically change that buttons position and/or size? I know you can change the title and some things but I don't see how to change it's position or size.

enter image description here

Now I would like to change the position of it accordingly. Is it possible? If yes, please let me know how, since I am trying to change the position of my button in the following code, but it does not work in the header file.

@property (nonatomic, strong) IBOutlet UIButton *mybuttonOutlet;

In the implementation file:

-(void)viewDidLoad {


    screenSizeHeight=[UIScreen mainScreen].bounds.size.height;


if(screenSizeHeight==568)

    mybuttonOutlet.frame= CGRect(38, 464 ,157,25);


if(screenSizeHeight==480)

    mybuttonOutlet.frame= CGRect(38, 364 ,157,25);

}
Antheridium answered 6/5, 2013 at 19:16 Comment(5)
did you connect this myButtonOutlet with your .xib ?Slice
I don't find any reason why this won't work unless you forgot to connect the IBOutlet.Megalocardia
yes, I connected my button to my .xibAntheridium
Just a wild guess, did you connect multiple outlets to the same button?Megalocardia
No just only one outlet to my button.Antheridium
F
14

Remove Use Autolayout from the button in IB or storyboard.

Fraud answered 6/5, 2013 at 19:57 Comment(1)
I believe you can only remove autolayout from the entire xib/SB itself - which worked for me too.Janus
B
5

If you want to adjust positions with Autolayout enabled, you will have to change your code like this

-(void)viewDidLayoutSubviews {

    screenSizeHeight=[UIScreen mainScreen].bounds.size.height;

    if(screenSizeHeight==568)
        mybuttonOutlet.frame= CGRect(38, 464 ,157,25);

    if(screenSizeHeight==480)
        mybuttonOutlet.frame= CGRect(38, 364 ,157,25);
}

Basically you need to perform any custom layout adjustments in viewDidLayoutSubviews method if Autolayout is enabled

Brachylogy answered 15/11, 2014 at 12:38 Comment(0)
I
0

Please perform this check.

-(void)viewDidLoad{
    if(self.mybuttonOutlet==nil)NSLog(@"Button is NIL");
}

Now if you get the log "Button is NIL", then probably you forgot to link your IB button to your variable mybuttonOutlet.

Incessant answered 15/11, 2014 at 14:40 Comment(0)
O
0
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end


#import "ViewController.h"

@interface ViewController ()
@property(nonatomic, strong) IBOutlet UIButton *theButton;


// -(IBAction)moveTheButton:(id)sender;

@end

@implementation ViewController
@synthesize theButton;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(IBAction)moveTheButton:(id)sender{
    //set default position
    CGRect btFrame = theButton.frame;
    btFrame.origin.x = 145;
    btFrame.origin.y = 285;
    theButton.frame = btFrame;

    //position changing
    btFrame.origin.x += 40;
    theButton.frame = btFrame;

    //new size of button
    CGRect rect=CGRectMake(145, 285, 190, 30);
    [self.theButton setBounds:rect];

}

@end
Opinicus answered 10/6, 2015 at 5:52 Comment(0)
B
0

I eventually went for the constraints. Get an outlet of the constraints that determine the position of the button (top, bottom, leading, trailing) and change the constraint(s) value(s).

self.constBtnSubmitTrailing.constraint = 50.0

This so you can keep AutoLayout enabled.

Blate answered 17/7, 2015 at 12:31 Comment(0)
D
0

The solution I figured out for this is to create new View object inside the main View, and put my "dynamically changing objects" inside that View (as shown in picture).

Object hierarchy

Then I use Auto-Layout to position the new View in the main View. But bugButton:UIButton which is inside the new View, changes position as expected with:

bugButton.frame.origin = CGPoint(x: newX, y: newY)
Dochandorrach answered 29/1, 2016 at 6:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.