195

Getting this error in Swift 2.0.

Binary operator '|' cannot be applied to two UIViewAutoresizing operands

Here is the code:

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

Any idea what can be the problem? enter image description here

Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
Khawar
  • 9,151
  • 9
  • 46
  • 67
  • 2
    Effectively the same problem as in [Swift 2.0 - Binary Operator “|” cannot be applied to two UIUserNotificationType operands](http://stackoverflow.com/questions/30761996/swift-2-0-binary-operator-cannot-be-applied-to-two-uiusernotificationtype). – Martin R Jun 16 '15 at 12:27

6 Answers6

465

The OptionSetType got an updated syntax for Swift 2.x and another update for Swift 3.x

Swift 3.x

view.autoresizingMask = [.flexibleWidth, .flexibleHeight]

Swift 2.x

view.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
keithbhunter
  • 12,258
  • 4
  • 33
  • 58
29

This are the differences between Swift 1.2 and 2:

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

// swift 2
view.autoresizingMask = [.FlexibleWidth, .FlexibleTopMargin]
Jorge Casariego
  • 21,948
  • 6
  • 90
  • 97
6

Try with xcode7-b6:

view.autoresizingMask = UIViewAutoresizing.FlexibleWidth.union(UIViewAutoresizing.FlexibleHeight)
Tai Le
  • 8,530
  • 5
  • 41
  • 34
5

For Swift 3 Xcode 8 b1:

view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
Pushpa Y
  • 1,010
  • 1
  • 12
  • 20
ICL1901
  • 7,632
  • 14
  • 90
  • 138
  • @Pushpa: curious, other than adding the word "For" at the front of my answer, did you edit/change something else that I cannot see? Thanks – ICL1901 Jun 17 '16 at 07:26
  • 2
    Yes, putted the code inside code block :) And yes this answer is already posted with Swift 2. – Pushpa Y Jun 17 '16 at 08:36
1

actual for swift 3.0.2:

view.autoresizingMask = [.layerWidthSizable, .layerHeightSizable]
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
-1

use this code swift 2 with Xcode 7.2

self.view.autoresizingMask = [.FlexibleRightMargin, .FlexibleLeftMargin, .FlexibleBottomMargin, .FlexibleTopMargin]
Maninderjit Singh
  • 1,419
  • 1
  • 13
  • 23