您可以为 Swift 中的 UIRefreshControl 创建自定义视图吗?

Can you make a custom view for a UIRefreshControl in Swift?

我想将 custom Activity Indicator 放入视图中,并将其放入下拉刷新视图中。此下拉刷新专用于 table 视图。我尝试了什么....

    var headerView = UIView(frame: self.refreshControl.bounds)
    headerView.addSubview(activityIndicator)
    headerView.backgroundColor = UIColor.clear
    headerView.frame = refreshControl.bounds
    headerView.contentMode = .scaleAspectFit
    headerView.translatesAutoresizingMaskIntoConstraints = false
    refreshControl.tintColor = .clear
    refreshControl.addSubview(headerView)

是的,您可以通过调用 addSubview 方法将自定义视图添加到刷新控件。

// Outlet of table view
@IBOutlet weak var tbl      : UITableView!

// Create global variable of NVActivityIndicatorView
var nvActivityIndicator     : NVActivityIndicatorView?

override func viewDidLoad() {
    super.viewDidLoad()

    // Create refresh control and add as subview in your table view.
    let refreshControl = UIRefreshControl()
    refreshControl.backgroundColor = .red
    refreshControl.addTarget(self, action: #selector(pullToRefresh), for: .valueChanged)
    tbl.addSubview(refreshControl)

    // Create NVActivityIndicator view and add as subview inside refresh control
    nvActivityIndicator = NVActivityIndicatorView(frame: refreshControl.bounds, type: NVActivityIndicatorType.ballRotate, color: .blue, padding: 0)
    nvActivityIndicator?.backgroundColor = refreshControl.backgroundColor
    nvActivityIndicator?.translatesAutoresizingMaskIntoConstraints = false

    refreshControl.addSubview(nvActivityIndicator!)

    // Add constraint to auto resize nvActivityIndicator as per refresh control view.
    let nvActivityIndicator_top = NSLayoutConstraint(item: nvActivityIndicator!, attribute: .top, relatedBy: .equal, toItem: refreshControl, attribute: .top, multiplier: 1, constant: 0)
    let nvActivityIndicator_bottom = NSLayoutConstraint(item: nvActivityIndicator!, attribute: .bottom, relatedBy: .equal, toItem: refreshControl, attribute: .bottom, multiplier: 1, constant: 0)
    let nvActivityIndicator_leading = NSLayoutConstraint(item: nvActivityIndicator!, attribute: .leading, relatedBy: .equal, toItem: refreshControl, attribute: .leading, multiplier: 1, constant: 0)
    let nvActivityIndicator_trailing = NSLayoutConstraint(item: nvActivityIndicator!, attribute: .trailing, relatedBy: .equal, toItem: refreshControl, attribute: .trailing, multiplier: 1, constant: 0)

    // Add constrains
    refreshControl.addConstraint(nvActivityIndicator_top)
    refreshControl.addConstraint(nvActivityIndicator_bottom)
    refreshControl.addConstraint(nvActivityIndicator_leading)
    refreshControl.addConstraint(nvActivityIndicator_trailing)

    // Set custom view background colour as per refreshControl background colour
    refreshControl.tintColor = refreshControl.backgroundColor
}

@objc func pullToRefresh() {
    // Start animation here.
    nvActivityIndicator?.startAnimating()
}

希望对您有所帮助。