Reputation: 1553
I create a button programmatically and point its frame to another button that I have created on storyboard, when orientation changes it doesnt stay in middle as it supposed to be.
I also create a UIImageview
but it stays in the middle when orientation changes
@property (nonatomic, weak) IBOutlet UIImageView *logoImage;
@property (nonatomic, weak) IBOutlet UIButton *loginButton;
@property (strong, nonatomic) UIButton* popUpButton;
UIImage *logOutButtonImage=[UIImage imageNamed:@"Button - Logout.png"];
_popUpButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_popUpButton setBackgroundImage:logOutButtonImage forState:UIControlStateNormal];
_popUpButton.frame=_loginButton.frame;
[_popUpButton addTarget:self action:@selector(didPressLink:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_popUpButton];
Image and button on storyboard, they are in the middle
image's property
button'property
result:
works normal on view didload
no matter what the orientation is:
doesnt stay in the middle when orientation changes
Why button doesn't stay in the middle, how can I achieve that?
Upvotes: 0
Views: 170
Reputation: 21726
Set autoresizing mask
_popUpButton.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
Upvotes: 2