在XIB类型中创建了一个UIView子类CustomUIView: 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)
}
问题是视图控制器的视图不遵守XIB中定义的自动调整大小参数。
现在,从理论上讲,可以告诉视图控制器的视图,clip to bounds
但这不是适当的解决方案。我想念什么?
看不到更多细节很难说,但是...
这是一个对我有用的示例(非常接近您显示的布局):
在Storyboard中的布局方式:
结果:
旋转(以显示自动调整大小):
TwoLabelView.swift类:
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的另一种观点:
笔记:
@IBOutlet var contentView: UIView!
编辑2
要确保从xib加载的视图正确调整大小,请执行以下任一操作:
A)将xib中的Layout
属性设置为或contentView
Translates Mask Into Constraints
B)如果Layout
设置为Automatic
,我们可以修改setup()
函数:
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
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句