显示键盘时,WKWebView会出现约束问题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了显示键盘时,WKWebView会出现约束问题相关的知识,希望对你有一定的参考价值。
当关注来自WKWebView的输入字段时,键盘在提示时不起作用,我想知道如何设置约束以避免此问题
该页面加载得很好。在页面上,有一个输入字段。当我单击输入字段时,键盘会弹出,但不再设置对该字段的焦点,也无法输入任何内容。 xcode提供以下错误日志:
override func viewDidLoad() {
super.viewDidLoad()
setupWebView()
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
self.showWebView()
}
func showWebView() {
webView.frame = view.frame
webView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(webView)
view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[webView]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["webView":webView]))
view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-20-[webView]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["webView":webView]))
}
func setupWebView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame:.zero, configuration: webConfiguration)
webView.uiDelegate = self
webView?.navigationDelegate = self
//Disable scroll of WebView
webView.scrollView.bounces = false
webView.translatesAutoresizingMaskIntoConstraints = false
let url = URL(string: self.url)
let request = URLRequest(url: url!)
print(request)
webView.load(request)
}
错误日志:
2019-02-11 15:53:46.940718+0100 AppName[9824:4549339] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
2019-02-11 15:53:46.943297+0100 AppName[9824:4549339] [MC] Reading from public effective user settings.
2019-02-11 15:53:47.085142+0100 AppName[9824:4549339] API error: <_UIKBCompatInputView: 0x113d03880; frame = (0 0; 0 0); layer = <CALayer: 0x28017c800>> returned 0 width, assuming UIViewNoIntrinsicMetric
2019-02-11 15:53:47.085268+0100 AppName[9824:4549339] API error: <_UIKBCompatInputView: 0x113d03880; frame = (0 0; 0 0); layer = <CALayer: 0x28017c800>> returned 0 width, assuming UIViewNoIntrinsicMetric
2019-02-11 15:53:47.123997+0100 AppName[9824:4549339] [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)
(
"<NSAutoresizingMaskLayoutConstraint:0x282275ae0 h=-&- v=-&- _UIToolbarContentView:0x113d3d9e0.width == UIToolbar:0x113d3d5a0.width (active)>",
"<NSLayoutConstraint:0x282262bc0 H:|-(0)-[_UIButtonBarStackView:0x113d3f970] (active, names: '|':_UIToolbarContentView:0x113d3d9e0 )>",
"<NSLayoutConstraint:0x2822637a0 _UIButtonBarStackView:0x113d3f970.trailing == _UIToolbarContentView:0x113d3d9e0.trailing (active)>",
"<NSLayoutConstraint:0x282273cf0 H:|-(16)-[_UIModernBarButton:0x113d54b00] (active, names: '|':_UIButtonBarButton:0x113d546b0 )>",
"<NSLayoutConstraint:0x282273d40 H:[_UIModernBarButton:0x113d54b00]-(>=8)-| (active, names: '|':_UIButtonBarButton:0x113d546b0 )>",
"<NSLayoutConstraint:0x282278320 H:|-(>=5)-[_UIModernBarButton:0x113d56e30] (active, names: '|':_UIButtonBarButton:0x113d567e0 )>",
"<NSLayoutConstraint:0x282278370 H:[_UIModernBarButton:0x113d56e30]-(>=5)-| (active, names: '|':_UIButtonBarButton:0x113d567e0 )>",
"<NSLayoutConstraint:0x282278dc0 H:|-(8)-[_UIModernBarButton:0x113d57ee0'Done'] (active, names: '|':_UIButtonBarButton:0x113d57350 )>",
"<NSLayoutConstraint:0x282278e10 H:[_UIModernBarButton:0x113d57ee0'Done']-(16)-| (active, names: '|':_UIButtonBarButton:0x113d57350 )>",
"<NSLayoutConstraint:0x282275090 'UISV-canvas-connection' UILayoutGuide:0x2838457a0'UIViewLayoutMarginsGuide'.leading == _UIButtonBarButton:0x113d546b0.leading (active)>",
"<NSLayoutConstraint:0x2822750e0 'UISV-canvas-connection' UILayoutGuide:0x2838457a0'UIViewLayoutMarginsGuide'.trailing == _UIButtonBarButton:0x113d57350.trailing (active)>",
"<NSLayoutConstraint:0x282275130 'UISV-spacing' H:[_UIButtonBarButton:0x113d546b0]-(0)-[UIView:0x113d56600] (active)>",
"<NSLayoutConstraint:0x282275180 'UISV-spacing' H:[UIView:0x113d56600]-(0)-[_UIButtonBarButton:0x113d567e0] (active)>",
"<NSLayoutConstraint:0x2822751d0 'UISV-spacing' H:[_UIButtonBarButton:0x113d567e0]-(0)-[UIView:0x113d57170] (active)>",
"<NSLayoutConstraint:0x282275220 'UISV-spacing' H:[UIView:0x113d57170]-(0)-[_UIButtonBarButton:0x113d57350] (active)>",
"<NSLayoutConstraint:0x282275c70 'UIView-Encapsulated-Layout-Width' UIToolbar:0x113d3d5a0.width == 0 (active)>",
"<NSLayoutConstraint:0x2822699f0 'UIView-leftMargin-guide-constraint' H:|-(0)-[UILayoutGuide:0x2838457a0'UIViewLayoutMarginsGuide'](LTR) (active, names: '|':_UIButtonBarStackView:0x113d3f970 )>",
"<NSLayoutConstraint:0x282269a90 'UIView-rightMargin-guide-constraint' H:[UILayoutGuide:0x2838457a0'UIViewLayoutMarginsGuide']-(0)-|(LTR) (active, names: '|':_UIButtonBarStackView:0x113d3f970 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x282273d40 H:[_UIModernBarButton:0x113d54b00]-(>=8)-| (active, names: '|':_UIButtonBarButton:0x113d546b0 )>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
2019-02-11 15:53:47.134917+0100 AppName[9824:4549339] [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)
(
"<NSAutoresizingMaskLayoutConstraint:0x282275ae0 h=-&- v=-&- _UIToolbarContentView:0x113d3d9e0.width == UIToolbar:0x113d3d5a0.width (active)>",
"<NSLayoutConstraint:0x282262bc0 H:|-(0)-[_UIButtonBarStackView:0x113d3f970] (active, names: '|':_UIToolbarContentView:0x113d3d9e0 )>",
"<NSLayoutConstraint:0x2822637a0 _UIButtonBarStackView:0x113d3f970.trailing == _UIToolbarContentView:0x113d3d9e0.trailing (active)>",
"<NSLayoutConstraint:0x282278320 H:|-(>=5)-[_UIModernBarButton:0x113d56e30] (active, names: '|':_UIButtonBarButton:0x113d567e0 )>",
"<NSLayoutConstraint:0x282278370 H:[_UIModernBarButton:0x113d56e30]-(>=5)-| (active, names: '|':_UIButtonBarButton:0x113d567e0 )>",
"<NSLayoutConstraint:0x282278dc0 H:|-(8)-[_UIModernBarButton:0x113d57ee0'Done'] (active, names: '|':_UIButtonBarButton:0x113d57350 )>",
"<NSLayoutConstraint:0x282278e10 H:[_UIModernBarButton:0x113d57ee0'Done']-(16)-| (active, names: '|':_UIButtonBarButton:0x113d57350 )>",
"<NSLayoutConstraint:0x282275090 'UISV-canvas-connection' UILayoutGuide:0x2838457a0'UIViewLayoutMarginsGuide'.leading == _UIButtonBarButton:0x113d546b0.leading (active)>",
"<NSLayoutConstraint:0x2822750e0 'UISV-canvas-connection' UILayoutGuide:0x2838457a0'UIViewLayoutMarginsGuide'.trailing == _UIButtonBarButton:0x113d57350.trailing (active)>",
"<NSLayoutConstraint:0x282275130 'UISV-spacing' H:[_UIButtonBarButton:0x113d546b0]-(0)-[UIView:0x113d56600] (active)>",
"<NSLayoutConstraint:0x282275180 'UISV-spacing' H:[UIView:0x113d56600]-(0)-[_UIButtonBarButton:0x113d567e0] (active)>",
"<NSLayoutConstraint:0x2822751d0 'UISV-spacing' H:[_UIButtonBarButton:0x113d567e0]-(0)-[UIView:0x113d57170] (active)>",
"<NSLayoutConstraint:0x282275220 'UISV-spacing' H:[UIView:0x113d57170]-(0)-[_UIButtonBarButton:0x113d57350] (active)>",
"<NSLayoutConstraint:0x282275c70 'UIView-Encapsulated-Layout-Width' UIToolbar:0x113d3d5a0.width == 0 (active)>",
"<NSLayoutConstraint:0x2822699f0 'UIView-leftMargin-guide-constraint' H:|-(0)-[UILayoutGuide:0x2838457a0'UIViewLayoutMarginsGuide'](LTR) (active, names: '|':_UIButtonBarStackView:0x113d3f970 )>",
"<NSLayoutConstraint:0x282269a90 'UIView-rightMargin-guide-constraint' H:[UILayoutGuide:0x2838457a0'UIViewLayoutMarginsGuide']-(0)-|(LTR) (active, names: '|':_UIButtonBarStackView:0x113d3f970 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x282278370 H:[_UIModernBarButton:0x113d56e30]-(>=5)-| (active, names: '|':_UIButtonBarButton:0x113d567e0 )>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
我现在花了好几个小时没有任何成功的结果。我已经读过多个类似的线程而没有任何好的答案。我的应用程序工作得很好,但突然间这个问题无处可去。很高兴能得到一些指导
答案
如果不看故事板,很难说出你的约束有什么问题。有时如果你清除所有约束并再次添加它们会有所帮助,想一想哪个元素需要坚持什么。更改“查看为...”以在故事板中查看不同屏幕尺寸的视图有助于可视化您出错的位置。
以上是关于显示键盘时,WKWebView会出现约束问题的主要内容,如果未能解决你的问题,请参考以下文章
Cordova ios WKWebView第一个touchstart事件在键盘启动时没有触发
SwiftUI 软件键盘 + 两个 TextFields = 约束冲突