我正在使用XCode和SWIFT编写应用程序。在这个应用程序中,我有3个图像(用作按钮)的二次维度,我希望围绕每个人的圈子寄宿。为了方便起见,我将故事板中的整个场景复制为xml:
<scene sceneID="kBV-hn-1qD">
<objects>
<viewController id="1W2-aL-qa3" customClass="MainMenuViewController" customModule="myApp" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="Ftp-6X-DtC">
<rect key="frame" x="0.0" y="0.0" width="320" height="568"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<stackView opaque="NO" contentMode="scaleToFill" alignment="top" translatesAutoresizingMaskIntoConstraints="NO" id="P4I-ig-c4s">
<rect key="frame" x="0.0" y="20" width="320" height="548"/>
<subviews>
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" alignment="center" spacing="30" translatesAutoresizingMaskIntoConstraints="NO" id="lO5-P0-6OK">
<rect key="frame" x="0.0" y="0.0" width="320" height="548"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="E 220 D Typ 213.205 Europa" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="64K-qR-19j">
<rect key="frame" x="51.5" y="0.0" width="217.5" height="14.5"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<nil key="highlightedColor"/>
</label>
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="auto" translatesAutoresizingMaskIntoConstraints="NO" id="FfZ-8l-klY">
<rect key="frame" x="0.0" y="44.5" width="320" height="180"/>
<constraints>
<constraint firstAttribute="width" secondItem="FfZ-8l-klY" secondAttribute="height" multiplier="1946:613" id="GnI-Cz-le8"/>
</constraints>
</imageView>
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" spacing="10" translatesAutoresizingMaskIntoConstraints="NO" id="pHF-dG-yvg">
<rect key="frame" x="30" y="254.5" width="260" height="181.5"/>
<subviews>
<stackView opaque="NO" contentMode="scaleToFill" distribution="fillEqually" spacing="30" translatesAutoresizingMaskIntoConstraints="NO" id="iYd-gv-2qR">
<rect key="frame" x="0.0" y="0.0" width="260" height="67.5"/>
<subviews>
<imageView userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="imag1" translatesAutoresizingMaskIntoConstraints="NO" id="g75-Nz-ZD1">
<rect key="frame" x="0.0" y="0.0" width="66.5" height="67.5"/>
<gestureRecognizers/>
<constraints>
<constraint firstAttribute="width" secondItem="g75-Nz-ZD1" secondAttribute="height" multiplier="1:1" id="UHL-Uf-KD3"/>
</constraints>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="number" keyPath="layer.BorderWidth">
<integer key="value" value="1"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="boolean" keyPath="userInteractionEnabled" value="YES"/>
</userDefinedRuntimeAttributes>
<connections>
<outletCollection property="gestureRecognizers" destination="6WY-zH-jA4" appends="YES" id="Wx6-9n-dQS"/>
</connections>
</imageView>
<imageView userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="image2" translatesAutoresizingMaskIntoConstraints="NO" id="JFT-pa-c0w">
<rect key="frame" x="96.5" y="0.0" width="67" height="67.5"/>
<gestureRecognizers/>
<constraints>
<constraint firstAttribute="width" secondItem="JFT-pa-c0w" secondAttribute="height" multiplier="1:1" id="hj2-X2-Oi3"/>
</constraints>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="number" keyPath="layer.borderWidth">
<integer key="value" value="1"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
</imageView>
<imageView userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="image3" translatesAutoresizingMaskIntoConstraints="NO" id="4me-av-MpM">
<rect key="frame" x="193.5" y="0.0" width="66.5" height="67.5"/>
<gestureRecognizers/>
<constraints>
<constraint firstAttribute="width" secondItem="4me-av-MpM" secondAttribute="height" multiplier="1:1" id="Jg0-Pp-C4W"/>
</constraints>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="number" keyPath="layer.borderWidth">
<integer key="value" value="1"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
</imageView>
</subviews>
</stackView>
<stackView opaque="NO" contentMode="scaleToFill" distribution="fillEqually" spacing="30" translatesAutoresizingMaskIntoConstraints="NO" id="4a4-Kr-r5O">
<rect key="frame" x="0.0" y="77.5" width="260" height="104"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="title1" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="sNf-g9-jIH">
<rect key="frame" x="0.0" y="0.0" width="66.5" height="104"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<nil key="highlightedColor"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="boolean" keyPath="adjustsFontSizeToFitWidth" value="YES"/>
</userDefinedRuntimeAttributes>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="title2" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Rkt-Jm-Q6O">
<rect key="frame" x="96.5" y="0.0" width="67" height="104"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<nil key="highlightedColor"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="boolean" keyPath="adjustsFontSizeToFitWidth" value="YES"/>
</userDefinedRuntimeAttributes>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="title3" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="usr-XX-ok1">
<rect key="frame" x="193.5" y="0.0" width="66.5" height="104"/>
<fontDescription key="fontDescription" type="system" pointSize="11"/>
<color key="textColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<nil key="highlightedColor"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="number" keyPath="numberOfLines">
<integer key="value" value="2"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="boolean" keyPath="adjustsFontSizeToFitWidth" value="YES"/>
</userDefinedRuntimeAttributes>
</label>
</subviews>
</stackView>
</subviews>
<color key="backgroundColor" red="0.17254901959999999" green="0.1843137255" blue="0.21960784310000001" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</stackView>
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" translatesAutoresizingMaskIntoConstraints="NO" id="ri1-oS-sYY">
<rect key="frame" x="0.0" y="466" width="320" height="82"/>
<subviews>
<stackView opaque="NO" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="CHD-Aa-aFJ">
<rect key="frame" x="0.0" y="0.0" width="320" height="41"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Reifengrößen Tabellen" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="ouQ-uv-S8C">
<rect key="frame" x="0.0" y="0.0" width="279" height="41"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<nil key="highlightedColor"/>
</label>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="DwR-8c-Yma">
<rect key="frame" x="279" y="0.0" width="41" height="41"/>
<color key="backgroundColor" red="0.28627450980392155" green="0.31764705882352939" blue="0.38039215686274508" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstAttribute="width" secondItem="DwR-8c-Yma" secondAttribute="height" multiplier="1:1" id="f7e-0I-cD2"/>
</constraints>
<fontDescription key="fontDescription" type="system" pointSize="34"/>
<inset key="contentEdgeInsets" minX="10" minY="10" maxX="10" maxY="10"/>
<state key="normal" title=">">
<color key="titleColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</state>
</button>
</subviews>
<constraints>
<constraint firstItem="ouQ-uv-S8C" firstAttribute="leading" secondItem="CHD-Aa-aFJ" secondAttribute="leading" constant="10" id="BSc-9F-13o"/>
<constraint firstAttribute="bottom" secondItem="DwR-8c-Yma" secondAttribute="bottom" constant="10" id="WrV-Lg-iPT"/>
<constraint firstItem="DwR-8c-Yma" firstAttribute="top" secondItem="CHD-Aa-aFJ" secondAttribute="top" constant="10" id="but-WD-Pv4"/>
<constraint firstAttribute="trailing" secondItem="DwR-8c-Yma" secondAttribute="trailing" constant="10" id="w54-iR-ImV"/>
</constraints>
</stackView>
<stackView opaque="NO" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="SY4-r7-wpS">
<rect key="frame" x="0.0" y="41" width="320" height="41"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Reifenluftdruck Tabelle" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="ekA-DI-kqQ">
<rect key="frame" x="0.0" y="0.0" width="279" height="41"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<nil key="highlightedColor"/>
</label>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="4xg-3r-8Ul">
<rect key="frame" x="279" y="0.0" width="41" height="41"/>
<color key="backgroundColor" name="BtnColor"/>
<constraints>
<constraint firstAttribute="width" secondItem="4xg-3r-8Ul" secondAttribute="height" multiplier="1:1" id="IGD-cv-Lh9"/>
</constraints>
<fontDescription key="fontDescription" type="system" pointSize="34"/>
<inset key="contentEdgeInsets" minX="10" minY="10" maxX="10" maxY="10"/>
<state key="normal" title=">">
<color key="titleColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</state>
</button>
</subviews>
<constraints>
<constraint firstAttribute="trailing" secondItem="4xg-3r-8Ul" secondAttribute="trailing" constant="10" id="ZW1-dn-roZ"/>
<constraint firstItem="ekA-DI-kqQ" firstAttribute="leading" secondItem="SY4-r7-wpS" secondAttribute="leading" constant="10" id="gho-q7-S78"/>
<constraint firstAttribute="bottom" secondItem="4xg-3r-8Ul" secondAttribute="bottom" constant="10" id="kQS-Uq-qHe"/>
<constraint firstItem="4xg-3r-8Ul" firstAttribute="top" secondItem="SY4-r7-wpS" secondAttribute="top" constant="10" id="yfs-2P-hG2"/>
</constraints>
</stackView>
</subviews>
</stackView>
</subviews>
<constraints>
<constraint firstItem="ri1-oS-sYY" firstAttribute="leading" secondItem="lO5-P0-6OK" secondAttribute="leading" id="1d6-4R-bTC"/>
<constraint firstAttribute="trailing" secondItem="ri1-oS-sYY" secondAttribute="trailing" id="CiI-t0-EkU"/>
<constraint firstAttribute="trailing" secondItem="pHF-dG-yvg" secondAttribute="trailing" constant="30" id="Igw-XA-Cuj"/>
<constraint firstItem="pHF-dG-yvg" firstAttribute="leading" secondItem="lO5-P0-6OK" secondAttribute="leading" constant="30" id="tDP-Mw-tQU"/>
</constraints>
</stackView>
</subviews>
<constraints>
<constraint firstItem="pHF-dG-yvg" firstAttribute="height" relation="greaterThanOrEqual" secondItem="P4I-ig-c4s" secondAttribute="height" multiplier="0.33" id="JPO-Ar-QaF"/>
<constraint firstItem="FfZ-8l-klY" firstAttribute="width" relation="lessThanOrEqual" secondItem="P4I-ig-c4s" secondAttribute="width" id="XME-W1-8qw"/>
<constraint firstItem="FfZ-8l-klY" firstAttribute="height" secondItem="P4I-ig-c4s" secondAttribute="height" multiplier="0.33" id="Xdi-Ev-p9Q"/>
</constraints>
</stackView>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstItem="P4I-ig-c4s" firstAttribute="leading" secondItem="Sdq-aC-hAd" secondAttribute="leading" id="6qA-Lk-0Oj"/>
<constraint firstAttribute="trailing" secondItem="P4I-ig-c4s" secondAttribute="trailing" id="DWf-Dm-Vu8"/>
<constraint firstAttribute="bottom" secondItem="P4I-ig-c4s" secondAttribute="bottom" id="k0s-eZ-rER"/>
<constraint firstItem="P4I-ig-c4s" firstAttribute="top" secondItem="Sdq-aC-hAd" secondAttribute="top" id="qii-j0-uVP"/>
</constraints>
<viewLayoutGuide key="safeArea" id="Sdq-aC-hAd"/>
</view>
<connections>
<outlet property="btnlblContainer" destination="pHF-dG-yvg" id="wXx-7u-yzP"/>
<outlet property="image2" destination="JFT-pa-c0w" id="VCf-iJ-hpD"/>
<outlet property="imageButtonStack" destination="iYd-gv-2qR" id="KIN-ge-Rfe"/>
<outlet property="komplettImage" destination="4me-av-MpM" id="YQp-ba-NFr"/>
<outlet property="komplettLabel" destination="usr-XX-ok1" id="KV0-gT-Abc"/>
<outlet property="listContainer" destination="ri1-oS-sYY" id="Xc6-q1-cwq"/>
<outlet property="mainStack" destination="P4I-ig-c4s" id="vOO-84-DRe"/>
<outlet property="image2" destination="g75-Nz-ZD1" id="wCZ-zV-mtr"/>
<outlet property="titleLabel" destination="64K-qR-19j" id="txL-qN-epK"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="Inm-Jw-5df" userLabel="First Responder" sceneMemberID="firstResponder"/>
<tapGestureRecognizer id="6WY-zH-jA4">
<connections>
<segue destination="glE-1m-7jE" kind="presentation" id="nMN-iJ-VWK"/>
</connections>
</tapGestureRecognizer>
</objects>
<point key="canvasLocation" x="978.75" y="116.19718309859155"/>
</scene>
在我的代码中,我使用它来获得圆形寄宿生:
override func viewDidLayoutSubviews() {
let white = UIColor(white:1, alpha:1)
for v in imageButtonStack.subviews {
v.layer.borderColor = white.cgColor
v.layer.cornerRadius = (v.layer.bounds.size.width/2)
v.layer.masksToBounds = true
}
}
现在,这在加载视图时运行良好,但是,当我旋转屏幕时,我再次得到带有圆边的二次边界(就好像cornerRadius太小了)。当我向后旋转时,它看起来像是角落太大了...... 任何想法?
编辑:
问题似乎是,在更改子视图的大小之前调用viewDidLayoutSubviews()
时。我必须在哪里放置代码?
答案 0 :(得分:1)
设置视图的剪贴蒙版:
.html
答案 1 :(得分:0)
尝试setNeedsLayout
let white = UIColor(white:1, alpha:1)
for v in imageButtonStack.subviews {
v.layer.borderColor = white.cgColor
v.layer.cornerRadius = (v.layer.bounds.size.width/2)
v.layer.masksToBounds = true
}
self.view.setNeedsLayout()
答案 2 :(得分:0)
好吧,我自己得到了它:
override func viewWillTransition(to size: CGSize, with coordinator UIViewControllerTransitionCoordinator) {
viewWillTransition(to: size, with: coordinator)
coordinator.animateAlongsideTransition(in: nil, animation: nil, completion: {
let white = UIColor(white:1, alpha:1)
for v in imageButtonStack.subviews {
v.layer.borderColor = white.cgColor
v.layer.cornerRadius = (v.layer.bounds.size.width/2)
v.layer.masksToBounds = true
}
})
}
因为在启动时没有调用此方法,所以代码仍然应该在viewDidAppear()中。
在短时间内你可以看到错误的&#34;寄宿生,但他们立即调整