NSLayoutContraint将按钮放在视图的中心

时间:2015-08-05 12:32:50

标签: ios swift autolayout

我正和Constaints一起玩,他们似乎很相似:)

我想知道,在视图的中心(水平和垂直方向)放置按钮的VisualString是什么。

我应该使用

NSLayoutContraint(item: attribute: relatedBy: toItem: attribute: multiplier: constant)

2 个答案:

答案 0 :(得分:0)

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[titleLabel]-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:NSDictionaryOfVariableBindings(titleLabel)]];


[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[titleLabel]-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:NSDictionaryOfVariableBindings(titleLabel)]];

编辑: - 这个会更好用

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[titleLabel]" options:NSLayoutFormatAlignAllCenterX metrics:nil views:NSDictionaryOfVariableBindings(titleLabel)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[titleLabel]" options:NSLayoutFormatAlignAllCenterY metrics:nil views:NSDictionaryOfVariableBindings(titleLabel)]];

答案 1 :(得分:0)

使用Restraint,您只需要这样做:

let imageViewHorizontalConstraint = NSLayoutConstraint(
  item:       imageView,
  attribute:  .CenterX,
  relatedBy:  .Equal,
  toItem:     self,
  attribute:  .CenterX,
  multiplier: 1.0,
  constant:   0
)

let imageViewVerticalConstraint = NSLayoutConstraint(
  item:       imageView,
  attribute:  .CenterY,
  relatedBy:  .Equal,
  toItem:     self,
  attribute:  .CenterY,
  multiplier: 1.0,
  constant:   0
)

addConstraints([imageViewHorizontalConstraint, imageViewVerticalConstraint])

没有视觉格式

 // Center horizontally
        [self addConstraint:[NSLayoutConstraint constraintWithItem:centerView
                                                         attribute:NSLayoutAttributeCenterX
                                                         relatedBy:NSLayoutRelationEqual
                                                            toItem:self
                                                         attribute:NSLayoutAttributeCenterX
                                                        multiplier:1.0
                                                          constant:0.0]];

        [self addConstraint:[NSLayoutConstraint constraintWithItem:centerView
                                                         attribute:NSLayoutAttributeCenterY
                                                         relatedBy:NSLayoutRelationEqual
                                                            toItem:self
                                                         attribute:NSLayoutAttributeCenterY
                                                        multiplier:1.0
                                                          constant:0.0]];

视觉格式

// Variables
NSDictionary *views = @{
                        @"view" : self.view,
                        @"superview" : self.view.superview,
                        };
NSDictionary *metrics = @{
                          @"width" : @(CGRectGetWidth(self.view.frame)),
                          @"height" : @(CGRectGetHeight(self.view.frame))
                          };

// Constraints
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[superview]-(<=1)-[view(width)]"
                                                               options:NSLayoutFormatAlignAllCenterY
                                                               metrics:metrics
                                                                 views:views];
[self.view.superview addConstraints:constraints];

constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[superview]-(<=1)-[view(height)]"
                                                      options:NSLayoutFormatAlignAllCenterX
                                                      metrics:metrics
                                                        views:views];
[self.view.superview addConstraints:constraints];