【发布时间】: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