WKWebView LayoutConstraints问题

时间:2017-10-28 19:57:43

标签: ios swift uiwebview autolayout

我创建了一个简单的webview应用程序。但是有一个小问题,我无法解决它。它加载第一页没有问题。

enter image description here


当我点击第一个输入时,程序崩溃,错误代码如下:

2017-10-28 23:50:54.289690+0400 BFI Schools[68425:3885613] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. 
Try this: 
    (1) look at each constraint and try to figure out which you don't expect; 
    (2) find the code that added the unwanted constraint or constraints and fix it. 
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 

我的代码如下:

//
//  ViewController.swift
//  BFI Schools
//
//  Created by Kamandar Abdullayev on 10/28/17.
//  Copyright © 2017 ROOM404.AZ. All rights reserved.
//

import UIKit
import WebKit

class ViewController: UIViewController , UIWebViewDelegate {
    @IBOutlet weak var webView: UIWebView!
    @IBOutlet weak var spinner: UIActivityIndicatorView!

    override func viewDidLoad() {
        super.viewDidLoad()

        view.translatesAutoresizingMaskIntoConstraints=false

        let url = URL(string: "https://www.parent.e-hism.co")!
        let request = URLRequest(url: url)

        if Reachability.isConnectedToNetwork() == true {
            webView.loadRequest(request)
        } else {
            let alert = UIAlertView(title: "No Internet Connection", message: "Make sure your device is connected to the internet.", delegate: nil, cancelButtonTitle: "OK")
            alert.show()
        }
    }

    func webViewDidStartLoad(_ : UIWebView) {
        spinner.startAnimating()
    }

    func webViewDidFinishLoad(_ : UIWebView) {
        spinner.stopAnimating()
    }
}

我已经尝试过在互联网上找到的所有帮助,但没有运气。

3 个答案:

答案 0 :(得分:1)

一般来说,你应该遵循下一个流程

  1. 将webView作为子视图添加到UIViewController的视图中(您可以 已在XIB或Storyboard中完成)
  2. 在webView和UIViewController的视图中添加Autolayout约束 代码或在Interface Builder中
  3. 另外,请删除

    view.translatesAutoresizingMaskIntoConstraints=false

  4. 如果一切正常,请附上您的XIB或故事板。

    <强>更新 这是我的ViewController,仅显示WKWebView。

        class ViewController: UIViewController {
    
            lazy var webView: WKWebView = {
                let webView = WKWebView(frame: .zero)
                webView.translatesAutoresizingMaskIntoConstraints = false
                return webView
            }()
    
            override func viewDidLoad() {
                super.viewDidLoad()
    
                // layout code
                view.addSubview(webView)
                view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[webView]|", options: [], metrics: nil, views: ["webView": webView]))
                view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[webView]|", options: [], metrics: nil, views: ["webView": webView]))
    
                // and then code to load request
    
            }
        }
    

答案 1 :(得分:1)

值得注意的是,很多人都会遇到这种情况。我有一个干净的项目正在运行,没有其他控件并且遇到同样的问题:点击搜索(键盘弹出窗口)时的布局限制。

这可能表示为iOS 11错误:WKWebView constrains issue when keyboard pops up

答案 2 :(得分:1)

根据以下来自Reveal的记录,这似乎是Apple代码中的自动版式问题。我敢打赌他们会在某个时候解决它。

broken constraint view hierarchy