为什么要让Interface Builder不断更改我的Storyboard?

时间:2019-02-11 14:28:59

标签: interface-builder

在XCode 10.1中,Interface Builder不断弄乱我的情节提要。乍一看似乎是随机的,更改了精心放置的UI元素的大小和/或位置。我可以改正。然后,当我对情节提要中的另一个元素进行不相关的更改时,它将再次还原。当我纠正1个UI元素的1个宽度时,当我查看xml代码中的更改时,我注意到数十个其他UI元素的width和or position属性再次被弄乱了。即使我选择提交所有这些更改,让Interface Builder顺其自然,下一次我编辑某些内容时,也会发生一批新的看似随机的更改。

将行为归结为核心,我注意到以下几点:

  • 似乎所有与具有widthSizable =“ YES”和heightSizable =“ YES”的视图都放置在具有这些属性的视图中有关。
  • 您所看到的并不总是您所得到的。如果xml表示对象的宽度为103,那么Interface Builder可能会说它为104。但是,在您对设计进行实际更改之前,我不会将这一更改提交给xml。仅在那时,它将代码中的103更改为104。结果,将元素的属性更改为1会导致其他多项更改。

请参见代码示例。我有3个粉红色视图是3个红色父视图的子视图。小心地将粉红色视图调整为x = 4,y = 4,并且宽度和高度为263。 我还在子视图中放置了2个标签。放在x = 25,宽度为103。

但是在Interface Builder中打开它会改变一切。第一个和第三个粉红色视图的宽度将减小1点。第二个粉红色视图和第二个标签的宽度将增加1点,其x位置将减少1点。仅当您进行了一些实际的手动更改(如更正其中一项更改)后,情节提要的Xml代码才会更改。

这是怎么回事?似乎与视图的自动调整大小属性有关。

为什么界面构建器坚持这些更改?我需要使用一些设置来防止界面生成器代表我进行更改吗?

<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14460.31" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
    <device id="ipad9_7" orientation="landscape">
        <adaptation id="fullscreen"/>
    </device>
    <dependencies>
        <deployment identifier="iOS"/>
        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14460.20"/>
        <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
    </dependencies>
    <scenes>
        <!--Pads View Controller-->
        <scene sceneID="29X-ru-k3e">
            <objects>
                <viewController storyboardIdentifier="DUBPadsViewController" id="K8u-H8-1gq" customClass="DUBPadsViewController" customModule="InterfaceBuilderBug" customModuleProvider="target" sceneMemberID="viewController">
                    <layoutGuides>
                        <viewControllerLayoutGuide type="top" id="R7c-RJ-s0L"/>
                        <viewControllerLayoutGuide type="bottom" id="cf2-ad-Oen"/>
                    </layoutGuides>
                    <view key="view" contentMode="scaleToFill" id="W4r-9T-iqb">
                        <rect key="frame" x="0.0" y="0.0" width="973" height="335"/>
                        <autoresizingMask key="autoresizingMask" flexibleMinX="YES" flexibleMaxX="YES" flexibleMinY="YES" flexibleMaxY="YES"/>
                        <subviews>
                            <view contentMode="scaleToFill" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="ZrE-4D-D5t">
                                <rect key="frame" x="40" y="299" width="134" height="22"/>
                                <autoresizingMask key="autoresizingMask" flexibleMinX="YES" widthSizable="YES" flexibleMaxX="YES" flexibleMinY="YES" heightSizable="YES" flexibleMaxY="YES"/>
                                <subviews>
                                    <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Label A" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Rcr-5Y-UIA">
                                        <rect key="frame" x="25" y="0.0" width="103" height="22"/>
                                        <autoresizingMask key="autoresizingMask" flexibleMinX="YES" widthSizable="YES" flexibleMaxX="YES" flexibleMinY="YES" heightSizable="YES" flexibleMaxY="YES"/>
                                        <fontDescription key="fontDescription" name="Avenir-Medium" family="Avenir" pointSize="11"/>
                                        <nil key="highlightedColor"/>
                                    </label>
                                </subviews>
                                <color key="backgroundColor" red="1" green="0.28351856600000003" blue="0.11137302910000001" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                            </view>
                            <view contentMode="scaleToFill" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="wA1-jm-LnT">
                                <rect key="frame" x="177" y="299" width="134" height="22"/>
                                <autoresizingMask key="autoresizingMask" flexibleMinX="YES" widthSizable="YES" flexibleMaxX="YES" flexibleMinY="YES" heightSizable="YES" flexibleMaxY="YES"/>
                                <subviews>
                                    <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Label B" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="ZmY-M4-qUf">
                                        <rect key="frame" x="25" y="0.0" width="103" height="22"/>
                                        <autoresizingMask key="autoresizingMask" flexibleMinX="YES" widthSizable="YES" flexibleMaxX="YES" flexibleMinY="YES" heightSizable="YES" flexibleMaxY="YES"/>
                                        <fontDescription key="fontDescription" name="Avenir-Medium" family="Avenir" pointSize="11"/>
                                        <nil key="highlightedColor"/>
                                    </label>
                                </subviews>
                                <color key="backgroundColor" red="1" green="0.28351856600000003" blue="0.11137302910000001" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                            </view>
                            <view contentMode="scaleToFill" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="EKv-pO-CaP">
                                <rect key="frame" x="40" y="16" width="271" height="271"/>
                                <autoresizingMask key="autoresizingMask" flexibleMinX="YES" widthSizable="YES" flexibleMaxX="YES" flexibleMinY="YES" heightSizable="YES" flexibleMaxY="YES"/>
                                <subviews>
                                    <view contentMode="scaleToFill" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="KkF-uY-icM">
                                        <rect key="frame" x="4" y="4" width="263" height="263"/>
                                        <autoresizingMask key="autoresizingMask" flexibleMinX="YES" widthSizable="YES" flexibleMaxX="YES" flexibleMinY="YES" heightSizable="YES" flexibleMaxY="YES"/>
                                        <color key="backgroundColor" red="0.93333333330000001" green="0.48687065969999999" blue="0.92156862750000001" alpha="1" colorSpace="deviceRGB"/>
                                    </view>
                                </subviews>
                                <color key="backgroundColor" red="1" green="0.28351856600000003" blue="0.11137302910000001" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                            </view>
                            <view contentMode="scaleToFill" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="a9U-gL-6LU">
                                <rect key="frame" x="351" y="16" width="271" height="271"/>
                                <autoresizingMask key="autoresizingMask" flexibleMinX="YES" widthSizable="YES" flexibleMaxX="YES" flexibleMinY="YES" heightSizable="YES" flexibleMaxY="YES"/>
                                <subviews>
                                    <view contentMode="scaleToFill" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="V3f-qo-7Gz">
                                        <rect key="frame" x="4" y="4" width="263" height="263"/>
                                        <autoresizingMask key="autoresizingMask" flexibleMinX="YES" widthSizable="YES" flexibleMaxX="YES" flexibleMinY="YES" heightSizable="YES" flexibleMaxY="YES"/>
                                        <color key="backgroundColor" red="0.93333333330000001" green="0.48687065969999999" blue="0.92156862750000001" alpha="1" colorSpace="deviceRGB"/>
                                    </view>
                                </subviews>
                                <color key="backgroundColor" red="1" green="0.28351856600000003" blue="0.11137302910000001" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                            </view>
                            <view contentMode="scaleToFill" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Abi-Xp-jLY">
                                <rect key="frame" x="660" y="16" width="271" height="271"/>
                                <autoresizingMask key="autoresizingMask" flexibleMinX="YES" widthSizable="YES" flexibleMaxX="YES" flexibleMinY="YES" heightSizable="YES" flexibleMaxY="YES"/>
                                <subviews>
                                    <view contentMode="scaleToFill" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="8jR-Gr-IWp">
                                        <rect key="frame" x="4" y="4" width="263" height="263"/>
                                        <autoresizingMask key="autoresizingMask" flexibleMinX="YES" widthSizable="YES" flexibleMaxX="YES" flexibleMinY="YES" heightSizable="YES" flexibleMaxY="YES"/>
                                        <color key="backgroundColor" red="0.93333333330000001" green="0.48687065969999999" blue="0.92156862750000001" alpha="1" colorSpace="deviceRGB"/>
                                    </view>
                                </subviews>
                                <color key="backgroundColor" red="1" green="0.28351856600000003" blue="0.11137302910000001" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                            </view>
                        </subviews>
                        <color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
                    </view>
                    <size key="freeformSize" width="973" height="335"/>
                </viewController>
                <placeholder placeholderIdentifier="IBFirstResponder" id="6NU-Rl-zi7" userLabel="First Responder" sceneMemberID="firstResponder"/>
            </objects>
            <point key="canvasLocation" x="669" y="-44"/>
        </scene>
    </scenes>
</document>

1 个答案:

答案 0 :(得分:0)

尝试使用autolayout。有了它,您可以将视图限制为任何大小和位置,即使您更改画布大小,它们也将保留。