繁体   English   中英

您可以在Swift中为UIRefreshControl制作自定义视图吗?

[英]Can you make a custom view for a UIRefreshControl in Swift?

我想在视图中放置自定义活动指示器,然后将其放置在下拉菜单中以刷新视图。 此刷新刷新专门用于表视图。 我试过了...

    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()
}

我希望这能帮到您。

暂无
暂无

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

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