简体   繁体   English

Xcode iOS-如何使图像适合不同的屏幕尺寸

[英]xcode ios - how to make an image fit into different screen sizes

I am making an iOS app. 我正在制作一个iOS应用。 Basically several login fields with a logo on top. 基本上是几个登录字段,顶部带有徽标。 The storyboard should simply fit device screen width. 情节提要仅适合设备屏幕的宽度。

I have started with a Scroll View (to make sure everything is available when the keyboard pops up). 我从滚动视图开始(以确保键盘弹出时所有功能都可用)。 The Scroll View has constraints: top 0, left 0, right 0. 滚动视图具有以下约束:顶部0,左侧0,右侧0。

Then I have put a Stack View in the Scroll View. 然后,将“堆栈视图”放入“滚动视图”中。 It also has constraints: top 0, left 0, right 0. 它还具有约束条件:顶部0,左侧0,右侧0。

I have added text fields and an Image View. 我添加了文本字段和图像视图。 The Image View also has constraints: top 0, left 0, right 0 and content mode "aspect fit". 图像视图还具有以下限制:顶部0,左侧0,右侧0和内容模式“纵横比”。 I want the logo to be simply resized to fit screen width. 我希望徽标可以简单地调整大小以适合屏幕宽度。 The PNG image has a "Dummy logo" text. PNG图像具有“虚拟徽标”文本。

This is what I see in XCode (the same happens in the simulator and on a real device): 这是我在XCode中看到的(在模拟器和真实设备中也是如此): 在此处输入图片说明 The image keeps its original size and is clipped. 图像保持其原始尺寸并被裁剪。 What should I do to make the Image View follow screen width? 我应该怎么做才能使图像视图遵循屏幕宽度? Thank you 谢谢

The problem was that my layout was broken . 问题是我的布局坏了 It is shown by the red dot in "file explorer" (top left). 它由“文件资源管理器”中的红点显示(左上方)。 After clicking on the dot Xcode has shown me which properties don't make sense. 单击点后,Xcode向我显示了哪些属性没有意义。 Size of Scroll view contents were not fixed. 滚动视图内容的大小不固定。 I had to link the size of StackView and main View - I did that by selecting (hold Ctrl + click) both View and StackView in file explorer and then right clicking on them and adding equal height/width constraints (though I still have to verify that everything looks reasonable on different devices). 我必须链接StackView和Main View的大小-通过在文件资源管理器中同时选择View和StackView(按住Ctrl键并单击),然后右键单击它们并添加相等的高度/宽度约束 (尽管我仍然必须验证在不同的设备上看起来一切都合理)。

Constraining the ImageView to StackView had no effect, because StackView constraint was the size of ScrollView, which can hold any size, so effectively the size was not fixed to anything. 将ImageView约束为StackView无效,因为StackView约束为ScrollView的大小,该大小可以容纳任何大小,因此实际上大小并未固定为任何大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM