AutoLayout - Unable to simultaneously satisfy constraints in swift5

my problem occurs when you press the button on the sidebar of the web view. The button sends a simple message to the web view and copies the clipboard. And Swift shows you the received message as a toast. It's very simple.



The function does not appear to be problematic,



but the UI is a problem. When the button is pressed, the screen slides from top to bottom, showing the toast and pressing the button again activates the function of pressing the other button on the sidebar.



The screen is the same, but the screen seems to be down. The sidebar screen contains a scroll. Is that what it is about?



Error log:



   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)
    (
        "",
        "",
        "",
        "",
        "=8)-|   (active, names: '|':_UIButtonBarButton:0x133dcc1d0 )>",
        "=5)-[_UIModernBarButton:0x133ecbe60]   (active, names: '|':_UIButtonBarButton:0x133ecbc10 )>",
        "=5)-|   (active, names: '|':_UIButtonBarButton:0x133ecbc10 )>",
        "",
        "",
        "<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
    =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  may also be helpful.




Found the cause of the problem. Pressing the button will bring up the keyboard and disappear again. Why is this happening?


The function of this button communicates with me. Button asks me to show him a toast message.



JSfunction

~~~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 Button View

~~~jsp

<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>

~~~



**Swift Code in 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
        ....
}
}

~~~

Answered by beginerIOS in 388554022

I solved the problem by adding a line of code.

    t.readOnly = true;


Full Code

    t.value = $("#address_text").val();
    t.readOnly = true;
    t.select();
    document.execCommand('copy');
    document.body.removeChild(t);

The problem is probably in the constraints you have defined on the view objects.


What are the constraints you have defined ?

Please, explain precisely.


Could be you have defined constraints inside the scrollView that creates the problem.

This issue occurred in the js function.


~~~ js

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

~~~

There is no problem if I delete this part.How can I solve this problem?


I need to make a copy of the clipboard.

I have not tested, so it is just a guess after looking at doc:

h ttps://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand


did you try to specify boolean userInterface ? Test with true and with false.

boolean execCommand(DOMString command, optional boolean userInterface, optional DOMString ? value);
Accepted Answer

I solved the problem by adding a line of code.

    t.readOnly = true;


Full Code

    t.value = $("#address_text").val();
    t.readOnly = true;
    t.select();
    document.execCommand('copy');
    document.body.removeChild(t);
AutoLayout - Unable to simultaneously satisfy constraints in swift5
 
 
Q