简体   繁体   English

滚动视图在iPad上的xcode(iOS)中无法正常工作

[英]Scrollview not working properly on iPad in xcode(iOS)

Recently, i've been building and app which requires a scrollview, for all the content to fit. 最近,我一直在构建和需要滚动视图的应用程序,以适合所有内容。 I've had a few difficulties creating my scrollview. 创建滚动视图时遇到了一些困难。 I've tried a few different methods for building it, but ended up building it in storyboard with autolayout. 我尝试了几种其他方法来构建它,但最终使用自动布局在情节提要中构建它。 I've set the height of the view controller to 1000, and therefore also the simulated size to freeform. 我将视图控制器的高度设置为1000,因此将模拟尺寸设置为自由格式。 The width of the content view inside the scrollview, I've to the same as the view which contains the scrollview. 滚动视图内部的内容视图的宽度,必须与包含滚动视图的视图的宽度相同。

The hierarchy goes like this: 层次结构如下所示:

  1. -View controller scene -查看控制器场景
  2. -Top layout guide -顶部布局指南
  3. -Bottom layout guide -底部布局指南
  4. -View -视图
  5. -Scroll view -滚动视图
  6. -Content view -内容视图
  7. -Label with text here -在此处加上文字标签
  8. -Constraints -Constraints
  9. -And so on -等等

I've been replicating the process in this link, to create the scrollview: https://www.ralfebert.de/snippets/ios/auto-layout-recipes/uiscrollview-storyboard/ 我一直在复制此链接中的过程,以创建滚动视图: https ://www.ralfebert.de/snippets/ios/auto-layout-recipes/uiscrollview-storyboard/

When running the application on the simulator, I get the following result: The scrollview is not working properly on the iPad. 在模拟器上运行该应用程序时,得到以下结果:在iPad上,scrollview无法正常工作。 It seems like the scrollview doesn't adapt to the changes in the width, which happens when its ran on an iPad, since it's bigger. 滚动视图似乎无法适应宽度的变化,因为它更大,因此在iPad上运行时会发生这种情况。 When the app is ran on the iPad, the scrollview stays the same width as if it were shown on the iPhone. 在iPad上运行该应用程序时,滚动视图的宽度与在iPhone上显示的宽度相同。

This is how it looks in the editor. 这就是它在编辑器中的外观。 The view controller on the left is the view controller which has the scrollview implemented, and the one on the right doesn't. 左侧的视图控制器是实现了滚动视图的视图控制器,而右侧的则没有。 The first image shows the storyboard, when I view it with the iPhone 8, and the second image is when I view it with an 9,7" iPad: 第一张图片显示的是情节提要,当我使用iPhone 8查看时,第二张图片显示的是当我用9.7“ iPad查看时: iPhone 8

iPad 9.7英寸

It would be greatly appreciated if anyone could give me a hint to whats going on! 如果有人可以给我提示发生的事情,将不胜感激!

EDIT: Left side is what it looks like, and right side is what it should look like: 编辑:左侧是它的外观,而右侧是它的外观:

  1. Add the UIScrollView to your main view UIScrollView添加到主视图
  2. Set Top, Left, Right, Bottom constraints all to Zero 将上,左,右,下约束全部设置为零
  3. Add a UILabel inside the scroll view 在滚动视图内添加UILabel
  4. Set number of lines to Zero 将行数设置为零
  5. Set Top, Left, Right, Bottom constraints all to Zero 将上,左,右,下约束全部设置为零
  6. Set the label's width constraint equal to the scroll view's width 设置标签的宽度约束等于滚动视图的宽度

That should do it :) 那应该做的:)

This will keep the label's text aligned to the top of the scroll view. 这将使标签的文本与滚动视图的顶部对齐。 If you add enough text to fill more than the full screen, it will scroll vertically. 如果您添加了足够多的文本以填满整个屏幕,则它将垂直滚动。

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

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