二元运算符“|”不能应用于两个UIViewAutoResizing操作数

在Swift 2.0中出现此错误。

二元运算符“|”不能应用于两个UIViewAutoResizing操作数

下面是代码:

let view = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 568))
addSubview(view)
view.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight

你知道问题出在哪里吗? enter image description here

47692 次浏览

OptionSetType获得了SWIFT 2.X的更新语法和SWIFT 3.X的另一个更新

Swift 3.X

view.autoresizingMask = [.flexibleWidth, .flexibleHeight]

Swift 2.X

view.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]

尝试使用Xcode7-B6:

view.autoresizingMask = UIViewAutoresizing.FlexibleWidth.union(UIViewAutoresizing.FlexibleHeight)

以下是Swift 1.2和2之间的区别:

// swift 1.2
view.autoresizingMask = .FlexibleWidth | .FlexibleTopMargin


// swift 2
view.autoresizingMask = [.FlexibleWidth, .FlexibleTopMargin]

在Xcode 7.2中使用此代码Swift 2

self.view.autoresizingMask = [.FlexibleRightMargin, .FlexibleLeftMargin, .FlexibleBottomMargin, .FlexibleTopMargin]

对于Swift 3Xcode 8 b1

view.autoresizingMask = [.flexibleWidth, .flexibleHeight]

SWIFT 3.0.2的实际值:

view.autoresizingMask = [.layerWidthSizable, .layerHeightSizable]