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:
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
Arkit - How to Put 3D Object on Qrcode
How to Alloc/Dealloc Unsafe Pointers in Swift
Scenekit -- How to Get Animations for a .Dae Model
How to Quit Swift Repl Without Using Ctrl-D
Storyboard Entry Point Missing
Present Actionsheet in Swiftui on iPad
How to Simulate Traits/Mixins in Swift
How to Avoid Using Anypublisher/Erasetoanypublisher All Over the Place
How to Recognize Continuous Touch in Swift
Nsuserdefaults in Swift - Implementing Type Safety
How to Test That Statictexts Contains a String Using Xctest
How to Use Swift Package Manager in Xcode 9 Playground
Using Cocoa Nssavepanel in Sandbox Causes Assertion Failure
In Swift, When Using "[Weak Self] In", Should I Double Up on It When Nested Inside Another Closure
Get a Unique String for a Given Anyobject