加载 webview 时,带有大标题的导航栏会缩小 [英] Navigation bar with large title shrinks when webview is loaded

查看:33
本文介绍了加载 webview 时,带有大标题的导航栏会缩小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在视图控制器中,我正在加载一个启用了大导航标题的 webview(WKWebview).问题是它在 webview 加载之前完美地显示了大导航栏和标题,一旦 webview 被加载它就会缩小到正常.任何帮助将不胜感激.

In viewcontroller, I'm loading a webview(WKWebview) with large navigation title enabled. The problem is its showing the large navigation bar and title perfectly before the webview gets loaded once the webview got loaded it shrinks to normal. Anyhelp would be appreciated.

提前致谢...!

推荐答案

Swift4、Swift5:

Swift4, Swift5 :

  • 这个问题的原因是当您的 webView 开始加载页面时,您的导航栏高度发生了变化.所以使用 viewLayoutMarginsDidChange() 我们可以改变导航栏的高度.
  • viewLayoutMarginsDidChange() 调用以通知视图控制器其根视图的布局边距已更改.每次导航栏高度发生变化时都会调用此方法.
    • The reason of this issue is your navigationBar height is changing when you webView starts loading the page. so using viewLayoutMarginsDidChange() we can change navigationBar height.
    • viewLayoutMarginsDidChange() Called to notify the view controller that the layout margins of its root view changed. This method will get called every time when navigationBar height changes.
    • 下面的代码对我有用

      import WebKit
      
      class ViewController: UIViewController, WKNavigationDelegate {
      
          private var webView: WKWebView!
          var didChange = false //Set true when we have to update navigationBar height in viewLayoutMarginsDidChange()
      
          override func viewLayoutMarginsDidChange() {
              if didChange {
                  print("Height : - \(self.navigationController?.navigationBar.frame.size.height)")
              // set NavigationBar Height here    
                 self.navigationController!.navigationBar.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 96.0)
                  didChange.toggle() 
                  }
              }
      
          override func viewDidLoad() {
              super.viewDidLoad()
      
          let appearance = UIBarButtonItem.appearance()
          appearance.setBackButtonTitlePositionAdjustment(UIOffset.init(horizontal: 0.0, vertical: -60), for: .default)
      
          self.navigationItem.title = "WebView"
          self.navigationController?.navigationBar.isTranslucent = true
          self.navigationController?.navigationBar.tintColor = UIColor.black
          self.navigationController?.navigationBar.prefersLargeTitles = true
      
          webView = WKWebView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height))
          webView.navigationDelegate = self
          view = webView
      
      
          let myURL = URL(string:"https://google.com")
          let myRequest = URLRequest(url: myURL!)
          webView.load(myRequest)
      
          }
      
      
          func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
              print("Did Start")
            //webView page starts loading
              didChange = true
          }
      
      }
      

      这篇关于加载 webview 时,带有大标题的导航栏会缩小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆