4

I have created a simple webview app. But there is a small problem and I can not fix it. It loads the first page without issue.

enter image description here


When I click to the first input, the program crashes and the error code is below:

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) 

My code is below:

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

I have tried all help that I find on the Internet, but no luck.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
maxileft
  • 225
  • 4
  • 15

3 Answers3

9

Based on the following captures from Reveal, it seems like an Auto Layout issue in Apple’s code. I bet they will fix it at some point.

broken constraint view hierarchy

Jano
  • 62,815
  • 21
  • 164
  • 192
  • Issue persists in 2021 – Waylan Sands Jan 29 '21 at 01:08
  • I've seen many answers that refers to the keyboard, the answer is yes and no. I've been facing this issue for quite a while by now, the issue is not the keyboard its the toolbar that comes with it just as @Jano mentioned, in the following link you will find a hackey workaround to hide that toolbar, once its removed the issue should go away. the answer: https://stackoverflow.com/a/57226420 – mr.xed Aug 05 '23 at 01:14
2

In general you should follow next flow

  1. Add webView as a subview to UIViewController's view (you might have done it in XIB or Storyboard)
  2. Add Autolayout constraints between webView and UIViewController's view in code or in Interface Builder
  3. Also, please remove

    view.translatesAutoresizingMaskIntoConstraints=false

If everything about is OK, please attach your XIB or Storyboard.

Update Here is my ViewController that shows WKWebView only.

    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

        }
    }
Sander
  • 1,325
  • 1
  • 11
  • 30
  • i have tried your suggestion, but no luck again. the first page opens without problem, but wheni click to inpub box, boom. ihave changed the url to another website, the same problem there too, first page opens, but after input box click crashes. And keyboard not showing also. – maxileft Oct 28 '17 at 21:06
  • Can you attach your XIB or Storyboard screenshot. – Sander Oct 28 '17 at 21:09
  • [storyboard screenshot](https://imgur.com/a/JIZTk) . import UIKit import WebKit class ViewController: UIViewController , WKNavigationDelegate{ var webView : WKWebView! override func viewDidLoad() { super.viewDidLoad() let site = "http://parent.e-hism.co/" let url = NSURL(string: site) let request = NSURLRequest(url: url! as URL) webView = WKWebView(frame: self.view.frame) webView.navigationDelegate = self webView.load(request as URLRequest) self.view.addSubview(webView) self.view.sendSubview(toBack: webView) } } – maxileft Oct 28 '17 at 21:16
  • Updated my answer. Included my example of such UIViewController – Sander Oct 28 '17 at 21:51
  • now im gettin error: System group container for systemgroup.com.apple.configurationprofiles path is /Users/maxileft/Library/Developer/CoreSimulator/Devices/D19FA1A9-B940-4E84-8665-1835B61C10B9/data/Containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles 2017-10-29 14:37:47.697976+0400 BFI Schools[76569:4189931] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key webView.' – maxileft Oct 29 '17 at 10:38
  • It means that you have outlet connection in your storyboard called “webView”. So, remove it there. – Sander Oct 29 '17 at 14:20
  • im getting the same error as first, still : Unable to simultaneously satisfy constraints . and all long errors. i have tried from scratch clean project, same issue again – maxileft Oct 30 '17 at 18:22
  • I don't know what to say. Of course you could upload simple project somewhere i.e. Github and I'll look at it there. – Sander Oct 30 '17 at 18:35
2

It might be interesting to note this happens to a lot of people. I have a clean project running, no additional controls and get the same problem: layout constraints when clicking Search (keyboard popup).

It might be indicated as an iOS 11 bug here: WKWebView constrains issue when keyboard pops up

Yasper
  • 501
  • 5
  • 23