How to customize UIRefreshControl to make pull-down-height lower than default

亡梦爱人 提交于 2019-12-12 10:44:25

问题


In UIRefreshControl in iOS App, I think the default setting is that when I pull down UITableView(UISrollView) by about "100px", refresh begins.

I would like to make the above value smaller.(for example, "50px")

Is this possible?

If possible, please tell me sample codes.


回答1:


Try this:

// definition
extension UIRefreshControl {
    func refreshManually() {
        beginRefreshing()
        sendActions(for: .valueChanged)
    }
}

// usage
var isRefreshingManually = false
func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if scrollView.contentOffset.y < -80.0 {
        if !isRefreshingManually && !refreshControl.isRefreshing {
            isRefreshingManually = true
            refreshControl.refreshManually()
        }
    } else if scrollView.contentOffset.y >= 0 {
        isRefreshingManually = false
    }
}

My sample code is for UICollectionView, but UITableView & UIScollView work well, too.

Replace "-80.0" in my code to threshould value you want.




回答2:


you can make it right with some modification

lazy var refreshControl: UIRefreshControl = {
        let refreshControl = UIRefreshControl()
        refreshControl.tintColor = UIColor.red
        return refreshControl
    }()

//refreshControl.addTarget(self, action: #selector(handleRefresh), for: .valueChanged)

every PullToRefresh must have couple lines of code like this, that handleRefresh function, do whatever you need to refresh the page.

you just need to comment out addTarget line and add this function to your code ```

func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if scrollView.contentOffset.y < -80 { //change 80 to whatever you want
            if  !self.refreshControl.isRefreshing {
                handleRefresh()
            }
        }
    }



回答3:


You can try this method. When UIRefreshControl is added before UITableView(UIScrollView) is appear , the height of the view may be bigger than the actually height the view display on screen , so the UIRefreshControl pull to refresh range is bigger than actual

let ori = tableView.frame
let temp_frame = CGRect.init(x: ori.origin.x, y: ori.origin.y, width: 
ori.size.width, height: ori.size.height/1.3 )
tableView.frame = temp_frame
tableView.addSubview(UIRefreshControl())
tableView.frame = ori

This code modified the height of view before adding refreshControl , you can adjust the height based on your preference.



来源:https://stackoverflow.com/questions/25699331/how-to-customize-uirefreshcontrol-to-make-pull-down-height-lower-than-default

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!