Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 具有某些复杂条件的UIScrollView中的动态大小UIImageView_Ios_Swift_Uiscrollview_Uiimageview_Autolayout - Fatal编程技术网

Ios 具有某些复杂条件的UIScrollView中的动态大小UIImageView

Ios 具有某些复杂条件的UIScrollView中的动态大小UIImageView,ios,swift,uiscrollview,uiimageview,autolayout,Ios,Swift,Uiscrollview,Uiimageview,Autolayout,如果要创建此类布局,应如何配置自动布局?或者让我知道,如果只有自动布局是不可能的 [UIView] [UIScrollView] [UIImageView] [UILabel] [UIButton] 我想实现的条件: UIImageView: 宽度:高度=1:1(宽高比=1:1) 宽度=UIView。宽度*70%(最小宽度) 如果屏幕尺寸足够大,UIImageView应该扩展到最大宽度,不需要滚动 如果屏幕大小不够大,但可以在其最小宽度中显示而无需任何滚动,U

如果要创建此类布局,应如何配置自动布局?或者让我知道,如果只有自动布局是不可能的

[UIView]
  [UIScrollView]
    [UIImageView]
    [UILabel]
    [UIButton]
我想实现的条件:

  • UIImageView:
    • 宽度:高度=1:1(宽高比=1:1)
    • 宽度=UIView。宽度*70%(最小宽度)
  • 如果屏幕尺寸足够大,UIImageView应该扩展到最大宽度,不需要滚动
  • 如果屏幕大小不够大,但可以在其最小宽度中显示而无需任何滚动,UIImageView应缩小到最小宽度,且无需滚动(即,所有子视图均无需任何滚动)
  • 如果屏幕大小不够大,并且即使UIImageView缩小到最小宽度,也需要滚动,UIImageView应该缩小到最小宽度,并且需要滚动(即并非所有子视图都显示为没有滚动)

这可能对你有好处

解释一切都有点困难,但你应该能够遵循图片中的约束条件。您唯一看不到的是,多行标签必须将其
内容压缩阻力优先级设置为
Required(1000)

其想法是使用特定的优先级值设置约束,以便自动布局将尝试使图像视图宽度(在
998
Priority)与滚动视图一样宽,但允许其随着下面内容的增长而缩小到最小宽度70%。当它达到70%时,它不会变小,图像视图+标签+按钮的组合高度将强制“内容”视图垂直展开,因为它的高度约束为
999

此处标签有7行文字,字体大小为18:

下面是字体大小增加到28时的外观-请注意,图像视图现在小于滚动视图的全宽:

现在,随着字体大小增加到36,我们看到图像视图的最小宽度为70%,并且按钮已按下,超过了滚动视图框架的底部(因此所有内容都将滚动):

下面是故事板的源代码-通过调整字体大小和/或标签中的文本量来查看它的工作情况:

<?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" useSafeAreas="YES" colorMatched="YES" initialViewController="eB4-FG-006">
    <device id="retina4_7" orientation="portrait">
        <adaptation id="fullscreen"/>
    </device>
    <dependencies>
        <deployment identifier="iOS"/>
        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14460.20"/>
        <capability name="Safe area layout guides" minToolsVersion="9.0"/>
        <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
    </dependencies>
    <scenes>
        <!--View Controller-->
        <scene sceneID="cKl-8C-enU">
            <objects>
                <viewController id="eB4-FG-006" sceneMemberID="viewController">
                    <view key="view" contentMode="scaleToFill" id="qMY-Qc-gio">
                        <rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
                        <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                        <subviews>
                            <scrollView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="q6G-7e-kz1">
                                <rect key="frame" x="20" y="60" width="335" height="567"/>
                                <subviews>
                                    <view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="mbq-g1-m6Z" userLabel="ContentView">
                                        <rect key="frame" x="0.0" y="0.0" width="335" height="567"/>
                                        <subviews>
                                            <imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="N7z-eO-cci">
                                                <rect key="frame" x="0.0" y="0.0" width="335" height="335"/>
                                                <color key="backgroundColor" red="0.0" green="0.97680455450000003" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                                                <constraints>
                                                    <constraint firstAttribute="width" secondItem="N7z-eO-cci" secondAttribute="height" multiplier="1:1" id="3Za-Q8-IFq"/>
                                                </constraints>
                                            </imageView>
                                            <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" verticalCompressionResistancePriority="1000" textAlignment="natural" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Pp5-EQ-8Vh" userLabel="Multi-LineLabel">
                                                <rect key="frame" x="142.5" y="343" width="50" height="150.5"/>
                                                <color key="backgroundColor" red="0.99953407049999998" green="0.98835557699999999" blue="0.47265523669999998" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                                                <string key="text">Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7</string>
                                                <fontDescription key="fontDescription" type="system" pointSize="18"/>
                                                <nil key="textColor"/>
                                                <nil key="highlightedColor"/>
                                            </label>
                                            <button opaque="NO" contentMode="scaleToFill" verticalCompressionResistancePriority="1000" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="rg8-I7-3hZ">
                                                <rect key="frame" x="144.5" y="501.5" width="46" height="30"/>
                                                <color key="backgroundColor" red="1" green="0.83234566450000003" blue="0.47320586440000001" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                                                <constraints>
                                                    <constraint firstAttribute="height" constant="30" id="NZU-hx-0M5"/>
                                                </constraints>
                                                <state key="normal" title="Button"/>
                                            </button>
                                        </subviews>
                                        <color key="backgroundColor" red="1" green="0.14913141730000001" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                                        <constraints>
                                            <constraint firstItem="rg8-I7-3hZ" firstAttribute="top" secondItem="Pp5-EQ-8Vh" secondAttribute="bottom" constant="8" id="6c5-vJ-c8B"/>
                                            <constraint firstItem="rg8-I7-3hZ" firstAttribute="centerX" secondItem="mbq-g1-m6Z" secondAttribute="centerX" id="C6W-uG-Irk"/>
                                            <constraint firstItem="Pp5-EQ-8Vh" firstAttribute="centerX" secondItem="mbq-g1-m6Z" secondAttribute="centerX" id="MDd-eI-O2X"/>
                                            <constraint firstItem="Pp5-EQ-8Vh" firstAttribute="top" secondItem="N7z-eO-cci" secondAttribute="bottom" constant="8" id="Ozv-Hn-EbX"/>
                                            <constraint firstItem="N7z-eO-cci" firstAttribute="top" secondItem="mbq-g1-m6Z" secondAttribute="top" id="Qbn-EK-zhn"/>
                                            <constraint firstAttribute="bottom" relation="greaterThanOrEqual" secondItem="rg8-I7-3hZ" secondAttribute="bottom" constant="8" id="XRT-bz-2bI"/>
                                            <constraint firstItem="rg8-I7-3hZ" firstAttribute="top" secondItem="Pp5-EQ-8Vh" secondAttribute="bottom" constant="8" id="bny-1w-xOv"/>
                                            <constraint firstItem="N7z-eO-cci" firstAttribute="width" relation="greaterThanOrEqual" secondItem="mbq-g1-m6Z" secondAttribute="width" multiplier="0.7" id="bsP-M1-7uP"/>
                                            <constraint firstItem="N7z-eO-cci" firstAttribute="width" secondItem="mbq-g1-m6Z" secondAttribute="width" priority="998" id="fr8-4U-00h"/>
                                            <constraint firstItem="N7z-eO-cci" firstAttribute="centerX" secondItem="mbq-g1-m6Z" secondAttribute="centerX" id="hVh-Ld-mDS"/>
                                        </constraints>
                                    </view>
                                </subviews>
                                <color key="backgroundColor" red="0.0" green="0.47843137250000001" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                                <constraints>
                                    <constraint firstAttribute="trailing" secondItem="mbq-g1-m6Z" secondAttribute="trailing" id="Fbg-Dx-idJ"/>
                                    <constraint firstAttribute="bottom" secondItem="mbq-g1-m6Z" secondAttribute="bottom" id="Hrx-mL-t1p"/>
                                    <constraint firstItem="mbq-g1-m6Z" firstAttribute="top" secondItem="q6G-7e-kz1" secondAttribute="top" id="La2-Bo-p6J"/>
                                    <constraint firstItem="mbq-g1-m6Z" firstAttribute="leading" secondItem="q6G-7e-kz1" secondAttribute="leading" id="S8w-du-yf0"/>
                                    <constraint firstItem="mbq-g1-m6Z" firstAttribute="height" secondItem="q6G-7e-kz1" secondAttribute="height" priority="999" id="lWB-wI-t5Q"/>
                                    <constraint firstItem="mbq-g1-m6Z" firstAttribute="width" secondItem="q6G-7e-kz1" secondAttribute="width" id="ynr-jB-Phr"/>
                                </constraints>
                            </scrollView>
                        </subviews>
                        <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
                        <constraints>
                            <constraint firstItem="ASm-Jv-0JK" firstAttribute="trailing" secondItem="q6G-7e-kz1" secondAttribute="trailing" constant="20" id="27b-fv-IjT"/>
                            <constraint firstItem="q6G-7e-kz1" firstAttribute="leading" secondItem="ASm-Jv-0JK" secondAttribute="leading" constant="20" id="4sv-oj-8jc"/>
                            <constraint firstItem="q6G-7e-kz1" firstAttribute="top" secondItem="ASm-Jv-0JK" secondAttribute="top" constant="40" id="KKa-P9-1oI"/>
                            <constraint firstItem="ASm-Jv-0JK" firstAttribute="bottom" secondItem="q6G-7e-kz1" secondAttribute="bottom" constant="40" id="b3R-Aw-brw"/>
                        </constraints>
                        <viewLayoutGuide key="safeArea" id="ASm-Jv-0JK"/>
                    </view>
                </viewController>
                <placeholder placeholderIdentifier="IBFirstResponder" id="zh5-Lh-ZPo" userLabel="First Responder" sceneMemberID="firstResponder"/>
            </objects>
            <point key="canvasLocation" x="806" y="-161"/>
        </scene>
    </scenes>
</document>

第1行
第2行
第3行
第4行
第5行
第6行
第7行