我正在用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="&gt;">
                                                                    <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="&gt;">
                                                                    <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
    }
}

现在,当视图被加载时,这个工作得很好,但是,当我旋转屏幕时,我得到了带有圆角的二次边界(好像拐角半径太小)。当我把它转回来的时候,它的转弯半径看起来太大了。。。
有什么想法吗?
编辑:
问题似乎是,在更改子视图的大小之前调用viewDidLayoutSubviews()时。我应该把代码放在哪里?

最佳答案

设置视图的剪裁遮罩:

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
        v.clipsToBounds = true  // <--
    }
}

关于ios - 圆形寄宿生在轮换后不圆,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48275946/

10-13 04:04