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

sru*_*ack 2 xcode loading spinner

我是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!)

}

}
Run Code Online (Sandbox Code Playgroud)

//另一个快速文件

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

}
Run Code Online (Sandbox Code Playgroud)

小智 5

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

在此输入图像描述

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

在此输入图像描述

然后,您将收到类似这样的提示

在此输入图像描述

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

在此输入图像描述

从那里,你想要使用线条

activityIndicator.startAnimating()
Run Code Online (Sandbox Code Playgroud)

和

activityIndicator.stopAnimating()
Run Code Online (Sandbox Code Playgroud)

在您想要启动和停止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

}
Run Code Online (Sandbox Code Playgroud)

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

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