- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有一个 TableViewCell 和两个按钮来切换不同的约束。
我想更新它的高度约束和单元格高度。
喜欢下面的图1
当我点击 buttonB 时, View 会像 pic2
然后我点击buttonA, View 会回到pic1
我尝试修改约束,但无法更新高度。
有什么想法或回答我吗?
谢谢
图1
图2
代码如下:
class CellContainView: UIView {
let buttonA: UIButton = { () -> UIButton in
let ui = UIButton()
ui.titleLabel?.numberOfLines = 0
ui.setTitle("Click\nA", for: .normal)
ui.backgroundColor = UIColor.blue
return ui
}()
let buttonB: UIButton = { () -> UIButton in
let ui = UIButton()
ui.titleLabel?.numberOfLines = 0
ui.setTitle("Click\nB", for: .normal)
ui.backgroundColor = UIColor.gray
return ui
}()
let buttonC: UIButton = { () -> UIButton in
let ui = UIButton()
ui.titleLabel?.numberOfLines = 0
ui.setTitle("Click\nC", for: .normal)
ui.backgroundColor = UIColor.brown
return ui
}()
let labelA: UILabel = { () -> UILabel in
let ui = UILabel()
ui.text = "Test"
return ui
}()
let viewA: UIView = { () -> UIView in
let ui = UIView()
ui.backgroundColor = UIColor.red
return ui
}()
override init(frame: CGRect) {
super.init(frame: frame)
addUI()
addConstrain()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func addUI() {
self.addSubview(buttonA)
self.addSubview(buttonB)
self.addSubview(buttonC)
self.addSubview(labelA)
self.addSubview(viewA)
}
func addConstrain() {
buttonA.snp.makeConstraints { (make) in
make.left.top.equalToSuperview()
make.height.equalTo(60)
}
buttonB.snp.makeConstraints { (make) in
make.left.equalTo(buttonA.snp.right)
make.top.right.equalToSuperview()
make.width.equalTo(buttonA.snp.width)
make.height.equalTo(buttonA.snp.height)
}
buttonC.snp.makeConstraints { (make) in
make.left.equalTo(15)
make.right.equalTo(-15)
make.bottom.equalTo(-15)
make.height.equalTo(50)
}
labelA.snp.makeConstraints { (make) in
make.left.equalTo(15)
make.top.equalTo(buttonA.snp.bottom).offset(15)
make.width.equalTo(195)
make.height.equalTo(50)
}
viewA.snp.makeConstraints { (make) in
make.left.equalTo(labelA.snp.right)
make.top.equalTo(buttonA.snp.bottom).offset(15)
make.right.equalTo(-15)
make.height.equalTo(50)
make.bottom.equalTo(buttonC.snp.top).offset(-10)
}
}
func updateConstrain(sender: UIButton) {
switch sender {
case buttonA:
viewA.snp.updateConstraints { (make) in
make.height.equalTo(50)
}
case buttonB:
viewA.snp.updateConstraints { (make) in
make.height.equalTo(150)
}
default:
break
}
}
}
class TestTableViewCell: UITableViewCell {
let cellContainView: CellContainView = { () -> CellContainView in
let ui = CellContainView()
ui.backgroundColor = UIColor.orange
return ui
}()
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.addSubview(cellContainView)
cellContainView.snp.makeConstraints { (make) in
make.left.top.equalTo(15)
make.bottom.right.equalTo(-15)
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
let tableView: UITableView = { () -> UITableView in
let ui = UITableView()
return ui
}()
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 44
tableView.register(TestTableViewCell.self, forCellReuseIdentifier: "TestTableViewCell")
tableView.tableFooterView = UIView()
self.view.addSubview(tableView)
tableView.snp.makeConstraints { (make) in
make.top.left.right.bottom.equalToSuperview()
}
}
@objc func buttonAClicked(sender: UIButton) {
let index = IndexPath(row: 0, section: 0)
let cell = tableView.cellForRow(at: index) as! TestTableViewCell
cell.cellContainView.updateConstrain(sender: sender)
tableview.reloadData()
}
@objc func buttonBClicked(sender: UIButton) {
let index = IndexPath(row: 0, section: 0)
let cell = tableView.cellForRow(at: index) as! TestTableViewCell
cell.cellContainView.updateConstrain(sender: sender)
tableview.reloadData()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TestTableViewCell", for: indexPath) as! TestTableViewCell
cell.cellContainView.buttonA.addTarget(self, action: #selector(buttonAClicked), for: .touchUpInside)
cell.cellContainView.buttonB.addTarget(self, action: #selector(buttonBClicked), for: .touchUpInside)
return cell
}
}
点击butttonB时更新约束问题(添加更新viewA heightConstrain & tableview.reloadData()):
TestCellUpdateHeight[29975:5195310] [LayoutConstraints] 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.
(
"<SnapKit.LayoutConstraint:0x6040000a4320@CellContainView.swift#75 UIButton:0x7fa1d0d0d680.height == 60.0>",
"<SnapKit.LayoutConstraint:0x6040000a49e0@CellContainView.swift#89 UIButton:0x7fa1d0d10610.height == 50.0>",
"<SnapKit.LayoutConstraint:0x6040000a51c0@CellContainView.swift#104 UIView:0x7fa1d0d11130.height == 150.0>",
"<SnapKit.LayoutConstraint:0x6040000a4800@CellContainView.swift#80 UIButton:0x7fa1d0d0f7b0.top == TestCellUpdateHeight.CellContainView:0x7fa1d0d0d470.top>",
"<SnapKit.LayoutConstraint:0x6040000a4a40@CellContainView.swift#82 UIButton:0x7fa1d0d0f7b0.height == UIButton:0x7fa1d0d0d680.height>",
"<SnapKit.LayoutConstraint:0x6040000a4ec0@CellContainView.swift#88 UIButton:0x7fa1d0d10610.bottom == TestCellUpdateHeight.CellContainView:0x7fa1d0d0d470.bottom - 15.0>",
"<SnapKit.LayoutConstraint:0x6040000a5100@CellContainView.swift#102 UIView:0x7fa1d0d11130.top == UIButton:0x7fa1d0d0f7b0.bottom + 15.0>",
"<SnapKit.LayoutConstraint:0x6040000a5220@CellContainView.swift#105 UIView:0x7fa1d0d11130.bottom == UIButton:0x7fa1d0d10610.top - 10.0>",
"<SnapKit.LayoutConstraint:0x6040000a52e0@TestTableViewCell.swift#26 TestCellUpdateHeight.CellContainView:0x7fa1d0d0d470.top == TestCellUpdateHeight.TestTableViewCell:0x7fa1d208a000.top + 15.0>",
"<SnapKit.LayoutConstraint:0x6040000a53a0@TestTableViewCell.swift#27 TestCellUpdateHeight.CellContainView:0x7fa1d0d0d470.bottom == TestCellUpdateHeight.TestTableViewCell:0x7fa1d208a000.bottom - 15.0>",
"<NSLayoutConstraint:0x6080002819f0 'UIView-Encapsulated-Layout-Height' TestCellUpdateHeight.TestTableViewCell:0x7fa1d208a000'TestTableViewCell'.height == 230 (active)>"
)
Will attempt to recover by breaking constraint
最佳答案
一般来说,要更新 uiview:UIView
的高度约束,您应该先保存它:
var viewHeightConstraint: Constraint!
uiview.snp.makeConstraints { (make) in
viewHeightConstraint = make.height.equalTo(50).constraint
}
如果您只是简单地使用updateConstraints
函数,它会添加一个新的高度约束,从而导致问题Unable to simultaneously satisfy constraints
。所以你必须删除最后一个:
viewHeightConstraint.deactivate()
然后再次进行约束:
uiview.snp.makeConstraints { (make) in
viewHeightConstraint = make.height.equalTo(100).constraint
}
关于ios - 如何通过 SnapKit 更新高度约束并更新单元格高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53007231/
我已经尝试了各种安装“Snapkit”的方法,但到目前为止我运气不佳。 我正在使用以下内容:- Xcode 8.3.3 到目前为止,我已经尝试按照所有在线文档来解决问题,但它仍然无法正常工作。包括以下
我有几个标签 应该有以下要求 最小宽度为 38 像素 右对齐 高度为 22 像素 每侧额外填充 8 像素的宽度 到目前为止,我设法使 1、2 和 3 正常工作(如图所示),但我在第四个约束上遇到了麻烦
以下是我的 viewDidAppear() 方法,我试图用它为我的应用程序构建功能原型(prototype)。 GraphicsView 实例变量通过 Storyboard绑定(bind)到我编写的
我用一个场景制作了 Storyboard。在我的 UIViewController 中,我使用 SnapKit 定义了以下内容: @IBOutlet weak var emailTextField:
我正在使用 Snapkit 来简化我的自动布局代码,但是似乎经常弹出一个场景,我想知道是否有一种方法可以减少代码。 假设我需要将 UIView 的边缘固定到它的 super View 边距,我们可能会
我尝试了不同的教程来使用 Snapkit 制作灯光动画,但似乎没有任何帮助。我什至尝试过 Spring 框架,但效果不佳。这是我的代码。 private func setupMatchView() {
我正在尝试在 SnapKit 中创建此布局的约束,但我很难设置最大大小 (lessThanOrEqual)。 let maxWidthContainer: CGFloat = 435 let maxH
我构建了一个应用程序,它不使用任何界面生成器,而是使用 Snapkit 创建自动布局约束。纵向时一切看起来都很好,但是一些屏幕需要一些特定于横向的限制。 我在这里和谷歌上搜索了关于执行此操作的快速介绍
只是尝试使用 SnapKit 自动布局添加 UILabel。我使用的代码如下: override func viewDidLoad() { super.viewDidLoad() se
我根据iPhone X版本编辑了我的应用程序。其他 iPhone 型号由于屏幕尺寸的原因此图像已损坏。如何根据 iPhone 版本编辑我定义的值?例如,我分享一个示例代码: registerlabel
我正在尝试为我的应用程序制作 Logo (UILabel) 从中间到顶部的动画。我尝试的是更新约束,但它似乎不起作用。问题是动画,即 Logo ,从原点 (0,0) 而不是 View 的中间到顶部。必
我想要的是这样的: 这是我的代码: let label1 = UILabel() let label2 = UILabel() label1.textColor = .whit
现在如果我想在左边和右边设置相等的边距,它需要两行: label.snp.makeConstraints { make in make.left.equalTo(Constants.margi
我目前正在将 SnapKit 与 Swift 结合使用,使用演练来制作测验应用程序。我目前正在制定一些约束,并想将一些新约束附加到我的约束数组中 我收到错误消息“元组类型 '()' 的值没有成员 an
当我尝试在创建约束后立即更新 snapkit 约束时,我得到一个Updated constraint could not find existing matching constraint to up
我是SnapKit的初学者,我想用SnapKit实现一个UITableViewController,每一行有两个UILabel,一个是Title,另一个是Value。我的问题是 UITableView
我正在尝试使用 SnapKit 实现 2 个 View 的动画。 这是我的动画 View : class MatchAnimation: UIView { let viewBackground
我有一个 TableViewCell 和两个按钮来切换不同的约束。 我想更新它的高度约束和单元格高度。 喜欢下面的图1 当我点击 buttonB 时, View 会像 pic2 然后我点击button
func setupPosition() { box.snp.makeConstraints{(make)->Void in make.edges.equalTo(vi
我有一个标签,无论使用什么设备,我都希望它在屏幕上具有相同的相对位置。例如。标签的位置距 View 上边距 10%,距 View 左边距 30%。 常量将始终进行定位,例如距离 View 边距 150
我是一名优秀的程序员,十分优秀!