Navigation:
1. Loading the progress bar
2. Navigation bar add back, Close button
Load progress bar
The code is as follows:
Self.progressView.trackTintColor = UIColor.whiteself.progressView.progressTintColor = Uicolor.colorwithhex (value: 0x72a438, alpha:1). Withalphacomponent (0.8) Self.progressView.frame = Cgrect.init (x:0, y:0, Width: UIScreen.main.bounds.width, height:1) Self.progressView.transform = Cgaffinetransform.init (scalex:1, Y:3) Self.view.addSubview (self.progressview) self.progressView.progress = 0.05//Set initial value to prevent Web page from loading too slowly without progress//Swift 4 Kvoself.observeblock = Webview.observe (\.estimatedprogress, Options:NSKeyValueObservingOptions.new.union ( Nskeyvalueobservingoptions.old)) {(WebView, changed) in if let new = changed.newvalue{ Self.progressView.isHidden = False self.progressView.progress = Float (new) if new >= 1.0{ delay (second : 0.3, Block: { Self.progressView.isHidden = True }) } }}
Load Failure hidden progress bar Func WebView (_ Webview:wkwebview, Didfail navigation:wknavigation!, Witherror error:error) { Self.prog Ressview.ishidden = true}
Deferred execution function:
Func delay (Second:double,block: @escaping ((), Void)) { DispatchQueue.main.asyncAfter (deadline:. Now () + Second, execute: { block () })}
Navigation bar add back, Close button
The code is as follows:
Func Showleftnavigationitem () {let gobackbtn = Uibutton.init () let closebtn = Uibutton.init () Gobackbtn.setimage (Uiimage.init (named: "Navi_go_back"), For:UIControlState.normal) gobackbtn.settitle ("Back", For:UIControlState.normal) gobackbtn.addtarget (Self, Action: #selector (GoBack), for: Uicontrolevents.touchupinside) gobackbtn.sizetofit () gobackbtn.contentedgeinsets = UIEdgeInsetsMake (0,-8 , 0, 8) let Backitem = Uibarbuttonitem.init (customview:gobackbtn) closebtn.settitle ("Off", for: Uicontrolstate.normal) closebtn.addtarget (Self, Action: #selector (Popviewcontroller), for: Uicontrolevents.touchupinside) Closebtn.sizetofit () let closeitem = Uibarbuttonitem.init (customView: CLOSEBTN) let items:[uibarbuttonitem] = [Backitem,closeitem] self.navigationItem.leftBarButtonItems = Items
button corresponding to the selector:
@objc func goBack () {self.webView.goBack ()} @objc func Popviewcontroller () { self.navigationcontroller?. Popviewcontroller (Animated:true)}
When loading is complete, decide whether to display the two buttons:
Func WebView (_ Webview:wkwebview, Didfinish navigation:wknavigation!) { checkgoback ()}func WebView (_ Webview:wkwebview, Didfail navigation:wknavigation!, Witherror error:error) { Checkgoback ()}///check Returns (Pop/goback) func checkgoback () { self.navigationcontroller?. Interactivepopgesturerecognizer?. IsEnabled =!self.webview.cangoback if self.webview.cangoback{ showleftnavigationitem () }else{ Self.navigationItem.leftBarButtonItems = Nil }}
White back arrow Picture:
Save 2x at right-click: () 3x: ()
Ficow Original, reproduced please specify the source:http://www.cnblogs.com/ficow/p/7639912.html
IOS Wkwebview loading progress bar, navigation bar back & Close (Swift 4)