二元运算符' |'无法应用于两个UIViewAutoresizing操作数

时间:2015-06-16 12:15:42

标签: ios swift cocoa-touch swift2

在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

6 个答案:

答案 0 :(得分:439)

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

Swift 3.x

view.autoresizingMask = [.flexibleWidth, .flexibleHeight]

Swift 2.x

view.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]

答案 1 :(得分:30)

这是Swift 1.2和2之间的区别:

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

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

答案 2 :(得分:6)

尝试使用xcode7-b6:

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

答案 3 :(得分:5)

Swift 3 Xcode 8 b1

view.autoresizingMask = [.flexibleWidth, .flexibleHeight]

答案 4 :(得分:1)

实际上是swift 3.0.2:

view.autoresizingMask = [.layerWidthSizable, .layerHeightSizable]

答案 5 :(得分:-1)

将此代码swift 2与Xcode 7.2一起使用

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