二进制运算符“ |” 不能应用于两个UIViewAutoresizing操作数


193

在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

知道可能是什么问题吗? 在此处输入图片说明


Answers:


457

OptionSetType得到了斯威夫特2.X更新的语法和斯威夫特3.x的另一个更新

斯威夫特3.x

view.autoresizingMask = [.flexibleWidth, .flexibleHeight]

斯威夫特2.x

view.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]

30

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

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

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

6

尝试使用xcode7-b6:

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

5

对于Swift 3 Xcode 8 b1

view.autoresizingMask = [.flexibleWidth, .flexibleHeight]

@Pushpa:很好奇,除了在答案的开头添加“ For”一词外,您是否还编辑/更改了我看不见的其他内容?谢谢
David DelMonte

2
是的,推杆里面的代码代码块:)是的这个答案已经贴有斯威夫特2
普什帕Ÿ


-1

在Xcode 7.2中使用此代码swift 2

self.view.autoresizingMask = [.FlexibleRightMargin, .FlexibleLeftMargin, .FlexibleBottomMargin, .FlexibleTopMargin]
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.