将 NSConstraints 从 Objective-C 翻译成 Swift
Translating NSConstraints From Objective-C to Swift
我希望将基于 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有类似的方法吗?
如有任何帮助,我们将不胜感激。提前感谢所有回复的人。 :)
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
更像是一种 英语语言方式 来描述您的限制条件。
与可视化语言格式(以纯字符串编码您的约束)不同,Cartography
和 Snap
都可以利用 XCode.
中的代码完成功能
通常认为使用 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
}
我希望将基于 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有类似的方法吗?
如有任何帮助,我们将不胜感激。提前感谢所有回复的人。 :)
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
更像是一种 英语语言方式 来描述您的限制条件。
与可视化语言格式(以纯字符串编码您的约束)不同,Cartography
和 Snap
都可以利用 XCode.
通常认为使用 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
}