【问题标题】:Swift: Custom action for UIRefreshControl in WKWebViewSwift:WKWebView 中 UIRefreshControl 的自定义操作
【发布时间】:2020-06-10 14:53:05
【问题描述】:

我使用的是最新的XcodeSwift 版本。

我正在使用WKWebView 显示来自我的网络服务器的内容。

我正在使用以下代码pull to refresh

let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: #selector(refreshWebView(_:)), for: UIControl.Event.valueChanged)
homeWebView.scrollView.addSubview(refreshControl)

由于刷新似乎与(重新)加载 URL 不同,这会导致一些意外行为。此外,它不会触发func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {

有什么方法可以catch pull to refresh 函数并制作一些自定义的东西,例如正在加载另一个网址?

【问题讨论】:

    标签: swift xcode uirefreshcontrol


    【解决方案1】:

    您可以将Selector 中的操作更改为您传递给自定义操作的操作,方法如下:

    refreshControl.addTarget(self, action: #selector(myCustomAction), for: UIControl.Event.valueChanged)
    

    行动:

    @objc func myCustomAction() {
        print("my custom action here...")
    }
    

    【讨论】:

      【解决方案2】:

      Swift 4 和 Swift 5

      lazy var refreshControl: UIRefreshControl = {
          let ref = UIRefreshControl()
          ref.attributedTitle = NSAttributedString(string: "Pull to refresh")
          ref.tintColor = UIColor.red
          ref.addTarget(self, action: #selector(refreshData), for: UIControl.Event.valueChanged)
          return ref
      }()
      
        override func viewDidLoad() {
              super.viewDidLoad()
              myWebView.scrollView.addSubview(refreshControl) 
          }
      
      @objc func refreshData(){
          //TODO:- Do whatever you want 
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-31
        • 2012-06-11
        • 2017-11-09
        • 1970-01-01
        • 1970-01-01
        • 2018-09-28
        相关资源
        最近更新 更多