繁体   English   中英

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

[英]Centre the button in view use constraints programmatically in iOS 8

我有一个子视图,并添加了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]];

请帮我纠正一下。 提前致谢。

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

你得到这样的东西:

演示截图

您可以使用此代码将任何项目( 例如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]];

我还写了一篇关于在我的博客上不使用Storyboard对齐任何UIView对象的文章。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM