Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在UITableView中将SF符号彼此对齐?_Ios_Iphone_Uitableview_Uiimage_Sf Symbols - Fatal编程技术网

Ios 如何在UITableView中将SF符号彼此对齐?

Ios 如何在UITableView中将SF符号彼此对齐?,ios,iphone,uitableview,uiimage,sf-symbols,Ios,Iphone,Uitableview,Uiimage,Sf Symbols,在WWDC SF符号视频中,苹果表示在显示SF符号时更喜欢水平和垂直对齐。这可能意味着每个UIImageView.center.x都应该包含相同的值,以便它们在一列中对齐。我的问题是如何确定x值应该是什么,因为它们都是不同的宽度。以下是一个示例屏幕截图: 此屏幕截图将UITableViewCellStyleDefault与cell.imageView和cell.textLabel一起使用。但是,我想知道如何使用自定义的UITableViewCell实现相同的效果。根据UIImageSymbol

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

此屏幕截图将
UITableViewCellStyleDefault
cell.imageView
cell.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
    }

}
结果:


调整cell.separatorInset可能更容易

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        cell.separatorInset = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 0)
}

为图像视图指定所需的宽度,然后将图像视图的内容模式设置为居中
myImageView.contentMode=.center
Yes,但是您如何知道图像视图的宽度?或者如何定位文本,使单元格排列在一起?这通常是通过自动布局完成的。。。我真的不理解你的评论:“特别是没有使用自动布局”?你打算把你的单元设计成故事板原型吗?还是只通过代码?我只是用代码做的。使用
layoutSubviews
定位感谢这里的帮助!
粗体、斜体、下划线是最宽的符号吗?是-至少现在是这样。我想在未来的更新中苹果可能会添加一个新的符号,但我的假设是你知道你将使用什么符号(或者至少是什么符号集)。