Autoresizing Masks Programmatically VS Interface Builder/Xib/Nib

Autoresizing masks programmatically vs Interface Builder / xib / nib

Yes, Interface Builder has it "reversed" in a sense (or UIView, depending on how you look at it). Your cited "scenarios" are correct.

View Controller not respecting XIB autoresizing mask

It's a little tough to tell, without seeing more details, but...

Here is an example that's working for me (pretty close to the layout you show):

Sample Image
Sample Image
Sample Image

How it's laid-out in Storyboard:

Sample Image

Result:

Sample Image

Rotated (to show the auto-resizing):

Sample Image


TwoLabelView.swift class:

class TwoLabelView: UIView {

@IBOutlet var contentView: UIView!
@IBOutlet var topLabel: UILabel!
@IBOutlet var botLabel: UILabel!

override init(frame: CGRect) {
super.init(frame: frame)
setup()
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}

func setup() {
let nib = UINib(nibName: "TwoLabelView", bundle: nil)
nib.instantiate(withOwner: self, options: nil)
addSubview(contentView)

contentView.frame = self.bounds

}
}

and, source to TwoLabelView.xib file:

<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="15505" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
<device id="retina4_7" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="15510"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="TwoLabelView" customModule="scratchy" customModuleProvider="target">
<connections>
<outlet property="botLabel" destination="AKB-d8-jMk" id="QpL-mN-x4M"/>
<outlet property="contentView" destination="iN0-l3-epB" id="klb-G5-hj6"/>
<outlet property="topLabel" destination="btE-jS-Ur5" id="LkK-0w-J4z"/>
</connections>
</placeholder>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
<view contentMode="scaleToFill" id="iN0-l3-epB">
<rect key="frame" x="0.0" y="0.0" width="233" height="71"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="btE-jS-Ur5">
<rect key="frame" x="16" y="8" width="197" height="21"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES" flexibleMaxY="YES"/>
<color key="backgroundColor" red="0.79527050256729126" green="0.96349185705184937" blue="0.73112398386001587" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="AKB-d8-jMk">
<rect key="frame" x="16" y="37" width="197" height="21"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMinY="YES" heightSizable="YES"/>
<color key="backgroundColor" red="0.0" green="0.47790580987930298" blue="0.99864691495895386" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<color key="backgroundColor" red="0.68716365098953247" green="0.31972628831863403" blue="0.86903256177902222" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>
<viewLayoutGuide key="safeArea" id="vUN-kp-3ea"/>
<point key="canvasLocation" x="282.39999999999998" y="-218.1409295352324"/>
</view>
</objects>
</document>

Edit

Another view of the xib in IB:

Sample Image

Notes:

  • File's Owner is set to our custom class
  • The xib's view itself is connected to @IBOutlet var contentView: UIView!

Edit 2

To make sure the view loaded from the xib resizes correctly, either:

A) Set the Layout property for contentView in the xib to Translates Mask Into Constraints, or

B) If Layout is set to Automatic, we can modify our setup() func:

func setup() {
let nib = UINib(nibName: "TwoLabelView", bundle: nil)
nib.instantiate(withOwner: self, options: nil)
addSubview(contentView)

// make sure we're using the right sizing method
if contentView.translatesAutoresizingMaskIntoConstraints == false {
contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
}

contentView.frame = self.bounds

}

Migrating a project using autoresizing masks for iPhone X

A third option would be to use autolayout where you need to and leave the rest of the app with autoresizing. Since XCode 8, you may mix autoresizing and autolayout. For each view in the xib or storyboard, you may choose to set an autoresizing mask or autolayout constraints. Using one kind of rules on a view disables the other kind for that view. But you may use the other kind on another view. This link has some more information : http://www.thomashanning.com/xcode-8-mixing-auto-autoresizing-masks/

If you choose to keep using only autoresizing masks, the helper methods below may help you to layout your views correctly.

statusBarHeight gives you the height of the status bar for the device. safeAreaBottomMargin gives you the bottom margin left for iPhoneX's home button indicator.

- (CGFloat) statusBarHeight {
return UIApplication.sharedApplication.statusBarFrame.size.height;
}

- (CGFloat) safeAreaBottomMargin {
if (@available(iOS 11.0, *)) {
UIWindow *window = UIApplication.sharedApplication.windows.firstObject;
return window.safeAreaLayoutGuide.owningView.frame.size.height - window.safeAreaLayoutGuide.layoutFrame.size.height - window.safeAreaLayoutGuide.layoutFrame.origin.y;
} else {
return 0;
}
}

Autoresizing Not Working Programmatically

Little mistake from my side.
My code is correct.

The issue is related to xCode 6.1, in launch screen we have xib file from xCode 6 so i have to remove reference of it from General Setting-> App Icon and Launch Images and use Assert Catalog instead of launchscreen.xib and it start working....

Anyway thanks for reply to my question.



Related Topics



Leave a reply



Submit