将NSConstraints从Objective-C转换为Swift

时间:2015-04-08 20:10:36

标签: objective-c swift constraints nslayoutconstraint

我希望将基于Objective-c的通用应用程序转换为Swift项目。到目前为止,我已经能够交换大部分代码,但我在翻译以下内容时遇到了问题:

NSLayoutConstraint *leftConstraint = [NSLayoutConstraint constraintWithItem:self.contentView
                                                                  attribute:NSLayoutAttributeLeading
                                                                  relatedBy:0
                                                                     toItem:self.view
                                                                  attribute:NSLayoutAttributeLeft
                                                                 multiplier:1.0
                                                                   constant:0];
[self.view addConstraint:leftConstraint];

NSLayoutConstraint *rightConstraint = [NSLayoutConstraint constraintWithItem:self.contentView
                                                                   attribute:NSLayoutAttributeTrailing
                                                                   relatedBy:0
                                                                      toItem:self.view
                                                                   attribute:NSLayoutAttributeRight
                                                                  multiplier:1.0
                                                                    constant:0];
[self.view addConstraint:rightConstraint];

我还必须更改此代码:

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    return NO;
}

在Swift中是否有类似于此的方法?

非常感谢任何帮助。在此先感谢所有回复的人。 :)

2 个答案:

答案 0 :(得分:6)

 let leftConstranint = NSLayoutConstraint(item: self.contentView, attribute: .Leading, relatedBy: .Equal, toItem: self.view, attribute: .Left, multiplier: 1.0, constant: 0)

 self.view.addConstraint(leftConstranint)

 let rightConstraint = NSLayoutConstraint(item: self.contentView, attribute: .Trailing, relatedBy: .Equal, toItem: self.view, attribute: .Right, multiplier: 1.0, constant: 0)

 self.view.addConstraint(rightConstraint)

.Leading.Left.Trailing.Right是来自NSLayoutAttribute的枚举。 还有来自UIGestureRecognizerDelegate的函数:

func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
      return false
}

使用Cartography,您可以执行以下的Autolayout:

layout(self.contentView, self.view) { (contentView, view) -> () in
        contentView.leading == view.left
        contentView.trailing == view.right
 }

使用Snap,您可以执行以下的Autolayout:

 self.contentView.snp_makeConstraints { maker in
        maker.leading.equalTo(self.view.snp_left)
        maker.trailing.equalTo(self.view.snp_right)
 }

Cartography更像是一种数学方式,可以在代码中制定约束。换句话说,Snap更像是一种英语方式来描述您在单词中的约束。

与Visual Language Format(用纯字符串编码约束)不同,CartographySnap都可以利用XCode中的代码完成。

答案 1 :(得分:1)

使用AutoLayout Visual Format Language通常被认为是可取的:

var viewsDictionary = [ "contentView" : self.contentView, 
                        "view"        : self.view ]

var viewsConstraint = 
       NSLayoutConstraint.constraintsWithViaualFormat(
                     "[contentView][view]", 
                      options: 0, 
                      metrics: nil, 
                        views: viewsDictionary)
view.addConstraint(viewsConstraint)

但你也可以使用另一种形式:

var leftConstraint = 
      NSLayoutConstraint(item: contentView,
                    attribute: .Leading, 
                    relatedBy: .Equal, 
                       toItem: view, 
                    attribute: .Left, 
                   multiplier: 1.0,
                     constant: 0)
view.addConstraint(leftConstraint)

var rightConstraint = 
      NSLayoutConstraint(item: contentView, 
                    attribute: .Trailing,
                    relatedBy: .Equal, 
                       toItem: view, 
                    attribute: .Right, 
                   multiplier: 1.0, 
                     constant: 0)
view.addConstraint(rightConstraint)

func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
  return false
}