【问题标题】:WKWebview Swift 4 keeps full screening Xcode 9WKWebview Swift 4 保持全屏 Xcode 9
【发布时间】:2018-07-02 19:36:37
【问题描述】:

我有一个带有 web 视图的 swift 4 项目我想将 WKWebview 保持在这个大小:

https://i.stack.imgur.com/3mKVb.png

不幸的是,webview 一直显示为全屏窗口并且不符合大小。

这是我正在使用的代码: 导入 UIKit 导入 WebKit 类 ViewController: UIViewController, WKUIDelegate {

@IBOutlet var webView: WKWebView!
override func loadView() {
    let webConfiguration = WKWebViewConfiguration()
    webView = WKWebView(frame: .zero, configuration: webConfiguration)
    webView.uiDelegate = self
    view = webView
}
override func viewDidLoad() {
    super.viewDidLoad()
    let myURL = URL(string: "https://www.apple.com")
    let myRequest = URLRequest(url: myURL!)
    webView.load(myRequest)
}}

【问题讨论】:

    标签: ios swift xcode


    【解决方案1】:

    这是我用于编程 WKWebView 以设置约束的代码:

    override func viewDidLoad() {
        super.viewDidLoad()
    
        let webConfiguration = WKWebViewConfiguration()
        webConfiguration.websiteDataStore = WKWebsiteDataStore.default()
        let webView = WKWebView(frame: .zero, configuration: webConfiguration)
    
        webView.navigationDelegate = self
        webView.uiDelegate = self
    
        self.webView = webView
    
        if let webView = self.webView
        {
            view.addSubview(webView)
            webView.translatesAutoresizingMaskIntoConstraints = false
            let height = NSLayoutConstraint(item: webView, attribute: .height, relatedBy: .equal, toItem: self.contentView, attribute: .height, multiplier: 1, constant: 0)
            let width = NSLayoutConstraint(item: webView, attribute: .width, relatedBy: .equal, toItem: self.contentView, attribute: .width, multiplier: 1, constant: 0)
            let offset = NSLayoutConstraint(item: webView, attribute: .top, relatedBy: .equal, toItem: self.contentView, attribute: .top, multiplier: 1, constant: 0)
            view.addConstraints([height, width, offset])
        }
    }
    

    这与您所做的不同,因为它设置了自动布局约束。在这种情况下,我有一个名为 contentView 的视图,它是我的故事板中的一个简单 UIView,它被固定到超级视图:

    【讨论】:

      猜你喜欢
      • 2018-07-12
      • 1970-01-01
      • 1970-01-01
      • 2018-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-06
      • 2019-03-30
      相关资源
      最近更新 更多