How to add top-level restrictions and disable translatesAutoresizingMaskIntoConstraints in the interface of the Builder.xib file

When loading a UIView from a nib file, the view is usually translatesAutoresizingMastIntoConstraintsset to YES.

As a result, you cannot add top-level constraints to the view (for example, width and height).

In the past, I was able to create a top-level view that allows me to create top-level constraints and sets translatesAutoresizingMastIntoConstraintsto NO.

How can I get this behavior when loading a UIView from nib without subclassing it?

+4
source share
1 answer

: , , . - , , . :

let view = UINib(nibName: "ABCView", bundle: nil).instantiateWithOwner(nil, options: nil)[0] as! UIView
view.translatesAutoresizingMaskIntoConstraints = false

, intrinsicContentSize / nib, "", , , , (.. ) / .


Xcode ( Xcode 6 Xcode 7.1).

:

  • : , , translatesAutoresizingMaskIntoConstraints YES.
  • : , translatesAutoresizingMaskIntoConstraints NO.

...

.xib :

<view contentMode="scaleToFill" id="bU6-qJ-x7d" userLabel="STATIC">
    <rect key="frame" x="0.0" y="0.0" width="320" height="439"/>
    <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
    <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
    <nil key="simulatedStatusBarMetrics"/>
    <freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>
    <point key="canvasLocation" x="687" y="37.5"/>
</view>

:

  • <UIView: 0x7fec19ccb1b0; frame = (0 0; 320 439); autoresize = W+H; layer = <CALayer: 0x7fec19ccae80>>
  • translatesAutoresizingMaskIntoConstraints:
  • Interface Builder.

.xib :

<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="0G6-nE-8IZ" userLabel="CONSTRAINABLE">
    <rect key="frame" x="0.0" y="0.0" width="320" height="439"/>
    <color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
    <nil key="simulatedStatusBarMetrics"/>
    <freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>
    <point key="canvasLocation" x="687" y="-455.5"/>
</view>

:

  • <UIView: 0x7f9503eb2ba0; frame = (0 0; 320 439); autoresize = RM+BM; layer = <CALayer: 0x7f9503e9dc80>>
  • translatesAutoresizingMaskIntoConstraints:
  • Interface Builder.

:

  • <autoresizingMask ....
  • UIView .
  • UIView Constrainable view ( - ).

:

  • UIView , subview. ( , ).
  • , (, ).
  • Interface Builder.
  • , .

:

  • , (- .)
  • (, ).
  • .
  • Freeform .

:

  • , (- .)
  • (, ).
  • .
  • Freeform .
+9

All Articles