简体   繁体   English

Swift - 如何使视图 controller 可滚动?

[英]Swift - How to make a view controller scrollable?

I have a view controller with some elements in it.我有一个包含一些元素的视图 controller。

class MyViewController: UIViewController {
    // ......
}

This is what I want: example of what I want这就是我想要的:我想要的例子

This is what my app looks like:这是我的应用程序的样子:

我的应用

I want to make the two elements (an imageView and a button) to be vertically scrollable like the example.我想让这两个元素(一个 imageView 和一个按钮)像示例一样垂直滚动。

What should I do?我应该怎么办? I have checked UIScrollView , but still don't know how to do it.我已经检查了UIScrollView ,但仍然不知道该怎么做。 Can you show me some code?你能给我看一些代码吗? Thanks!谢谢!

There is an UI Component inside UIKit called UIScrollView . UIKit 中有一个 UI 组件,名为UIScrollView This Component can be used to achieve the Scrolling Behaviour you probably aim for.该组件可用于实现您可能想要的滚动行为

Check out the official Documentation by Apple UIScrollView |查看 Apple UIScrollView 的官方文档 |Apple Developer Documentation苹果开发者文档

Another great resource for getting started with UIScrollViews is this one.另一个开始使用 UIScrollViews 的好资源是这个。 UIScrollView Tutorial: Getting Started UIScrollView 教程:入门

One way to do this is programmatically create an UIScrollView in your UIViewController .一种方法是在UIViewController UIScrollView

To control the scrollability you can set the ScrollView contentSize property.要控制可滚动性,您可以设置 ScrollView contentSize属性。 In the following sample we match contentSize.width with self.view.frame.size.width to prevent horizontal scroll.在下面的示例中,我们将contentSize.widthself.view.frame.size.width匹配以防止水平滚动。

So, to achieve your goal, you must add your components inside the scrollView using scrollView.addSubview因此,要实现您的目标,您必须使用scrollView.addSubview在 scrollView 中添加组件

This is a simple sample of a UIScrollView with a red UIView inside:这是 UIScrollView 的简单示例,内部带有红色 UIView:

import UIKit

class MyViewController: UIViewController, UIScrollViewDelegate {
    
    lazy var scrollView: UIScrollView = {
        let scroll = UIScrollView()
        scroll.translatesAutoresizingMaskIntoConstraints = false
        scroll.delegate = self
        scroll.contentSize = CGSize(width: self.view.frame.size.width, height: 1000)
        return scroll
    }()
    
    lazy var redView:UIView = {
        let redview = UIView(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
        redview.backgroundColor = .red
        return redview
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(scrollView)
        scrollView.addSubview(redView)
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        let layout = view.safeAreaLayoutGuide
        scrollView.centerXAnchor.constraint(equalTo: layout.centerXAnchor).isActive = true
        scrollView.centerYAnchor.constraint(equalTo: layout.centerYAnchor).isActive = true
        scrollView.widthAnchor.constraint(equalTo: layout.widthAnchor).isActive = true
        scrollView.heightAnchor.constraint(equalTo: layout.heightAnchor).isActive = true
    }
    
    
}

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

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