按钮方向问题

时间:2013-02-20 19:32:59

标签: ios objective-c uibutton orientation

我以编程方式创建一个按钮并将其框架指向我在故事板上创建的另一个按钮,当方向发生变化时,它不会保持在中间位置。

我还创建了一个UIImageview,但是当方向发生变化时它会保持在中间位置

@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];

故事板上的图像和按钮,它们位于中间

enter image description here

图片的属性

enter image description here

button'property

enter image description here

结果:

无论方向如何,

在视图didload上都能正常工作:

enter image description here

当方向改变时,

不会停留在中间

enter image description here

为什么按钮不会停留在中间,我该如何实现?

1 个答案:

答案 0 :(得分:2)

设置autoresizing mask

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