IOS Wkwebview loading progress bar, navigation bar back & Close (Swift 4)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.