gpt4 book ai didi

ios - 如何在 UITableView 中将 SF 符号相互对齐?

转载 作者:行者123 更新时间:2023-12-03 21:14:13 24 4
gpt4 key购买 nike

在 WWDC SF Symbols 视频中,Apple 表示在显示 SF Symbols 时更喜欢水平和垂直对齐。据推测,这意味着每个 UIImageView.center.x 应包含相同的值,以便它们排成一列。我的问题是如何确定 x 值应该是什么,因为它们的宽度都不同。以下是屏幕截图示例:

Screenshot

此屏幕截图将 UITableViewCellStyleDefaultcell.imageViewcell.textLabel 结合使用。但是,我想知道如何使用自定义 UITableViewCell 实现相同的效果。根据 UIImageSymbolConfiguration.pointSize 确定一定宽度的正确方法是吗?一旦你有了这个宽度,所有的符号都会根据这个中心显示?

任何关于如何布局/对齐 SF 符号的指导,尤其是在不使用自动布局的情况下,都会非常有帮助:)

最佳答案

这是一个非常简单的示例。

它循环遍历数据以确定最宽的符号,该符号将用于设置 imageView 的宽度约束常量。

class MySymbolCell: UITableViewCell {

let symbolImageView: UIImageView = {
let v = UIImageView()
v.contentMode = .center
return v
}()
let theLabel: UILabel = {
let v = UILabel()
return v
}()

var imageWidthConstraint: NSLayoutConstraint!

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

required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}

func commonInit() -> Void {

symbolImageView.translatesAutoresizingMaskIntoConstraints = false
theLabel.translatesAutoresizingMaskIntoConstraints = false

contentView.addSubview(symbolImageView)
contentView.addSubview(theLabel)

// this way we can change imageView width at run-time if needed
imageWidthConstraint = symbolImageView.widthAnchor.constraint(equalToConstant: 40.0)
imageWidthConstraint.priority = UILayoutPriority(rawValue: 999)

let g = contentView.layoutMarginsGuide

NSLayoutConstraint.activate([

// constrain imageView top / bottom / leading
symbolImageView.topAnchor.constraint(equalTo: g.topAnchor),
symbolImageView.bottomAnchor.constraint(equalTo: g.bottomAnchor),
symbolImageView.leadingAnchor.constraint(equalTo: g.leadingAnchor),

// constrain width
imageWidthConstraint,

// constrain height equal to width
symbolImageView.heightAnchor.constraint(equalTo: symbolImageView.widthAnchor),

// constrain label leading 8-pts from image view
theLabel.leadingAnchor.constraint(equalTo: symbolImageView.trailingAnchor, constant: 8.0),

// constrain label centerY to image view centerY
theLabel.centerYAnchor.constraint(equalTo: symbolImageView.centerYAnchor),

// constrain label trailing to content view trailing
theLabel.trailingAnchor.constraint(equalTo: g.trailingAnchor),

])
}
}

class MySymbolTableViewController: UITableViewController {

var myData: [String] = [
"arrow.up",
"arrow.down",
"person.badge.plus",
"eye.slash",
"line.horizontal.3",
"textformat.123",
]

var symbolImageViewWidth: CGFloat = 40.0

// configure as desired
let imageConfig = UIImage.SymbolConfiguration(pointSize: 20, weight: .light, scale: .default)

override func viewDidLoad() {
super.viewDidLoad()

// calculate how wide you need your cell's symbol image view to be

// either run a loop to get the widest symbol your data is using,
// or
// just make sure you can fit the widest of the symbols ("bold.italic.underline")

let testLoop = true

if testLoop {

var maxW: CGFloat = 0.0
myData.forEach {
if let img = UIImage(systemName: $0, withConfiguration: imageConfig) {
maxW = max(img.size.width, maxW)
}
}
symbolImageViewWidth = maxW

} else {

if let img = UIImage(systemName: "bold.italic.underline", withConfiguration: imageConfig) {
symbolImageViewWidth = img.size.width
}

}

tableView.register(MySymbolCell.self, forCellReuseIdentifier: "MySymbolCell")

}

override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myData.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MySymbolCell", for: indexPath) as! MySymbolCell

let img = UIImage(systemName: myData[indexPath.row], withConfiguration: imageConfig)

cell.symbolImageView.image = img
cell.theLabel.text = myData[indexPath.row]

cell.imageWidthConstraint.constant = symbolImageViewWidth

return cell
}

}

结果:

enter image description here

关于ios - 如何在 UITableView 中将 SF 符号相互对齐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61109245/

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