View Controller 不遵守 XIB 自动调整大小掩码
View Controller not respecting XIB autoresizing mask
在 CustomUIView: UIView
类型的 XIB 中创建了 UIView 子类。它包含多个出口,每个出口都使用自动调整大小(不是自动布局)。
这个 XIB 的视图是这样加载的:
// In CustomUIView class
// Initializer used by Interface Builder.
required init?(coder: NSCoder) {
super.init(coder: coder)
initialize()
}
func initialize() {
// Load the view
let contentView = // typical func to load view from NIB. Owner argument is self.
addSubview(contentView)
}
此 CustomUIView 被添加到视图控制器,如下所示:
问题是视图控制器的视图不遵守 XIB 中定义的自动调整参数。
现在,理论上可以将视图控制器的视图告诉 clip to bounds
,但这不是一个合适的解决方案。我错过了什么?
在没有看到更多细节的情况下,很难说,但是...
这是一个适合我的示例(非常接近您展示的布局):
故事板中的布局方式:
结果:
已旋转(以显示自动调整大小):
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
}
}
并且,来源到 TwoLabelView.xib 文件:
<?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>
编辑
IB中xib的另一种观点:
备注:
- 文件的所有者设置为我们的自定义 class
- xib 的视图本身连接到
@IBOutlet var contentView: UIView!
编辑 2
要确保从 xib 加载的视图正确调整大小,可以:
A) 将 xib 中 contentView
的 Layout
属性 设置为 Translates Mask Into Constraints
,或
B) 如果Layout
设置为Automatic
,我们可以修改我们的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
}
感谢@donmag 间接指出了解决方案。
这是@donmag 所没有的作者所没有的。
需要将自动调整大小掩码转换为约束:
单击您的 XIB 视图。
将布局 属性 更改为 "translates masks into constraints"。
在 CustomUIView: UIView
类型的 XIB 中创建了 UIView 子类。它包含多个出口,每个出口都使用自动调整大小(不是自动布局)。
这个 XIB 的视图是这样加载的:
// In CustomUIView class
// Initializer used by Interface Builder.
required init?(coder: NSCoder) {
super.init(coder: coder)
initialize()
}
func initialize() {
// Load the view
let contentView = // typical func to load view from NIB. Owner argument is self.
addSubview(contentView)
}
此 CustomUIView 被添加到视图控制器,如下所示:
问题是视图控制器的视图不遵守 XIB 中定义的自动调整参数。
现在,理论上可以将视图控制器的视图告诉 clip to bounds
,但这不是一个合适的解决方案。我错过了什么?
在没有看到更多细节的情况下,很难说,但是...
这是一个适合我的示例(非常接近您展示的布局):
故事板中的布局方式:
结果:
已旋转(以显示自动调整大小):
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
}
}
并且,来源到 TwoLabelView.xib 文件:
<?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>
编辑
IB中xib的另一种观点:
备注:
- 文件的所有者设置为我们的自定义 class
- xib 的视图本身连接到
@IBOutlet var contentView: UIView!
编辑 2
要确保从 xib 加载的视图正确调整大小,可以:
A) 将 xib 中 contentView
的 Layout
属性 设置为 Translates Mask Into Constraints
,或
B) 如果Layout
设置为Automatic
,我们可以修改我们的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
}
感谢@donmag 间接指出了解决方案。
这是@donmag 所没有的作者所没有的。
需要将自动调整大小掩码转换为约束:
单击您的 XIB 视图。
将布局 属性 更改为 "translates masks into constraints"。