在iOS 8中使用约束以编程方式将按钮居中于视图中。

6

我有一个子视图并添加了一个按钮。我希望将此按钮添加在该视图的中心位置。我使用Autolayout,因此需要通过编程设置此按钮的约束。

我尝试了这个代码,但播放按钮不在中心位置。


我有一个子视图并添加了一个按钮。我希望将此按钮添加在该视图的中心位置。我使用Autolayout,因此需要通过编程设置此按钮的约束。
[self.moviePlayer.view addSubview:self.playbtn];


 self.playbtn.translatesAutoresizingMaskIntoConstraints = NO;

[self.moviePlayer.view addConstraint:[NSLayoutConstraint constraintWithItem:self.playbtn attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual
                                           toItem:self.moviePlayer.view attribute:NSLayoutAttributeCenterX multiplier:0.667 constant:0]];

请帮我纠正它。提前致谢。
2个回答

15
您可以使用NSLayoutAttributeCenterXNSLayoutAttributeCenterY属性来将您的播放按钮居中,如下所示:
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    [self initViews];
    [self initConstraints];
}

-(void)initViews
{
    self.picture = [[UIImageView alloc] init];
    self.picture.image = [UIImage imageNamed:@"bg"];

    self.playButton = [[UIButton alloc] init];
    [self.playButton setImage:[UIImage imageNamed:@"playButton"] forState:UIControlStateNormal];

    [self.view addSubview:self.picture];
    [self.view addSubview:self.playButton];
}

-(void)initConstraints
{
    self.picture.translatesAutoresizingMaskIntoConstraints = NO;
    self.playButton.translatesAutoresizingMaskIntoConstraints = NO;

    id views = @{
                 @"picture": self.picture,
                 @"playButton": self.playButton
                 };

    // picture constraints
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[picture]|" options:0 metrics:nil views:views]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[picture]|" options:0 metrics:nil views:views]];


    // play button constraints

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.playButton attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.playButton attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];
}
你会得到类似这样的东西:

演示截图


0
你可以使用这段代码将任何物件(例如 self.btn)居中于其父视图中:
[self.btn.superview addConstraint:[NSLayoutConstraint
    constraintWithItem:self.btn.superview
    attribute:NSLayoutAttributeCenterX
    relatedBy:NSLayoutRelationEqual
    toItem:self.btn
    attribute:NSLayoutAttributeCenterX
    multiplier:1.0
    constant:0.0]];
[self.btn.superview addConstraint:[NSLayoutConstraint
    constraintWithItem:self.btn.superview
    attribute:NSLayoutAttributeCenterY
    relatedBy:NSLayoutRelationEqual
    toItem:self.btn
    attribute:NSLayoutAttributeCenterY
    multiplier:1.0
    constant:0.0]];

我也在我的博客上写了一篇关于如何对齐任何UIView对象而不使用Storyboards的文章(点击查看)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接