0

When working with Interface Builder (no matter what kind of document, Storyboard, View Controller, View, etc.), there is a checkbox in the File Inspector to Use Auto Layout.

enter image description here

Checking the source of an empty document, we see that it translated to a flag called useAutolayout:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="6211" systemVersion="14A298i" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES">
    <dependencies>
        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6204"/>
    </dependencies>
    <objects>
        <placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
        <placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
    </objects>
</document>

What is the equivalent when working without Interface Builder and purely building a UI in code?

Note: Please note that I posted a related question here. However, this new question targets the specifics of the technical details of the useAutolayout property whereas the former looks at the problem from a higher level which is why I found it appropriate to post a new question instead of updating the other.

Community
  • 1
  • 1
nburk
  • 22,409
  • 18
  • 87
  • 132

1 Answers1

1

There is no equivalent as such, because the interface builder XIB file is a specification which says how to create a NIB file and that is an archived set of UI objects. The UI objects, if they're configured to use constraints and actually have at least one constraint specified, use translatesAutoresizingMaskIntoConstraints to decide what to do at runtime. The interface builder flag effectively runs some processing to update the archived objects to set or unset that flag.

So, in code, if you're adding your own constraints you need to also run

view.translatesAutoresizingMaskIntoConstraints = NO;

to disable the old autoresizing mask from messing everything up...

Wain
  • 118,658
  • 15
  • 128
  • 151