Mord Fustang
Mord Fustang

Reputation: 1553

Button orientation issue

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

enter image description here

image's property

enter image description here

button'property

enter image description here

result:

works normal on view didload no matter what the orientation is:

enter image description here

doesnt stay in the middle when orientation changes

enter image description here

Why button doesn't stay in the middle, how can I achieve that?

Upvotes: 0

Views: 170

Answers (1)

B.S.
B.S.

Reputation: 21726

Set autoresizing mask

_popUpButton.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;

Upvotes: 2

Related Questions