【问题标题】:WKWebView is cutting off my other view in Xcode 9 & Swift 4.0WKWebView 正在切断我在 Xcode 9 和 Swift 4.0 中的其他视图
【发布时间】:2018-07-20 15:13:21
【问题描述】:

我对使用 Xcode 和 Swift 非常陌生。

我在使用新的 WKWebView 时遇到了问题。我通常使用情节提要,但我了解到 Xcode 9 存在一个错误,如果您进行的构建包含 ios 11 之前的任何内容,则该错误将不允许您使用 WKWebView。我了解到,如果您以编程方式添加它是可能的并一直遵循 Apple 的说明:https://developer.apple.com/documentation/webkit/wkwebview

我需要能够添加 Web 视图,同时在顶部为我创建的用作横幅的自定义视图留出空间。此顶视图的高度为 116,并且在添加 Webview 之前,约束适用于所有设备和方向。

当我添加 Webview 并添加它的自定义大小和约束时,Webview 也可以在所有设备和方向上完美运行。但是,我的顶部横幅视图不见了。

我不确定我遗漏了什么,或者我是否为 Webview 编写了错误的代码。我会将屏幕截图和代码附加到 Webview。

非常感谢您的帮助!

This is the Webview tab that is missing the banner

This is another tab that shows what the banner looks like

import UIKit
import WebKit

class FirstViewController: UIViewController, WKUIDelegate {


var myWebView: WKWebView!

override func loadView() {
    let webConfiguration = WKWebViewConfiguration()
    myWebView = WKWebView(frame: .zero, configuration: webConfiguration)
    myWebView.uiDelegate = self
    view = myWebView

}
override func viewDidLoad() {
    super.viewDidLoad()

    myWebView = WKWebView(frame: CGRect( x: 0, y: 116, width: 414, height: 571), configuration: WKWebViewConfiguration() )
    self.view.addSubview(myWebView)
    myWebView.translatesAutoresizingMaskIntoConstraints = false

    myWebView.topAnchor.constraint(equalTo: view.topAnchor, constant: 116).isActive = true
    myWebView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 49).isActive = true
    myWebView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
    myWebView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0).isActive = true
    myWebView.widthAnchor.constraint(equalToConstant: 414).isActive = true
    myWebView.heightAnchor.constraint(equalToConstant: 571).isActive = true


    let myURL = URL(string: "https://apple.com")
    let myRequest = URLRequest(url: myURL!)
    myWebView.load(myRequest)
}

【问题讨论】:

    标签: ios swift xcode webview wkwebview


    【解决方案1】:

    您的横幅消失只是因为您将 WKWebView 实例放入 ViewController 的 view 属性中,在 loadView 方法的最后一行。尝试将其添加为子视图,甚至在 Interface Builder 中创建 2 个视图,第一个用于您的横幅,第二个用于 Web 视图,您还可以通过 IB 设置约束。

    【讨论】:

      【解决方案2】:

      删除宽度和高度限制,只要您设置了Leading、Trailing、Top 和Bottom 约束,bottom 也应该是-49 而不是49,因为webViewBottom = mainviewBottom - 49

        myWebView.topAnchor.constraint(equalTo: view.topAnchor, constant: 116).isActive = true
        myWebView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -49).isActive = true
        myWebView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
        myWebView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0).isActive = true
      

      【讨论】:

        猜你喜欢
        • 2018-07-02
        • 1970-01-01
        • 2018-07-12
        • 1970-01-01
        • 1970-01-01
        • 2017-11-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多