【问题标题】:NSLayoutConstraints to limit label widthNSLayoutConstraints 限制标签宽度
【发布时间】:2020-03-10 15:15:35
【问题描述】:

我正在努力设置必要的约束以将标签的宽度限制为表格视图单元格中按钮的前导锚点。

期望的结果

标签在左边,按钮在右边。如果需要,标签会在按钮之前换行。

当前结果

标签将红色按钮拉向左侧。

代码

[button.centerYAnchor constraintEqualToAnchor:cell.contentView.centerYAnchor constant:0].active = YES;
[button.trailingAnchor constraintEqualToAnchor:cell.contentView.trailingAnchor constant:-5].active = YES;

[label.leadingAnchor constraintEqualToAnchor:cell.contentView.leadingAnchor constant:10].active = YES;
[label.trailingAnchor constraintEqualToAnchor:button.leadingAnchor constant:0].active = YES;

我尝试了各种方法,例如在单元格右侧添加另一个标签约束,但挤压按钮(我试图通过设置其抗压优先级来修复,但没有效果)

请问我需要什么限制才能达到预期的结果?

【问题讨论】:

  • 这应该就是你所需要的:[button setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];

标签: ios objective-c autolayout


【解决方案1】:

改变一下

[label.trailingAnchor constraintEqualToAnchor:button.leadingAnchor constant:0].active = YES;

进入

[label.trailingAnchor constraintLessThanOrEqualToAnchor:button.leadingAnchor constant:0].active = YES;

import UIKit

final class ViewController: UIViewController {

  lazy var tableView: UITableView = {
    let val = UITableView(frame: self.view.bounds, style: .plain)
    val.dataSource = self
    val.delegate = self
    val.estimatedRowHeight = 44
    val.register(MyCell.self, forCellReuseIdentifier: "MyCell")
    return val
  }()

  let data: [String] = [
    "LayoutDemo",
    "LayoutDemo LayoutDemo LayoutDemo LayoutDemo LayoutDemo LayoutDemo LayoutDemo LayoutDemo LayoutDemo LayoutDemo LayoutDemo LayoutDemo",
    "LayoutDemoLayoutDemoLayoutDemoLayoutDemo"
  ]

  override func viewDidLoad() {
    super.viewDidLoad()

    view.addSubview(tableView)
  }

}

extension ViewController: UITableViewDataSource {
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    data.count
  }

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) as? MyCell else { fatalError() }
    cell.dataText = data[indexPath.row]
    return cell
  }

}

extension ViewController: UITableViewDelegate {

  func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableView.automaticDimension
  }

}

class MyCell: UITableViewCell {

  var dataText: String? {
    didSet {
      label.text = dataText
    }
  }

  lazy var label: UILabel = {
    let val = UILabel(frame: .zero)
    val.translatesAutoresizingMaskIntoConstraints = false
    val.numberOfLines = 0
    val.backgroundColor = .gray
    return val
  }()

  lazy var button: UIButton = {
    let val = UIButton(type: .custom)
    val.translatesAutoresizingMaskIntoConstraints = false
    val.backgroundColor = .red
    return val
  }()

  override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)

    contentView.addSubview(label)
    contentView.addSubview(button)

    NSLayoutConstraint.activate([
      button.centerYAnchor.constraint(equalTo: contentView.centerYAnchor),
      button.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -5),
      button.heightAnchor.constraint(equalToConstant: 20),
      button.widthAnchor.constraint(equalToConstant: 20),

      label.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 5),
      label.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -5),
      label.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 10),
      label.trailingAnchor.constraint(lessThanOrEqualTo: button.leadingAnchor, constant: -10)
    ])

  }

  required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }

}

【讨论】:

  • 对不起可能我没看懂问题,我想他是想让按钮的位置固定,然后左边的标签适应宽高,中间有一个间距按钮和标签。
猜你喜欢
  • 2015-04-17
  • 2016-02-05
  • 2011-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多