在viewcontroller中,我正在加载启用了大导航标题的webview(WKWebview)。问题在于,一旦加载了Webview,它就会缩小到正常大小,从而在WebView加载之前完美地显示了较大的导航栏和标题。任何帮助将不胜感激。
预先感谢...!
答案 0 :(得分:0)
Swift4,Swift5:
viewLayoutMarginsDidChange()
我们可以更改navigationBar的高度。viewLayoutMarginsDidChange()
调用以通知视图控制器其根视图的布局边距已更改。每当NavigationBar高度更改时,都会调用此方法。以下代码对我有用
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
}
}
答案 1 :(得分:0)
这个解决方案对我有用,但不是使用
self.navigationController!.navigationBar.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 96.0)
我们可以使用:
self.navigationController?.navigationBar.sizeToFit()