WKWebView LayoutConstraints问题 [英] WKWebView LayoutConstraints issue
问题描述
我创建了一个简单的Webview应用程序.但是有一个小问题,我无法解决.它加载第一页没有问题.
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.
当我单击第一个输入时,程序崩溃,错误代码如下:
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)
我的代码如下:
//
// 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()
}
}
我尝试了所有可以在Internet上找到的帮助,但是没有运气.
I have tried all help that I find on the Internet, but no luck.
推荐答案
通常,您应该遵循下一个流程
In general you should follow next flow
- 将webView作为子视图添加到
UIViewController
的视图中(您可能会 已经在XIB或Storyboard中完成了 - 在以下位置的webView和
UIViewController
的视图之间添加自动布局约束 代码或在Interface Builder中 -
另外,请删除
- Add webView as a subview to
UIViewController
's view (you might have done it in XIB or Storyboard) - Add Autolayout constraints between webView and
UIViewController
's view in code or in Interface Builder Also, please remove
view.translatesAutoresizingMaskIntoConstraints=false
如果一切正常,请附加您的XIB或情节提要.
If everything about is OK, please attach your XIB or Storyboard.
更新 这是我的ViewController,仅显示WKWebView.
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
}
}
这篇关于WKWebView LayoutConstraints问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!