首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIRefreshControl -滚动视图需要“满”才能工作吗?

UIRefreshControl -滚动视图需要“满”才能工作吗?
EN

Stack Overflow用户
提问于 2022-03-09 13:56:50
回答 2查看 53关注 0票数 0

我有开发人员正在开发一个IOS应用程序,该应用程序有一个视图中的人网格(一个约会风格的应用程序)。我需要“拉刷新”才能在这个屏幕上工作,即使网格中的用户还没有填充屏幕。如果只有2-4个用户,网格还不够满,还不能滚动。

我的iOS开发人员告诉我,如果网格不是满的和可滚动的,iOS“拉到刷新”就不能工作。这是真的吗?不应该拉刷新工作,无论屏幕是否已满?或者这是怎么容易编程的呢?

谢谢。

EN

回答 2

Stack Overflow用户

发布于 2022-03-09 17:25:19

这是你能得到的最基本的例子.

代码语言:javascript
复制
class ViewController: UIViewController {

    let scrollView = UIScrollView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        scrollView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(scrollView)
        
        let g = view.safeAreaLayoutGuide
        
        NSLayoutConstraint.activate([

            // let's constrain the scroll view with 20-points on all sides
            //  so we can easily distinguish it from the main view
            scrollView.topAnchor.constraint(equalTo: g.topAnchor, constant: 20.0),
            scrollView.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 20.0),
            scrollView.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -20.0),
            scrollView.bottomAnchor.constraint(equalTo: g.bottomAnchor, constant: -20.0),

        ])
        
        // give it a background color so we can see it
        scrollView.backgroundColor = .red
        
        // Add the refresh control
        scrollView.refreshControl = UIRefreshControl()
        scrollView.refreshControl?.addTarget(self, action: #selector(handleRefreshControl), for: .valueChanged)

    }

    @objc func handleRefreshControl() {
        // do whatever you do to get new content
        print("refresh the content")
        
        // let's simulate a 1-second task to get new content
        DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
            self.scrollView.refreshControl?.endRefreshing()
        }
    }

}

请注意,我们甚至没有将任何子视图添加到滚动视图中,并且刷新控件仍然有效。

票数 0
EN

Stack Overflow用户

发布于 2022-03-09 17:27:26

尝试将alwaysBouncesVertical设置为true

来自用于UIScrollView的文档(它是UITableView的父文档):

如果将此属性设置为true,且

为true,则允许垂直拖动,即使内容小于滚动视图的界限。默认值为false。

https://developer.apple.com/documentation/uikit/uiscrollview/1619383-alwaysbouncevertical

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71410488

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档