gpt4 book ai didi

swift - 如何在不同情况下以编程方式快速限制 View 多次?

转载 作者:可可西里 更新时间:2023-11-01 01:50:45 26 4
gpt4 key购买 nike

我有一些标签作为按钮,每个标签都过滤我的表格 View 内容。在我的一个标签中,我需要将表格 View 稍微移到底部。因为我必须向用户显示另一个 View ,并且表格 View 必须位于下方。

我对 View 中的所有 View 都有约束,首先加载方法,我应该再次在标签监听器中约束我的 tableView。

这是我的第一个约束的代码

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

最佳答案

系统不喜欢将多个约束分配给具有相同优先级的 anchor ,因为它们会发生冲突并且系统会“破坏”其中一个以满足至少一个。如果没有另外定义,约束的默认优先级是 required (1000)。您可以为 anchor 分配多个约束,并通过为它们分配不同的优先级(例如 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 函数中,您只需切换约束的优先级并告诉系统更新您的 View 布局。

@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 动画 block 中并使整个动画效果:

@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 已经存在并且位于同一显示层次结构中时才有效- 目前还没有在你的代码中看到它 ;)

关于swift - 如何在不同情况下以编程方式快速限制 View 多次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54376234/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com