Vote count:
0
When loading a UIView from a nib file, the view usually has translatesAutoresizingMastIntoConstraints set to YES.
As a consequence, you cannot add top level constraints to the view (e.g. width and height).
In the past I have been able to generate a top level view which does allow me to create top level constraints, and sets translatesAutoresizingMastIntoConstraints to NO.
How can I get this behavior when loading a UIView from a nib without subclassing it?
asked 24 secs ago
1 Answer
Vote count:
0
This seems to be an undocumented Xcode feature (tested on Xcode 6).
I will use the following terms:
- Static view: the default views which do not allow top level constraints, and have
translatesAutoresizingMaskIntoConstraintsset to YES. - Constrainable view: a view which allows you to create top level constraints, and has
translatesAutoresizingMaskIntoConstraintsset to NO.
First let's take a look at some of their differences...
Static view
.xib contents:
<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>
More info:
<UIView: 0x7fec19ccb1b0; frame = (0 0; 320 439); autoresize = W+H; layer = <CALayer: 0x7fec19ccae80>>- translatesAutoresizingMaskIntoConstraints: YES
- Cannot add top level constraints in Interface Builder.
Constrainable view
.xib contents:
<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>
More info:
<UIView: 0x7f9503eb2ba0; frame = (0 0; 320 439); autoresize = RM+BM; layer = <CALayer: 0x7f9503e9dc80>>- translatesAutoresizingMaskIntoConstraints: NO
- Can add top level constraints in Interface Builder.
Subtle differences:
- The static one has an
<autoresizingMask ...element. - Dragging in a UIView from the Object Library produces a Static view.
- Drag a UIView into another to create a subview. (If you don't have anything in a nib, you need to temporarily drag a view in there, and delete it at the end of this process).
- Add a constraint to that view which the view will own (e.g. create a width constraint).
- Drag the view out of its superview and drop it on the white part of the Interface Builder document.
- It should now not have a superview and you should be able to create top level constraints on the view.
answered 24 secs ago
How to disable translatesAutoresizingMaskIntoConstraints in Interface Builder .xib file
Aucun commentaire:
Enregistrer un commentaire