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

时间:2014-10-18 17:53:03

标签: ios objective-c ios8 nslayoutconstraint iphone-6

我有一个子视图并添加了1个按钮。我想在该视图的中心添加此按钮。我使用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 个答案:

答案 0 :(得分:14)

您可以使用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]];
}

你得到这样的东西:

demo screenshot

答案 1 :(得分:0)

您可以使用此代码将任何项目(例如 self.btn )置于其 superview 中心:

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

我还写了an article about aligning any UIView objects,而没有在我的博客上使用 Storyboards