<i id='qkOMH'><tr id='qkOMH'><dt id='qkOMH'><q id='qkOMH'><span id='qkOMH'><b id='qkOMH'><form id='qkOMH'><ins id='qkOMH'></ins><ul id='qkOMH'></ul><sub id='qkOMH'></sub></form><legend id='qkOMH'></legend><bdo id='qkOMH'><pre id='qkOMH'><center id='qkOMH'></center></pre></bdo></b><th id='qkOMH'></th></span></q></dt></tr></i><div id='qkOMH'><tfoot id='qkOMH'></tfoot><dl id='qkOMH'><fieldset id='qkOMH'></fieldset></dl></div>

      <small id='qkOMH'></small><noframes id='qkOMH'>

      • <bdo id='qkOMH'></bdo><ul id='qkOMH'></ul>

        <legend id='qkOMH'><style id='qkOMH'><dir id='qkOMH'><q id='qkOMH'></q></dir></style></legend>

        <tfoot id='qkOMH'></tfoot>

        Swift 更新约束

        时间:2023-09-11

        1. <legend id='M7RNk'><style id='M7RNk'><dir id='M7RNk'><q id='M7RNk'></q></dir></style></legend>

              <i id='M7RNk'><tr id='M7RNk'><dt id='M7RNk'><q id='M7RNk'><span id='M7RNk'><b id='M7RNk'><form id='M7RNk'><ins id='M7RNk'></ins><ul id='M7RNk'></ul><sub id='M7RNk'></sub></form><legend id='M7RNk'></legend><bdo id='M7RNk'><pre id='M7RNk'><center id='M7RNk'></center></pre></bdo></b><th id='M7RNk'></th></span></q></dt></tr></i><div id='M7RNk'><tfoot id='M7RNk'></tfoot><dl id='M7RNk'><fieldset id='M7RNk'></fieldset></dl></div>

                <tbody id='M7RNk'></tbody>

                <tfoot id='M7RNk'></tfoot>
                  <bdo id='M7RNk'></bdo><ul id='M7RNk'></ul>
                • <small id='M7RNk'></small><noframes id='M7RNk'>

                  本文介绍了Swift 更新约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我将约束添加到在我的 UIView

                  I added the constraint to the buttons created in my UIView

                  func CreateButtonWithIndex(index:Int) {
                  
                      newButton.setTranslatesAutoresizingMaskIntoConstraints(false)
                  
                      self.view.addSubview(newButton)
                  
                      let newButtonConstraintL = NSLayoutConstraint(item: newButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 50)
                      let newButtonConstraintH = NSLayoutConstraint(item: newButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 50)
                      var newButtonConstraintX = NSLayoutConstraint(item: newButton, attribute: .CenterX, relatedBy: .Equal, toItem: view, attribute: .CenterX, multiplier: 1, constant: CGFloat(riga))
                      var newButtonConstraintY = NSLayoutConstraint(item: newButton, attribute: .CenterY, relatedBy: .Equal, toItem: view, attribute: .CenterY, multiplier: 1, constant: CGFloat(colonna))
                  
                      view.addConstraints([newButtonConstraintX,newButtonConstraintY,newButtonConstraintH,newButtonConstraintL])
                  
                      var pan = UIPanGestureRecognizer(target:self, action:"pan:")
                      pan.maximumNumberOfTouches = 2
                      pan.minimumNumberOfTouches = 1
                      newButton.addGestureRecognizer(pan)
                  }
                  
                  func pan(rec:UIPanGestureRecognizer) {
                    case .Ended: 
                          if let subview = selectedView {
                              if let button = rec.view as? UIButton {
                                  if let title = button.titleForState(.Normal){
                                      button.setTranslatesAutoresizingMaskIntoConstraints(false)
                                      let line = Float(p.x % snapX)
                                      let column = Float(p.x % snapY)
                                      let constraintL = NSLayoutConstraint(item: button, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 50)
                                      let constraintH = NSLayoutConstraint(item: button, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 50)
                                      var constraint2 = NSLayoutConstraint(item: button, attribute: .CenterX, relatedBy: .Equal, toItem: view, attribute: .CenterX, multiplier: 1, constant: CGFloat(line))
                                      var constraint3 = NSLayoutConstraint(item: button, attribute: .CenterY, relatedBy: .Equal, toItem: view, attribute: .CenterY, multiplier: 1, constant: CGFloat(column))
                  
                                      view.addConstraints([constraint2,constraint3,constraintL.constraintH])
                                  }
                              }
                          }
                  }
                  

                  在我的项目中,我的用户可以移动这些按钮,然后我尝试为识别的按钮添加更多约束,但我得到了一个无穷无尽的错误约束

                  In My Project my users can move these buttons and then I tried to add more constraints to the buttons recognized but I just get an endless error constraint

                    "<NSLayoutConstraint:0x7f8e5a4e9450 UIButton:0x7f8e5a644f60'Button6'.centerX == UIView:0x7f8e5a648bc0.centerX - 120>",
                    "<NSLayoutConstraint:0x7f8e5a5be040 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7f8e5a648bc0(736)]>",
                    "<NSAutoresizingMaskLayoutConstraint:0x7f8e5a5be1f0 h=-&- v=-&- 'UIView-Encapsulated-Layout-Left' H:|-(0)-[UIView:0x7f8e5a648bc0]   (Names: '|':UITransitionView:0x7f8e5a5a8190 )>",
                    "<NSAutoresizingMaskLayoutConstraint:0x7f8e5a5b53b0 h=--& v=--& UIButton:0x7f8e5a644f60'Button6'.midX == + 200>"
                  

                  将尝试通过打破约束来恢复

                  Will attempt to recover by breaking constraint

                  "<NSLayoutConstraint:0x7f8e5a4e9450 UIButton:0x7f8e5a644f60'Button6'.centerX == UIView:0x7f8e5a648bc0.centerX - 120>"
                  
                  "<NSLayoutConstraint:0x7f8e5a4e9510 UIButton:0x7f8e5a644f60'Button6'.centerY == UIView:0x7f8e5a648bc0.centerY - 40>",
                  "<NSLayoutConstraint:0x7f8e5a5be1a0 'UIView-Encapsulated-Layout-Height' V:[UIView:0x7f8e5a648bc0(414)]>",
                  "<NSAutoresizingMaskLayoutConstraint:0x7f8e5a5be240 h=-&- v=-&- 'UIView-Encapsulated-Layout-Top' V:|-(0)-[UIView:0x7f8e5a648bc0]   (Names: '|':UITransitionView:0x7f8e5a5a8190 )>",
                  "<NSAutoresizingMaskLayoutConstraint:0x7f8e5a5b82d0 h=--& v=--& UIButton:0x7f8e5a644f60'Button6'.midY == + 189>"
                  

                  将尝试通过打破约束来恢复

                  Will attempt to recover by breaking constraint

                  <NSLayoutConstraint:0x7f8e5a4e9510 UIButton:0x7f8e5a644f60'Button6'.centerY == UIView:0x7f8e5a648bc0.centerY - 40>
                  

                  ...我应该更新 newButtonConstraintX/Y 但我不明白我该怎么做吗?

                  ... should I update newButtonConstraintX / Y but I'm not understanding how I can do this ?

                  推荐答案

                  问题是您正在添加一个与现有约束冲突的新约束.

                  The issue is that you're adding a new constraint that conflicts with the existing constraint.

                  你有几个选择:

                  1. 在 iOS 8 中生效,您可以在添加新约束之前将约束的 active 属性设置为 false.

                  在 iOS 8 之前的版本中,您可能希望在添加新约束之前删除旧约束.

                  In iOS versions prior to 8, you would want to remove the old constraints before adding new constraints.

                  理想情况下,最好不必激活/停用(或者,更糟糕的是,添加和删除)约束,而只需修改单个约束的 constant 属性.例如在 Swift 3/4 中:

                  Ideally, it's best to not have to activate/deactivate (or, worse, add and remove) constraints, but rather just modify the constant property of a single constraint. For example in Swift 3/4:

                  class ViewController: UIViewController {
                  
                      private var xConstraint: NSLayoutConstraint!
                      private var yConstraint: NSLayoutConstraint!
                  
                      override func viewDidLoad() {
                          super.viewDidLoad()
                  
                          let label = UILabel()
                          label.text = "x"
                          label.translatesAutoresizingMaskIntoConstraints = false
                          view.addSubview(label)
                  
                          // I don't really need to save references to these, so these are local variables
                  
                          let widthConstraint = label.widthAnchor.constraint(equalToConstant: 50)
                          let heightConstraint = label.heightAnchor.constraint(equalToConstant: 50)
                  
                          // but since I'll be modifying these later, these are class properties
                  
                          xConstraint = label.centerXAnchor.constraint(equalTo: view.centerXAnchor)
                          yConstraint = label.centerYAnchor.constraint(equalTo: view.centerYAnchor)
                  
                          NSLayoutConstraint.activate([widthConstraint, heightConstraint, xConstraint, yConstraint])
                  
                          let pan = UIPanGestureRecognizer(target: self, action: #selector(handlePan(_:)))
                          view.addGestureRecognizer(pan)
                      }
                  
                      private var originalCenter: CGPoint!
                  
                      @objc func handlePan(_ gesture: UIPanGestureRecognizer) {
                          if gesture.state == .began {
                              originalCenter = CGPoint(x: xConstraint.constant, y: yConstraint.constant)
                          }
                  
                          let translation = gesture.translation(in: gesture.view!)
                  
                          xConstraint.constant = originalCenter.x + translation.x
                          yConstraint.constant = originalCenter.y + translation.y
                      }
                  
                  }
                  

                  当通过修改约束的constant可以达到想要的效果时,一般是最好的.

                  When the desired effect can be achieved by modifying the constant of the constraint, that's generally best.

                  有关 Swift 2 语法,请参阅此答案的上一版本.

                  For Swift 2 syntax, see previous revision of this answer.

                  这篇关于Swift 更新约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何设置动态宽度&amp;iOS自动布局中视图的高度? 下一篇:无法使用自动布局“打开"移动 UILabel 的 Y 位置在视野中

                  相关文章

                  <legend id='tJVMT'><style id='tJVMT'><dir id='tJVMT'><q id='tJVMT'></q></dir></style></legend>

                    1. <i id='tJVMT'><tr id='tJVMT'><dt id='tJVMT'><q id='tJVMT'><span id='tJVMT'><b id='tJVMT'><form id='tJVMT'><ins id='tJVMT'></ins><ul id='tJVMT'></ul><sub id='tJVMT'></sub></form><legend id='tJVMT'></legend><bdo id='tJVMT'><pre id='tJVMT'><center id='tJVMT'></center></pre></bdo></b><th id='tJVMT'></th></span></q></dt></tr></i><div id='tJVMT'><tfoot id='tJVMT'></tfoot><dl id='tJVMT'><fieldset id='tJVMT'></fieldset></dl></div>
                    2. <tfoot id='tJVMT'></tfoot>
                        <bdo id='tJVMT'></bdo><ul id='tJVMT'></ul>
                    3. <small id='tJVMT'></small><noframes id='tJVMT'>