ios UIScrollView 不在 Swift 中滚动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28144739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 04:30:33  来源:igfitidea点击:

UIScrollView not scrolling in Swift

iosswiftuiscrollviewxcode6

提问by Mohammad Nurdin

My UIScrollView won't scroll down. I don't know why. I already followed Apple documentation regarding to this issue.

我的 UIScrollView 不会向下滚动。我不知道为什么。我已经遵循了有关此问题的 Apple 文档。

@IBOutlet weak var scroller: UIScrollView!

    override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

}

override func viewDidLayoutSubviews() {
    scroller.scrollEnabled = true
    // Do any additional setup after loading the view
    scroller.contentSize = CGSizeMake(400, 2300)
}

回答by Christian

You need to set the frameof your UIScrollViewso that it is less than the contentsize. Otherwise, it won't scroll.

您需要设置frame您的UIScrollView,使其小于contentsize. 否则,它不会滚动。

Also, I would recommend that you add scroller.contentSize = CGSizeMake(400, 2300)to your viewDidLoadmethod.

另外,我建议您添加scroller.contentSize = CGSizeMake(400, 2300)到您的viewDidLoad方法中。

回答by flo527

Alot of the time the code is correct if you have followed a tutorial but what many beginners do not know is that the scrollView is NOT going to scroll normally through the simulator. It is suppose to scroll only when you press down on the mousepad and simultaneously scroll. Many Experienced XCode/Swift/Obj-C users are so use to doing this and so they do not know how it could possibly be overlooked by beginners. Ciao :-)

很多时候,如果您遵循教程,代码是正确的,但许多初学者不知道的是,scrollView 不会在模拟器中正常滚动。假设只有当您按下鼠标垫并同时滚动时才会滚动。许多有经验的 XCode/Swift/Obj-C 用户习惯于这样做,所以他们不知道它怎么可能被初学者忽视。再见 :-)

@IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(scrollView)
// Do any additional setup after the view
}

override func viewWillLayoutSubviews(){
super.viewWillLayoutSubviews()
scrollView.contentSize = CGSize(width: 375, height: 800)
}

This code will work perfectly fine as long as you do what I said up above

只要你按照我上面说的去做,这段代码就可以正常工作

回答by Fangming

Swift 3.0 version

斯威夫特 3.0 版本

scroller.contentSize = CGSize(width: scroller.contentSize.width, height: 2000)

回答by satheesh

If you use AutoLayout,

如果您使用自动布局,

Set content size in viewdidAppearwhich works for me.

在适合我的viewdidAppear 中设置内容大小。

override func viewDidAppear(_ animated: Bool) {

覆盖 func viewDidAppear(_ 动画:布尔){

    scrollView.contentSize = CGSize(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height+300)

}

回答by Sheetal Shinde

Do not give fix height to scroll view and always give top of first subview to scrollview and bottom of last subview to scrollview. By this way scroll view will automatically grow as per the size of contained subviews. No need to give contentSize to the scrollview.It will work for small as well as large size iPhone.

不要为滚动视图提供固定高度,并始终将第一个子视图的顶部设置为滚动视图,将最后一个子视图的底部设置为滚动视图。通过这种方式,滚动视图将根据包含的子视图的大小自动增长。无需为滚动视图提供 contentSize。它适用于小型和大型 iPhone。

回答by Carlos Compean

If you are using autolayout, then the contentSize property stops working and it will try to infer the content size from the constraints. If that is the case, then your problem could be that you are not defining the necessary constraints to the content view so that the scrollview can infer the content size. You should define the constraints of your content view to the top and bottom edges of the scrollview.

如果您使用自动布局,则 contentSize 属性将停止工作,它将尝试从约束推断内容大小。如果是这种情况,那么您的问题可能是您没有为内容视图定义必要的约束,以便滚动视图可以推断内容大小。您应该将内容视图的约束定义为滚动视图的顶部和底部边缘。

回答by Jevon Charles

The problem could be that your scrollView doesn't know its contentSize like stated above, but the fix is easier than what the above answers are. Like Carlossaid but I will elaborate more. If you want your scrollView to scroll vertically(up & down), make your contentView which is in the hierarchy of the scrollView equal width to the ViewController and give it a height constraint that works for your project i.e. 700. For the opposite(horizontally) make the height equal to the ViewController and the width some big number that works for your project.

问题可能是你的 scrollView 不知道它的 contentSize 如上所述,但修复比上面的答案更容易。就像卡洛斯说的,但我会详细说明。如果你想让你的 scrollView 垂直滚动(向上和向下),让你的 contentView 在 scrollView 的层次结构中等于 ViewController 的宽度,并给它一个适用于你的项目的高度约束,即 700。相反(水平)使高度等于 ViewController 和宽度一些适合您的项目的大数字。

回答by user3480687

It worked for me. In Size Inspector Layout = Translates Mask into constraints. Autoresizing = all click.

它对我有用。在 Size Inspector Layout = 将 Mask 转换为约束。自动调整大小 = 所有点击。

enter image description here

在此处输入图片说明

回答by Zaporozhchenko Oleksandr

In my case, I used UIStackView inside UIScrollView.

就我而言,我在 UIScrollView 中使用了 UIStackView。

Added some views-elements from code to stackview. It won't scroll.

将代码中的一些视图元素添加到 stackview。它不会滚动。

Fixed it by setting stackview's userInteractionEnabledto false.

通过将 stackview 设置userInteractionEnabledfalse.

回答by gellezzz

I do not know it is a good solution, but you can try to set headerview to empty UITableView.

我不知道这是一个好的解决方案,但您可以尝试将 headerview 设置为空 UITableView。

let scrollView: UIView = UIView(frame: CGRectMake(0, 0, 400, 2300))

tableView.tableHeaderView = scrollView