如何使用 Webview 框架 = self.view.bounds 使导航栏可见



我的问题是导航栏在我的WebView后面,因为它的框架是self.view.bounds,并且应用程序不显示导航栏。如果我忽略 WebView 框架 = self.view.bounds,我的 WebView 就不适合 iPhone 8 设备模拟的屏幕。

如何使我的 WebView 适合右侧、左侧和底部屏幕两侧,并在顶部显示导航栏???

我尝试修改我的 WebView 内容模式,但没有任何反应...

导航栏工作正常后退和刷新按钮也是如此。问题是使用 myWebView.frame=self.view.bound,使 Web 视图适合屏幕,导航栏隐藏在 myWebView :(后面

我的代码

 override func viewDidLoad() {
    super.viewDidLoad()
    myWebView.delegate = self
    let myURL=URL(string:"https://www.google.com")
    let requestObj = URLRequest(url:myURL!)
    myWebView.loadRequest(requestObj)
    self.view.addSubview(myWebView)
    URLCache.shared.removeAllCachedResponses()
    URLCache.shared.diskCapacity = 0
    URLCache.shared.memoryCapacity = 0

    myWebView.scalesPageToFit = true;
    myWebView.frame=self.view.bounds;

我的故事板图像

谢谢;)

试试这个:

override func viewDidLoad() {
    super.viewDidLoad()
    let navBarHeight: CGFloat = self.navigationController?.navigationBar.frame.height ?? 0.0

    title = "Webview"
    view.backgroundColor = .white
    let webView = UIWebView()
    webView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(webView)
    webView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: navBarHeight).isActive = true
    webView.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
    webView.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true
    webView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
    let request = URLRequest(url: URL(string: "https://apple.com")!)
    webView.loadRequest(request)
}

希望对你有帮助

最后我解决了这个问题。我所做的是:

1.- 选择我的视图控制器

2.- 在菜单上单击编辑器>嵌入> 导航控制器

Xcode 添加了导航控制器

,并在视图控制器上添加了导航项在此导航项上,可以添加栏按钮,例如用作后退按钮或刷新按钮。

做之前的所有事情,我仍然使用:

myWebView.scalesPageToFit = true;
myWebView.frame=self.view.bounds;

我的 WebView 适合使用不同设备的所有屏幕,导航栏非常适合:)

最新更新