Swift UIScrollView - 仅垂直滚动的正确实现 [英] Swift UIScrollView - Correct Implementation for Only Vertical Scrolling

查看:48
本文介绍了Swift UIScrollView - 仅垂直滚动的正确实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此之前,我很抱歉,因为我是 iOS 编程的初学者.

In advance, I apologize as I would say that I'm a beginner with iOS programming.

我想使用 UIScrollView 因为要显示的内容超过了屏幕的高度.因此,我只想要垂直滚动而不是水平滚动.

I want to use UIScrollView because the content to display exceed the height of the screen. Therefore, I would like only a vertical scroll and not a horizontal scrolling.

我正在使用 Storyboard 通过 AutoLayout 绘制视图.

I'm using the Storyboard to draw the views with AutoLayout.

这是我的 UIViewControllerUIScrollView 的截图:

Here's are the screenshot of my UIViewController with the UIScrollView :

然后我将标签更改为这样的大文本

Then I change the Label to a big text like that

override func viewDidLoad() {
    super.viewDidLoad()

    self.label1Label.text = "Seize jours après la chute du président Blaise Compaoré, le Burkina Faso a un nouveau chef d'EtatA l'issue d'ultimes tractions, civils et militaires se sont accordés, lundi 17 novembre, sur le nom du diplomate Michel KafandoSeize jours après la chute du président Blaise Compaoré, le Burkina Faso a un nouveau chef d'EtatA l'issue d'ultimes tractions, civils et militaires se sont accordés, lundi 17 novembre, sur le nom du diplomate Michel Kafando"
    self.label1Label.numberOfLines = 0
    self.label1Label.sizeToFit()

我的问题是,如果我不为我的 contentView 手动设置宽度(在 UIScrollView 内),滚动是水平的,而不是垂直的.(看下面的截图):

My problem is that if I don't set manually a width for my contentView (inside the UIScrollView), the scrolling is horizontal, not vertical. (Look Screenshot below):

我尝试设置 contentSize,就像我在许多谷歌帖子中看到的那样,但没有成功:

I've tried to set contentSize as I've seen in many google post but without success :

self.scrollView.contentSize = CGSizeMake(400.0, 600.0)

如果我为 contentview 手动设置宽度(ie : 320pts),滚动将是垂直的(好),但随后取决于 iphone 的大小,它不会覆盖整个宽度屏幕如下图所示:

If I set a width manually for my contentview (i.e : 320pts), the scrolling will be vertical (GOOD) but then depending on the iphone size, it won't cover the whole width of the screen as shown in the following screenshot :

问题是:使用 UIScrollView 来拥有一个遵守自动布局约束的 contentView 的正确实现是什么(full screen : 0top - 0bottom - 0left- 0right) 并且滚动只能垂直.

The question is : what is the correct implementation to use UIScrollView to have a contentView that respect the autolayout constraint (full screen : 0top - 0bottom - 0left - 0right) and the scrolling to be only vertical.

非常感谢您的帮助!

推荐答案

Mike Woelmer 在 Atomic Object 博客上展示了如何使用 Interface Builder 正确执行此操作.

Mike Woelmer shows how to do this correctly with Interface Builder on the Atomic Object blog.

http://spin.atomicobject.com/2014/03/05/uiscrollview-autolayout-ios/

我在 github 上也只有自己的代码(没有故事板实现).

I also have my own code only (no storyboard implementation) on github.

https://github.com/nadthevlad/AutolayotScrollview

您不想设置内容或视图的高度或宽度.相反,您希望使用 Autolayouts 引脚和约束来设置滚动视图的行为方式.

You don't want to set the height or width of your content or views. Instead you want to use Autolayouts pins and constraints to set how the scrollview behaves.

  1. 创建您的 UIScrollView *scrollView.

  1. Create your UIScrollView *scrollView.

您想要创建一个 UIView *contentView,您将把其余的视图元素放入其中.

You want to create one UIView *contentView which you will put the rest of your view elements into.

[self.view addSubview:scrollView];
[scrollView addSubview:contentView];
[contentView addSubview:_label1];
[contentView addSubview:_label2];
[contentView addSubview:_label3];
[contentView addSubview:_label4];
[contentView addSubview:_label5];
[contentView addSubview:_label6];

  • 将scrollView的4条边固定到self.view的4条边

  • Pin the 4 edges of scrollView to the 4 edges of self.view

    将 contentView 的顶部和底部边缘固定到 scrollView 的顶部和底部.

    Pin the top and bottom edges of contentView to the top and bottom of scrollView.

    这是棘手的部分.要设置水平尺寸,您需要将 contentView 的前缘(右)和后缘(左)固定到前缘和后缘 self.view 而不是 scrollView.尽管 contenView 是 scrollView 的子视图,但它的水平约束将到达 scrollView 之外并连接到 self.view.

    This is the tricky part. To set the horizontal sizing, you want the leading (right) and trailing(left) edges of the contentView to be pinned to the leading and trailing edges self.view instead of scrollView. Even though contenView is a sub view of scrollView its horizontal constraints are going to reach outside of the scrollView and connect to self.view.

    像往常一样将任何其他视图元素固定到 contentView.

    Pin any other view elements to contentView as you normally would.

    这篇关于Swift UIScrollView - 仅垂直滚动的正确实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆