iOS
UIViewAutoresizing
Swift
Xcode
programming error

Binary operator '' cannot be applied to two UIViewAutoresizing operands

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

This error appears when Swift does not accept the old-style bitwise-OR syntax you are using with autoresizing masks. In modern Swift, UIViewAutoresizing evolved into an OptionSet-style API, so the preferred way to combine values is with array-style literal syntax such as [.flexibleWidth, .flexibleHeight] rather than manually applying |.

The old Objective-C style does not translate directly

Older code often looked like this in Objective-C:

objc
view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

A direct translation into Swift used to cause confusion because the Swift type system became stricter about how these mask values are combined.

Use the OptionSet form in modern Swift

The modern Swift way is:

swift
view.autoresizingMask = [.flexibleWidth, .flexibleHeight]

This is clearer and matches how Swift expresses sets of bitmask-style options across many Apple APIs.

That is usually the correct fix for the compiler error.

Why the error happens

The core reason is that Swift does not always treat the values as plain integers you can freely combine with |. Instead, it models them as a typed set of options.

So this:

swift
view.autoresizingMask = .flexibleWidth | .flexibleHeight

may fail depending on the Swift version and API surface you are working with, while the OptionSet syntax succeeds because it is the intended representation.

If you really need raw-value bit operations

In lower-level or legacy situations, you may still see raw-value combination:

swift
1let mask = UIView.AutoresizingMask(rawValue:
2    UIView.AutoresizingMask.flexibleWidth.rawValue |
3    UIView.AutoresizingMask.flexibleHeight.rawValue
4)
5view.autoresizingMask = mask

That works because you are explicitly operating on the underlying integer representation. It is valid, but it is more verbose than the normal Swift style and usually unnecessary.

Remember that autoresizing masks and Auto Layout are different systems

Another practical point: autoresizingMask belongs to the older springs-and-struts layout system. If you are using Auto Layout, then:

  • set constraints instead
  • and usually disable autoresizing-mask translation when appropriate

For example:

swift
view.translatesAutoresizingMaskIntoConstraints = false

That line matters in Auto Layout code because autoresizing masks can otherwise be converted into constraints automatically.

Prefer clarity over raw bitmask habits

Many Swift migration errors come from bringing old C-style or Objective-C-style bitmask habits into a newer typed API. The language is nudging you toward a more expressive form:

  • use named options
  • use OptionSet syntax
  • let the type system stay explicit

That is why the array-like bracket syntax is the cleanest answer in most Swift codebases.

Check the actual type name in newer SDKs

Depending on the SDK version and imported symbols, you may see UIView.AutoresizingMask instead of the older UIViewAutoresizing spelling. The fix is the same, but the newer name makes the OptionSet nature more obvious.

Common Pitfalls

  • Translating old Objective-C bitmask code directly into Swift without using OptionSet syntax.
  • Reaching for raw-value bit operations when [.option1, .option2] is enough.
  • Forgetting that autoresizing masks are not the same thing as Auto Layout constraints.
  • Mixing translatesAutoresizingMaskIntoConstraints behavior with manual constraint code.
  • Assuming the compiler error means the API cannot combine multiple mask values at all.

Summary

  • In modern Swift, combine autoresizing options with OptionSet syntax such as [.flexibleWidth, .flexibleHeight].
  • The old | style often fails because Swift models the mask more strictly than old C-style code did.
  • Raw-value bitmask operations still exist, but they are usually not the best everyday syntax.
  • 'autoresizingMask belongs to the older layout system, not Auto Layout.'
  • The cleanest fix is usually to adopt the typed Swift option-set style.

Course illustration
Course illustration

All Rights Reserved.