如何在 swift 中以编程方式在不同情况下多次限制视图?

how to constraints views many times in different situations programmatically in swift?

我有一些标签作为按钮,每个按钮都会过滤我的 table 查看内容。在我的一个标签中,我需要稍微移动 table 视图 bottom.because 我必须向用户显示另一个视图并且 table 视图必须在下方。

我有约束我所有的视图首先加载方法,我应该再次约束我的 table在标签监听器中查看。

这是我的第一个约束代码

 override func setupViews() {
    super.setupViews()


    view.addSubview(header)
    view.addSubview(serachOptionView)
    view.addSubview(tableView)
    header.addSubview(mytitle)
    view.addSubview(serachBarView)

    serachBarView.addSubview(serachInCityLabel)
    serachBarView.addSubview(serachInCountryLabel)
    serachBarView.addSubview(serachInTransitLabel)
    serachBarView.addSubview(advanceSearch)
    serachBarView.addSubview(columnLabelLeft)
    serachBarView.addSubview(columnLabelRight)




    //TO - DO header

    header.translatesAutoresizingMaskIntoConstraints=false
    header.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0).isActive=true
    header.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive=true
    header.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive=true
    header.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.17).isActive=true
    header.backgroundColor =  UIColor.init(patternImage: UIImage(named: "test.png")!)





    // MARK -- serachBarView


    serachBarView.translatesAutoresizingMaskIntoConstraints=false
    serachBarView.trailingAnchor.constraint(equalTo: header.trailingAnchor, constant: -3).isActive=true
    serachBarView.leadingAnchor.constraint(equalTo: header.leadingAnchor, constant: 3).isActive=true
    serachBarView.bottomAnchor.constraint(equalTo: header.bottomAnchor, constant: -1).isActive=true
    serachBarView.heightAnchor.constraint(equalTo: header.heightAnchor, multiplier: 0.27).isActive=true



    // MARK -- INCity

    serachInCityLabel.translatesAutoresizingMaskIntoConstraints=false
    serachInCityLabel.trailingAnchor.constraint(equalTo: serachBarView.trailingAnchor, constant: -5).isActive=true
    serachInCityLabel.topAnchor.constraint(equalTo: serachBarView.topAnchor, constant: 1).isActive=true
    serachInCityLabel.bottomAnchor.constraint(equalTo: serachBarView.bottomAnchor, constant: -1).isActive=true
    serachInCityLabel.widthAnchor.constraint(equalTo: serachBarView.widthAnchor, multiplier: 0.23).isActive=true


    // MARK -- columnLabelRight

    columnLabelRight.translatesAutoresizingMaskIntoConstraints=false
    columnLabelRight.trailingAnchor.constraint(equalTo: serachInCityLabel.leadingAnchor, constant: 0).isActive=true
    columnLabelRight.topAnchor.constraint(equalTo: serachInCityLabel.topAnchor, constant: 4).isActive=true
    columnLabelRight.bottomAnchor.constraint(equalTo: serachInCityLabel.bottomAnchor, constant: -4).isActive=true
    columnLabelRight.widthAnchor.constraint(equalToConstant: 1.5).isActive=true



    // MARK -- INCountry

    serachInCountryLabel.translatesAutoresizingMaskIntoConstraints=false
    serachInCountryLabel.trailingAnchor.constraint(equalTo: columnLabelRight.leadingAnchor, constant: 0).isActive=true
    serachInCountryLabel.topAnchor.constraint(equalTo: serachBarView.topAnchor, constant: 1).isActive=true
    serachInCountryLabel.bottomAnchor.constraint(equalTo: serachBarView.bottomAnchor, constant: -1).isActive=true
    serachInCountryLabel.widthAnchor.constraint(equalTo: serachBarView.widthAnchor, multiplier: 0.24).isActive=true


    // MARK -- columnLabelRight

    columnLabelLeft.translatesAutoresizingMaskIntoConstraints=false
    columnLabelLeft.trailingAnchor.constraint(equalTo: serachInCountryLabel.leadingAnchor, constant: 0).isActive=true
    columnLabelLeft.topAnchor.constraint(equalTo: serachInCountryLabel.topAnchor, constant: 4).isActive=true
    columnLabelLeft.bottomAnchor.constraint(equalTo: serachInCountryLabel.bottomAnchor, constant: -4).isActive=true
    columnLabelLeft.widthAnchor.constraint(equalToConstant: 1.5).isActive=true

    // MARK -- INTranis


    serachInTransitLabel.translatesAutoresizingMaskIntoConstraints=false
    serachInTransitLabel.trailingAnchor.constraint(equalTo: serachInCountryLabel.leadingAnchor, constant: 0).isActive=true
    serachInTransitLabel.topAnchor.constraint(equalTo: serachBarView.topAnchor, constant: 1).isActive=true
    serachInTransitLabel.bottomAnchor.constraint(equalTo: serachBarView.bottomAnchor, constant: -1).isActive=true
    serachInTransitLabel.widthAnchor.constraint(equalTo: serachBarView.widthAnchor, multiplier: 0.24).isActive=true


    // MARK -- advance search


    advanceSearch.translatesAutoresizingMaskIntoConstraints=false
    advanceSearch.trailingAnchor.constraint(equalTo: serachInTransitLabel.leadingAnchor, constant: 0).isActive=true
    advanceSearch.topAnchor.constraint(equalTo: serachBarView.topAnchor, constant: 1).isActive=true
    advanceSearch.bottomAnchor.constraint(equalTo: serachBarView.bottomAnchor, constant: -1).isActive=true
    advanceSearch.widthAnchor.constraint(equalTo: serachBarView.widthAnchor, multiplier: 0.24).isActive=true


    // MARK -- tableView


    tableView.translatesAutoresizingMaskIntoConstraints=false
    tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0).isActive=true

    tableView.topAnchor.constraint(equalTo: serachBarView.bottomAnchor, constant: 0).isActive=true
    tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive=true
    tableView.delegate=self
    tableView.dataSource=self
    tableView.register(specialGoodCell.self, forCellReuseIdentifier: "myCell")


}

并移至我的 table 下方查看:

@objc func labelAction(){

      // MARK -- tableView


        tableView.translatesAutoresizingMaskIntoConstraints=false
        tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0).isActive=true

        tableView.topAnchor.constraint(equalTo: mypopUpView.bottomAnchor, constant: 0).isActive=true
        tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive=true
        tableView.delegate=self
        tableView.dataSource=self
        tableView.register(specialGoodCell.self, forCellReuseIdentifier: "myCell")

}

结果是没有移动和这个警告:

能够同时满足约束。

    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. 
(
    "<NSLayoutConstraint:0x28276f750 UIView:0x13fd45270.bottom == UIView:0x13fd4b2f0.bottom - 1   (active)>",
    "<NSLayoutConstraint:0x28276f610 V:[UIView:0x13fd45270]-(0)-[UITableView:0x1400a0000]   (active)>",
    "<NSLayoutConstraint:0x28276f2a0 V:[UIView:0x13fd4b2f0]-(40)-[UIView:0x13fd18a40]   (active)>",
    "<NSLayoutConstraint:0x28276f8e0 UIView:0x13fd18a40.height == 0.16*UIView:0x13fe428e0.height   (active)>",
    "<NSLayoutConstraint:0x28276a990 V:[UIView:0x13fd18a40]-(0)-[UITableView:0x1400a0000]   (active)>",
    "<NSLayoutConstraint:0x28276cb40 'UIView-Encapsulated-Layout-Height' UIView:0x13fe428e0.height == 568   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x28276f2a0 V:[UIView:0x13fd4b2f0]-(40)-[UIView:0x13fd18a40]   (active)>

例如...在您设置的多个约束中,您有一个前导、一个尾随,然后还有一个宽度。您不能先说 5 然后尾随 -5,然后还指定宽度,因为如果有意义的话,您已经用前导和尾随约束指定了宽度?如果不告诉我,我会更详细地解释。基本上如果你的屏幕是 300 像素宽。你有一个标签并将其限制为前导 50,尾随 -50 .. 然后你的标签宽度将自动变为 200 .. 你无法添加任何宽度锚点,因为它无法满足你所有的约束。

系统不喜欢将多个约束分配给具有相同优先级的锚点,因为它们会发生冲突并且系统'breaks'其中之一至少要满足一个。如果没有另外定义,约束的默认优先级是 required (1000)。您可以为一个锚点分配多个约束,并通过为它们分配不同的优先级(例如 defaultLowdefaultHigh 并代表您切换它们来激活所需的约束。

第一件事是保留对约束的引用以便更改它们。欢迎将您的代码更新为以下内容:

class MyViewController: UIViewController {

    // ...

    // Keep a reference to the constraints you want to change
    // depending on user interaction or desired state 
    // in your view controller class

    var tableViewConstraintTopAnchor1: NSLayoutConstraint!
    var tableViewConstraintTopAnchor2: NSLayoutConstraint!

    // ...

}

然后在您的 setupViews 函数中,分配它们:

func setupViews() {

    // ...

    // adding subviews and other constraints

    // ...

    // MARK -- tableView


    tableView.translatesAutoresizingMaskIntoConstraints=false
    tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive=true
    tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0).isActive=true

    // assign (desired, but conflicting) constraints with different priority

    tableViewConstraintTopAnchor1 = tableView.topAnchor.constraint(equalTo: serachBarView.bottomAnchor, constant: 0)
    tableViewConstraintTopAnchor1.priority = .defaultHigh
    tableViewConstraintTopAnchor1.isActive = true

    tableViewConstraintTopAnchor2 = tableView.topAnchor.constraint(equalTo: mypopUpView.bottomAnchor, constant: 0)
    tableViewConstraintTopAnchor2.priority = .defaultLow
    tableViewConstraintTopAnchor2.isActive = true

    tableView.delegate = self
    tableView.dataSource = self
    tableView.register(specialGoodCell.self, forCellReuseIdentifier: "myCell")

}

在您的 labelAction 函数中,您只需切换约束的优先级并告诉系统更新您的视图布局。

@objc func labelAction(){

    // MARK -- tableView

    // Switch the priority of the desired constraint to defaultHigh
    // the other ones to defaultLow

    tableViewConstraintTopAnchor1.priority = .defaultLow
    tableViewConstraintTopAnchor2.priority = .defaultHigh

    view.setNeedsLayout()
    view.layoutIfNeeded()

}

也可以将其放入 UIView 动画块中并使整个动画具有动画效果:

@objc func labelAction(){

    // MARK -- tableView

    UIView.animate(withDuration: 0.2) {

         self.tableViewConstraintTopAnchor1.priority = .defaultLow
         self.tableViewConstraintTopAnchor2.priority = .defaultHigh

         self.view.setNeedsLayout()
         self.view.layoutIfNeeded()

    }

}

viewDidLoad()(或您在其中调用的函数)中为您的 tableView 分配两个约束仅在 mypopUpView 已经存在并且位于同一显示层次结构中时有效 - 没有看到它到目前为止在你的代码中 ;)