ios - AutoLayout - 无法同时满足 swift5 中的约束条件

标签 ios swift storyboard

当您按下 Web View 侧边栏上的按钮时,我的问题出现了。该按钮向 WebView 发送一条简单消息并复制剪贴板。 Swift 将收到的消息显示为 toast 。非常简单。

功能看起来没有问题,

但是UI是个问题。当按下按钮时,屏幕从上到下滑动,显示 toast ,再次按下按钮激活按下侧边栏其他按钮的功能。

画面是一样的,不过画面好像是down了。侧边栏屏幕包含一个滚动条。是这个意思吗?

错误日志:

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:0x283a866c0 h=-&- v=-&- _UIToolbarContentView:0x133ebf3b0.width == UIToolbar:0x133ebef70.width   (active)>",
    "<NSLayoutConstraint:0x283a8b390 H:|-(0)-[_UIButtonBarStackView:0x133ec0570]   (active, names: '|':_UIToolbarContentView:0x133ebf3b0 )>",
    "<NSLayoutConstraint:0x283a8b3e0 _UIButtonBarStackView:0x133ec0570.trailing == _UIToolbarContentView:0x133ebf3b0.trailing   (active)>",
    "<NSLayoutConstraint:0x283a84910 H:|-(16)-[_UIModernBarButton:0x133dcc420]   (active, names: '|':_UIButtonBarButton:0x133dcc1d0 )>",
    "<NSLayoutConstraint:0x283a84960 H:[_UIModernBarButton:0x133dcc420]-(>=8)-|   (active, names: '|':_UIButtonBarButton:0x133dcc1d0 )>",
    "<NSLayoutConstraint:0x283a84f50 H:|-(>=5)-[_UIModernBarButton:0x133ecbe60]   (active, names: '|':_UIButtonBarButton:0x133ecbc10 )>",
    "<NSLayoutConstraint:0x283a84fa0 H:[_UIModernBarButton:0x133ecbe60]-(>=5)-|   (active, names: '|':_UIButtonBarButton:0x133ecbc10 )>",
    "<NSLayoutConstraint:0x283a856d0 H:|-(8)-[_UIModernBarButton:0x133dcd540'Done']   (active, names: '|':_UIButtonBarButton:0x133ecc380 )>",
    "<NSLayoutConstraint:0x283a85720 H:[_UIModernBarButton:0x133dcd540'Done']-(16)-|   (active, names: '|':_UIButtonBarButton:0x133ecc380 )>",
    "<NSLayoutConstraint:0x283a85ae0 'UISV-canvas-connection' UILayoutGuide:0x2820c1c00'UIViewLayoutMarginsGuide'.leading == _UIButtonBarButton:0x133dcc1d0.leading   (active)>",
    "<NSLayoutConstraint:0x283a85cc0 'UISV-canvas-connection' UILayoutGuide:0x2820c1c00'UIViewLayoutMarginsGuide'.trailing == _UIButtonBarButton:0x133ecc380.trailing   (active)>",
    "<NSLayoutConstraint:0x283a85d10 'UISV-spacing' H:[_UIButtonBarButton:0x133dcc1d0]-(0)-[UIView:0x133e94d90]   (active)>",
    "<NSLayoutConstraint:0x283a85d60 'UISV-spacing' H:[UIView:0x133e94d90]-(0)-[_UIButtonBarButton:0x133ecbc10]   (active)>",
    "<NSLayoutConstraint:0x283a85db0 'UISV-spacing' H:[_UIButtonBarButton:0x133ecbc10]-(0)-[UIView:0x133ecc1a0]   (active)>",
    "<NSLayoutConstraint:0x283a85e00 'UISV-spacing' H:[UIView:0x133ecc1a0]-(0)-[_UIButtonBarButton:0x133ecc380]   (active)>",
    "<NSLayoutConstraint:0x283a835c0 'UIView-Encapsulated-Layout-Width' UIToolbar:0x133ebef70.width == 0   (active)>",
    "<NSLayoutConstraint:0x283a8b250 'UIView-leftMargin-guide-constraint' H:|-(0)-[UILayoutGuide:0x2820c1c00'UIViewLayoutMarginsGuide'](LTR)   (active, names: '|':_UIButtonBarStackView:0x133ec0570 )>",
    "<NSLayoutConstraint:0x283a8b2f0 'UIView-rightMargin-guide-constraint' H:[UILayoutGuide:0x2820c1c00'UIViewLayoutMarginsGuide']-(0)-|(LTR)   (active, names: '|':_UIButtonBarStackView:0x133ec0570 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x283a84960 H:[_UIModernBarButton:0x133dcc420]-(>=8)-|   (active, names: '|':_UIButtonBarButton:0x133dcc1d0 )>

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.

我真的很想解决这个问题。

编辑


找到问题的原因。按下按钮将调出键盘并再次消失。为什么会这样?

这个按钮的作用是与我交流。 Button 让我给他看 toast 辞。

JS函数

function copy_address(){
      var t = document.createElement("textarea");
      document.body.appendChild(t);
      t.value = $("#text").val();
      t.select();
      document.execCommand('copy');
      document.body.removeChild(t);
      var postData = {
                tip: "testing",
                message: "sucess",
                timestamp: (new Date()).getTime().toString()
        };
        webkit.messageHandlers.send.postMessage(postData);
}

JSP 按钮 View

<div class="w" id="user"></div>
<div class="b-copy">
    <button type="button" class="btn" onclick="copy_address()">copy</button>
    <input type="text" id="copy_text" style="display: none;">
</div>

WebViewController 中的 Swift 代码

import Toaster
...
    override func loadView() {
        super.loadView()
      ...
        mainWebView = WKWebView(frame: mainWebView.frame, configuration: config)

        mainWebView.uiDelegate = self
        mainWebView.navigationDelegate = self

        view.addSubview(mainWebView)
        view.addSubview(indicatorImage)
}
    @available(iOS 8.0, *)
    func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
        if message.name == "message" {
            let sendmessage = message.body as! NSDictionary
            guard sendmessage["val"] != nil else {
                return
            }
            let val : String = sendmessage["val"] as! String
            if sendmessage["val2"] != nil {
                getmessage = sendmessage["val2"] as? String
            }
          switch val {
            case "testing":
                Toast(text: getmessage).show()
                break
        ....
}
}

最佳答案

我是通过模拟器发现这个问题的。

当我检查我的实际 iPhone 时,屏幕看起来像是从上到下下降,因为我看不到键盘,但实际上只是看了一下,然后就消失了。

这就是工具栏高度出现问题的原因。而键盘出现问题的原因是JS函数。

t.select(); 
document.execCommand('copy');

由于这部​​分,键盘可见并消失了。所以我通过搜索解决了这个问题。

t.readOnly = true;

我已经通过添加这段代码解决了我的问题。

关于ios - AutoLayout - 无法同时满足 swift5 中的约束条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58391782/

相关文章:

ios - UITableViewCell 内的 UITextView 键盘问题

ios - CFRunLoopSource 中的 CFRunLoopScheduleCallBack 函数的用途是什么?

ios - UIImageView 动画不工作

swift - 如何在表单的标题中创建自定义 View

ios - 在 NIB 文件中定义并在 StoryBoard 中使用的自定义 View 的 AutoLayout 问题

ios - 如何从 Storyboard 文件中获取现有的 Controller 对象?

ios - 用于移动聊天应用程序的 NoSQL 服务器解决方案?

android - Phonegap 使用默认阅读器打开 pdf(IOS 和 Android)

ios - 如何使用 swift2+ 在 IOS APP 中按需删除/删除 UITableView 中的一个部分及其行

ios - 如何自定义添加到 Storyboard上的标签栏项目