如何在Xcode中实现加载微调器

时间:2016-07-16 23:04:41

标签: xcode spinner loading

我是Xcode的新手。有人可以帮我实现加载微调器吗?

1)我需要从网页开始到网页加载结束运行加载微调器

2)另外,我需要在网站/互联网关闭的情况下显示错误提示

import UIKit
import WebKit
class WebView : WKWebView {

/**
 Initialize the WKWebView.
 */
init(){
    let webConfig:WKWebViewConfiguration = WKWebViewConfiguration()
    super.init(frame:CGRectZero,configuration:webConfig)
    self.translatesAutoresizingMaskIntoConstraints = false
    self.allowsBackForwardNavigationGestures = true
    createHomePage()
}

/**
 Set the position for the WKWebView.
 */
func setPosition(view: UIView) {
    self.translatesAutoresizingMaskIntoConstraints = false;
    let height = NSLayoutConstraint(item: self, attribute: .Height, relatedBy: .Equal, toItem: view, attribute: .Height, multiplier: 1, constant: +0)
    let width = NSLayoutConstraint(item: self, attribute: .Width, relatedBy: .Equal, toItem: view, attribute: .Width, multiplier: 1, constant: 0)
    let top = NSLayoutConstraint(item:self,attribute: .Top, relatedBy: .Equal, toItem: view, attribute: .Top, multiplier: 1, constant: 30)
    view.addConstraints([height, width, top])

}

/**
 Set the url the webview should display.
 */
func setUrl(url:String!) {
    if url != nil {
        let url = NSURL(string:url)
        let request = NSURLRequest(URL:url!)
        self.loadRequest(request)
    }
}

/**
 Create the home page.
 */
func createHomePage() {
    let UUID = UIDevice.currentDevice().identifierForVendor?.UUIDString
    self.setUrl("https://mywebsite.com?deviceId="+UUID!)

}

}

//另一个快速文件

import UIKit
import WebKit

class ViewController: UIViewController{

var webView: WebView

required init(coder aDecoder: NSCoder) {
    self.webView = WebView()
    super.init(coder: aDecoder)!
}

override func viewDidLoad() {
    super.viewDidLoad()
    view.addSubview(webView)
    webView.setPosition(view)
    view.backgroundColor = (UIColor(red: 0, green: 150/255, blue: 136/255, alpha: 1));
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

// MARK: Actions

}

1 个答案:

答案 0 :(得分:5)

在故事板文件中,在场景中的所需位置添加UIActivityIndi​​cator。然后,转到UIActivityIndi​​cator的属性检查器并将“Hides When Stopped Check”设置为“checked”或(true)。像这样

enter image description here

然后,创建一个将UIActivityIndi​​cator连接到ViewController.swift类的IBOutlet。您可以通过Ctrl +单击UIActivityIndi​​cator并将该行拖到ViewController.swift类来完成此操作。请注意,下面的拖放图片不是连接UIActivityIndi​​cator,而是连接UITextField。这是因为我刚刚在网上找到了一个图像作为一个例子,因为我无法弄清楚如何在Ctrl +点击拖动时拍摄Gyazo截图。

enter image description here

然后会出现类似这样的提示

enter image description here

只需将它命名为您想要的任何名称,但对于此示例,我将其命名为“activityIndi​​cator”。然后它会在链接项目时指向的行上写下此代码。

enter image description here

从那里开始,你想要使用

activityIndicator.startAnimating()

activityIndicator.stopAnimating()

在您要启动和停止ActivityIndi​​cator动画的位置,指示WebView正在加载以及何时加载完成。根据您提供给我们的代码,我相信您应该将ViewController.swift文件更改为:

import UIKit
import WebKit

class ViewController: UIViewController{

var webView: WebView
@IBOutlet var activityIndicator: UIActivityIndicatorView!

required init(coder aDecoder: NSCoder) { 
    //ViewController has been initialized and will be initializing WebView, so start animating the activityIndicator

    self.activityIndicator.startAnimating()


    self.webView = WebView()
    super.init(coder: aDecoder)!
}

override func viewDidLoad() {
    super.viewDidLoad()
    view.addSubview(webView)
    webView.setPosition(view)
    view.backgroundColor = (UIColor(red: 0, green: 150/255, blue: 136/255,     alpha: 1));

    //When the webView is added successfully to the subview, I believe that it has loaded correctly and the activityIndicator should stop animating.
    activityIndicator.stopAnimating()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

// MARK: Actions

}

至于第二个问题,如果你想在发生错误时提醒,我没有WebKit的经验,我不知道。但是,在我做一些研究并找到答案时,也许其他人可以帮助他。

如果这太深入,我的不好。你说你刚开始编写代码,我从未被教过如何将我的storyboard文件中的元素链接到我的ViewControllers,所以我希望如果你遇到/遇到过这个问题,这将对你有所帮助。

相关问题