Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/116.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何将`NSLayoutYAxisAnchor`用作变量?_Ios_Swift_Nslayoutconstraint_Nslayoutanchor - Fatal编程技术网

Ios 如何将`NSLayoutYAxisAnchor`用作变量?

Ios 如何将`NSLayoutYAxisAnchor`用作变量?,ios,swift,nslayoutconstraint,nslayoutanchor,Ios,Swift,Nslayoutconstraint,Nslayoutanchor,如何定义NSLayoutYAxisAnchor类型的变量 以下内容不起作用,但希望能说明我的意思: let anAnchor: NSLayoutYAxisAnchor = .topAnchor NSLayoutConstraints.activate([ viewA.anAnchor.constraint(equalTo: viewB.anAnchor) ]) 您可以在Swift 4中将其用作关键路径,如下所示: let anAnchor = \UIView.topAnchor .t

如何定义
NSLayoutYAxisAnchor
类型的变量

以下内容不起作用,但希望能说明我的意思:

let anAnchor: NSLayoutYAxisAnchor = .topAnchor

NSLayoutConstraints.activate([
   viewA.anAnchor.constraint(equalTo: viewB.anAnchor)
])

您可以在Swift 4中将其用作关键路径,如下所示:

let anAnchor = \UIView.topAnchor
.topAnchor
不是枚举类型,它是
UIView
的属性。

因此,在Swift 4之前不可能将其设置为变量。

您可以在Swift 4中将其用作键路径,如下所示:

let anAnchor = \UIView.topAnchor
.topAnchor
不是枚举类型,它是
UIView
的属性。

因此,在Swift 4之前不可能将其设置为变量。

您可以使用Swift 4的新关键路径功能:

let anAnchor = \UIView.topAnchor
let v1 = UIView(); let v2 = UIView() // just for testing purposes
let constraint = v1[keyPath:anAnchor].constraint(equalTo:v2[keyPath:anAnchor])
// now you can activate the constraint, etc.

您可以使用Swift 4的新关键路径功能执行此操作:

let anAnchor = \UIView.topAnchor
let v1 = UIView(); let v2 = UIView() // just for testing purposes
let constraint = v1[keyPath:anAnchor].constraint(equalTo:v2[keyPath:anAnchor])
// now you can activate the constraint, etc.

是的,但是如何将其定义为
.topanch
NSLayoutYAxisAnchor
似乎是y轴上任何锚点的常见类型。是的,但如何将其定义为
。topAnchor
NSLayoutYAxisAnchor
似乎是y轴上任何锚点的常见类型。使用Swift 4键路径如何?这就是如何将属性引用转换为变量的方法。使用Swift 4键路径怎么样?这就是将属性引用转换为变量的方法