代码之家  ›  专栏  ›  技术社区  ›  Ogre Stickers

WKWebView正在Xcode 9和Swift 4.0中删除我的其他视图

  •  0
  • Ogre Stickers  · 技术社区  · 8 年前

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

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

    我需要能够添加Webview,同时在顶部为我创建的用作横幅的自定义视图留出空间。此俯视图的高度为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)
    }
    
    2 回复  |  直到 8 年前
        1
  •  0
  •   Artem Garmash    8 年前

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

        2
  •  0
  •   Shehata Gamal    8 年前

    只要您正在设置前导、尾随、顶部和底部约束,就可以删除宽度和高度约束,并且底部应为-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