简体   繁体   中英

swift 4 refresh Control on top

Hi in my app I have a stretchy header and I am adding my refresh control like this tableView.addSubview(refreshControll)

But when using it, it is in middle or bottom of my view and not pinned to the top how do I fix this?

在此处输入图片说明

Since iOS 10 or iOS 11 UITableView and UICollectionView have gotten their own refresh control option.

var refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: #selector(reloadData), for: UIControl.Event.valueChanged)
tableView.refreshControl = refreshControl

You can also look at the Apple documentation about UITableViewController .

From iOS 10 you have two options to add a refresh control to a UIScrollView: You can add it to the tableView like another refresh control :

tableView.addSubview(refreshControl)

This will show the refreshControl on top of the UITableView, more precisely like if it was added to the tableHeaderView. The other option, is to use the already included refreshControl in the UIScrollView classes:

tableView.refreshControl = UIRefreshControl()

With this option the UIViewController takes control of where to put the refreshControl, since iOS 10 will move it to the top of the view just above the navigation bar. Check Mail app on the iPhone to see and example.

refreshControl.addTarget(self, action: #selector(refresh( sender:)), for: .valueChanged)
tableView.insertSubview(refreshControl, at: 0)

func refresh(sender: UIRefreshControl) {
    sender.beginRefreshing()
    // refresh tableview datasource
    refresh()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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