ios - 显示键盘时 WKWebView 出现约束问题

标签 ios swift xcode 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.

我现在已经在这上面花了几个小时,但没有任何成功的结果。我已经阅读了多个类似的主题,但没有任何好的答案。我的应用程序运行得很好,但突然不知从哪里出现了这个问题。很乐意提供一些指导

最佳答案

如果不查看 Storyboard ,很难说出您的约束有什么问题。有时,如果您清除所有约束并再次添加它们,考虑哪些元素需要坚持什么,这会有所帮助。更改“View as...”以在 Storyboard 中查看不同屏幕尺寸的 View 有助于可视化您出错的地方。

关于ios - 显示键盘时 WKWebView 出现约束问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54633478/

相关文章:

ios - 是否可以在 High Sierra 上的 Xcode 10 上启用暗模式?

ios - 从自定义相册 PHPhotoLibrary Objective-C 中删除图像

ios - [__NSCFDictionary enumerateObjectsUsingBlock :]: unrecognized selector sent to instance 0x101918a10'

objective-c - 动态给出一个相当于 NSButton 的 Key

ios - 如何在没有硬编码手动计算的情况下使用 UTC 时区偏移量来偏移 NSDate

iphone - MPMoviePlayerController - 控件未对齐

ios - "Communications error"- AVFoundation 相机

ios - 当列数更改时 UICollectionView 崩溃

uitableview - 使 UITableViewCell 的某些区域不可选择

c# - 未知代码片段 - 0xff000000