Using Uiactivityindicatorview with Uiwebview in Swift

Add an activity indicator on a uiwebview

How do I make the activity indicator stop only when the web page appears?

Your object can become delegate for webView to listen to -webViewDidFinishLoad delegate method. So:

- (void)viewDidLoad {
// Create the UIWebView
UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
webView.delegate = self; // Here is the key
...
}

...

- (void)webViewDidFinishLoad:(UIWebView *)webView {
[self.view viewWithTag:100].hidden = YES;
}

You may also implement -webViewDidStartLoad: to show activity indicator instead of showing it in -viewDidLoad method.

How do I add words to the activity indicator like "Loading"?

You should create a separate UILabel, there is no way to add text to standard UIActivityIndicatorView

How to add Activity Indicator to WKWebView (Swift 3)

Please, below code which is working fine.

@IBOutlet weak var Activity: UIActivityIndicatorView!
var webView : WKWebView!
@IBOutlet var containerView: UIView? = nil

override func viewDidLoad() {
super.viewDidLoad()

guard let url = URL(string: "http://www.facebook.com") else { return }
webView = WKWebView(frame: self.view.frame)
webView.translatesAutoresizingMaskIntoConstraints = false
webView.isUserInteractionEnabled = true
self.view.addSubview(self.webView)
let request = URLRequest(url: url)
webView.load(request)

// add activity
self.webView.addSubview(self.Activity)
self.Activity.startAnimating()
self.webView.navigationDelegate = self
self.Activity.hidesWhenStopped = true

}

Implement below these two delegate method:

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
Activity.stopAnimating()
}

func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
Activity.stopAnimating()
}

Let me know if it is not working.

UIActivityIndicatorView not showing in webview

Result:
Sample Image

Code:

class ViewController: UIViewController {

var loadSpinner: UIActivityIndicatorView!
var webView: UIWebView!

override func viewDidLoad() {
super.viewDidLoad()

self.webView = UIWebView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))

loadSpinner = UIActivityIndicatorView(activityIndicatorStyle: .gray)

webView.addSubview(loadSpinner)

view = webView

loadSpinner.startAnimating()
}

override func viewDidLayoutSubviews() {
loadSpinner.center = webView.center
}
}


Related Topics



Leave a reply



Submit