XCode:要使图像与屏幕大小相同,需要哪些约束?
XCode: which constraints are needed to have an image with the same size as screen?
在我的应用程序中有一个 UIViewController
,其唯一工作是显示从服务器下载的图像。我希望此图像与默认屏幕大小相同;此外,由于此图像是一种食谱,我希望用户能够缩放图像 in/out 以便更好地阅读,这就是为什么我将 UIImageview
嵌入 UIScrollView
.
UIScrollView
有4个约束:UIScrollView
和UIViewController
之间的4个距离都设置为0。
UIImageView
有4个约束:UIImageView
和包含它的UIScrollView
之间的4个距离都设置为0。
不管怎样,从下面的截图可以看出
结果与预期不符:UIScrollView
和 UIViewController
顶部之间的距离更大,图像更大。
你知道如何得到想要的结果吗?
<!--Recipe View Controller-->
<scene sceneID="y5Y-bz-arP">
<objects>
<viewController id="VnP-A1-Srr" customClass="RecipeViewController" customModule="Fishmeter" customModuleProvider="target" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="J4Y-SW-LaP"/>
<viewControllerLayoutGuide type="bottom" id="Yuj-fl-Q0L"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="1xx-WP-lNU">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<scrollView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" ambiguous="YES" misplaced="YES" translatesAutoresizingMaskIntoConstraints="NO" id="5rg-8n-qc9">
<rect key="frame" x="27" y="94" width="558" height="486"/>
<subviews>
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" ambiguous="YES" misplaced="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Y0D-XM-XJ8">
<rect key="frame" x="159" y="158" width="240" height="128"/>
</imageView>
</subviews>
<constraints>
<constraint firstAttribute="trailing" secondItem="Y0D-XM-XJ8" secondAttribute="trailing" id="P5c-2q-jZO"/>
<constraint firstItem="Y0D-XM-XJ8" firstAttribute="leading" secondItem="5rg-8n-qc9" secondAttribute="leading" id="VkK-Pa-vmE"/>
<constraint firstAttribute="bottom" secondItem="Y0D-XM-XJ8" secondAttribute="bottom" id="mzG-vW-1fV"/>
<constraint firstItem="Y0D-XM-XJ8" firstAttribute="top" secondItem="5rg-8n-qc9" secondAttribute="top" id="uoZ-V1-kxX"/>
</constraints>
</scrollView>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstAttribute="trailingMargin" secondItem="5rg-8n-qc9" secondAttribute="trailing" id="PbP-OD-eRM"/>
<constraint firstItem="Yuj-fl-Q0L" firstAttribute="top" secondItem="5rg-8n-qc9" secondAttribute="bottom" id="TQL-96-UgX"/>
<constraint firstItem="5rg-8n-qc9" firstAttribute="leading" secondItem="1xx-WP-lNU" secondAttribute="leadingMargin" id="XEX-ck-lZi"/>
<constraint firstItem="5rg-8n-qc9" firstAttribute="top" secondItem="J4Y-SW-LaP" secondAttribute="bottom" constant="-10" id="lL6-Dk-MZe"/>
</constraints>
</view>
<connections>
<outlet property="recipeImage" destination="Y0D-XM-XJ8" id="5pc-vd-Ak8"/>
<outlet property="scrollView" destination="5rg-8n-qc9" id="Ieo-Uk-HIB"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="cUt-8l-j6r" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="3265" y="-311"/>
</scene>
为了使 UIScrollView 正确处理其内容,滚动视图内容的约束不能由滚动视图的大小定义。约束必须引用在滚动视图外定义的大小。
所以,去掉UIImageView的约束,设置约束到UIViewController主视图.[=10=的上下左右距离为0 ]
--T
在我的应用程序中有一个 UIViewController
,其唯一工作是显示从服务器下载的图像。我希望此图像与默认屏幕大小相同;此外,由于此图像是一种食谱,我希望用户能够缩放图像 in/out 以便更好地阅读,这就是为什么我将 UIImageview
嵌入 UIScrollView
.
UIScrollView
有4个约束:UIScrollView
和UIViewController
之间的4个距离都设置为0。
UIImageView
有4个约束:UIImageView
和包含它的UIScrollView
之间的4个距离都设置为0。
不管怎样,从下面的截图可以看出
结果与预期不符:UIScrollView
和 UIViewController
顶部之间的距离更大,图像更大。
你知道如何得到想要的结果吗?
<!--Recipe View Controller-->
<scene sceneID="y5Y-bz-arP">
<objects>
<viewController id="VnP-A1-Srr" customClass="RecipeViewController" customModule="Fishmeter" customModuleProvider="target" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="J4Y-SW-LaP"/>
<viewControllerLayoutGuide type="bottom" id="Yuj-fl-Q0L"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="1xx-WP-lNU">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<scrollView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" ambiguous="YES" misplaced="YES" translatesAutoresizingMaskIntoConstraints="NO" id="5rg-8n-qc9">
<rect key="frame" x="27" y="94" width="558" height="486"/>
<subviews>
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" ambiguous="YES" misplaced="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Y0D-XM-XJ8">
<rect key="frame" x="159" y="158" width="240" height="128"/>
</imageView>
</subviews>
<constraints>
<constraint firstAttribute="trailing" secondItem="Y0D-XM-XJ8" secondAttribute="trailing" id="P5c-2q-jZO"/>
<constraint firstItem="Y0D-XM-XJ8" firstAttribute="leading" secondItem="5rg-8n-qc9" secondAttribute="leading" id="VkK-Pa-vmE"/>
<constraint firstAttribute="bottom" secondItem="Y0D-XM-XJ8" secondAttribute="bottom" id="mzG-vW-1fV"/>
<constraint firstItem="Y0D-XM-XJ8" firstAttribute="top" secondItem="5rg-8n-qc9" secondAttribute="top" id="uoZ-V1-kxX"/>
</constraints>
</scrollView>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstAttribute="trailingMargin" secondItem="5rg-8n-qc9" secondAttribute="trailing" id="PbP-OD-eRM"/>
<constraint firstItem="Yuj-fl-Q0L" firstAttribute="top" secondItem="5rg-8n-qc9" secondAttribute="bottom" id="TQL-96-UgX"/>
<constraint firstItem="5rg-8n-qc9" firstAttribute="leading" secondItem="1xx-WP-lNU" secondAttribute="leadingMargin" id="XEX-ck-lZi"/>
<constraint firstItem="5rg-8n-qc9" firstAttribute="top" secondItem="J4Y-SW-LaP" secondAttribute="bottom" constant="-10" id="lL6-Dk-MZe"/>
</constraints>
</view>
<connections>
<outlet property="recipeImage" destination="Y0D-XM-XJ8" id="5pc-vd-Ak8"/>
<outlet property="scrollView" destination="5rg-8n-qc9" id="Ieo-Uk-HIB"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="cUt-8l-j6r" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="3265" y="-311"/>
</scene>
为了使 UIScrollView 正确处理其内容,滚动视图内容的约束不能由滚动视图的大小定义。约束必须引用在滚动视图外定义的大小。
所以,去掉UIImageView的约束,设置约束到UIViewController主视图.[=10=的上下左右距离为0 ]
--T