Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIView won't move programmatically

so there must be something simple I am missing because I just can't figure out how to move my UIView

This is what I have so far

my .m file

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self buttonLayout];
}

-(void)buttonLayout
{
     UIView *theView = [self buttonGroup];
     theView.center=CGPointMake(50, 50);
}

My .h file

@property (nonatomic, retain) IBOutlet UIView *buttonGroup;

So this is what I have so far and i just cant seem to get it to move at all P.S i don't want it animated as i'm getting my layout to move to compensate for different iPhone screen sizes :)

like image 564
jLynx Avatar asked Feb 03 '26 20:02

jLynx


2 Answers

I have not tested this, but it seems like you need to:

A) Remove the view from its parent.

B) Adjust the center of its 'frame', not the view.

C) Re-add the subview.

Like so:

UIView *theView = [self buttonGroup];
CGRect theFrame = theView.frame;

[theView removeFromSuperview];

theFrame.origin.x = 50.0;
theFrame.origin.y = 50.0;

theView.frame = theFrame;

[self.view addSubview:theView];

This should work with autolayout.

like image 76
Josiah Avatar answered Feb 05 '26 12:02

Josiah


As the other poster says, you can't move views around by changing their center or frame when you have auto-layout in effect.

Instead, you have to add a vertical and horizontal constraint that positions the view from the top/left edge, connect an outlet to the constraint, and then change the constant value on the constraint from code.

like image 44
Duncan C Avatar answered Feb 05 '26 12:02

Duncan C



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!