ios WKWebView LayoutConstraints问题

jpfvwuh4  于 2023-08-08  发布在  iOS
关注(0)|答案(3)|浏览(124)

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


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

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()
    }
}


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

am46iovg

am46iovg1#

根据Reveal的以下捕获,这似乎是Apple代码中的自动布局问题。我打赌他们会在某个时候修好它。
100d1x

的字符串

ujv3wf0j

ujv3wf0j2#

一般来说,你应该遵循下一个流程
1.将webView作为子视图添加到UIViewController的视图中(您可能已经在XIB或Storyboard中完成了)
1.在代码或Interface Builder中添加webView和UIViewController视图之间的自动布局约束
1.另外,请删除
view.translatesAutoresizingMaskIntoConstraints=false
如果一切正常,请附上您的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

        }
    }

字符串

z9smfwbn

z9smfwbn3#

这可能是有趣的注意到这种情况发生在很多人身上。我有一个干净的项目运行,没有额外的控制和得到同样的问题:单击“搜索”(键盘弹出)时的布局约束。
这可能是iOS 11的bug:WKWebView constrains issue when keyboard pops up

相关问题