【问题标题】:Xcode align item in center if another item doesn't exists如果另一个项目不存在,Xcode 将项目居中对齐
【发布时间】:2019-02-09 21:11:38
【问题描述】:

我正在使用 UICollectionView 来显示项目。我的单元格包含 ImageView 和底部标签。 ImageView没有图像时如何在单元格中心制作标签?让我们说:

if ("no image in ImageView ") { 
ImageView.setActive = false
"what to do here?"

如何使标签居中?当然我可以设置约束值 = cellHeight/2 但是还有另一种方法吗?或者我可以在 Xcode 编辑器中这样做吗?

【问题讨论】:

  • 您可以在 UIStackView 中添加图像视图和标签。将堆栈视图分布设置为均等填充。如果图像视图中没有图像,则隐藏图像视图。您的标签将居中。
  • 好主意,我会试试的,但是当我的单元格可调整大小时可以使用 UIStackView 吗?
  • 让我知道它是否有效。
  • 不,它不起作用,控制台抱怨很多无法处理约束
  • 我会做两件事。 (2) 将 both 图像视图 label* 设置为表格单元格的 peers,从而无需依赖另一个 - 当然,如果在前面,您现在需要使用哪个视图。 (2) 使用 centerYAnchorcenterXAnchor 代替帧值 - cellHeight/2 可能是。但是您确实应该发布比我们提供帮助更多的代码。

标签: ios swift xcode alignment cell


【解决方案1】:

试试这个。 斯威夫特 4.1

  • 在您的 stackView 中添加 containerViews(在单元格内)。
  • 将您的图像视图和标签添加到容器视图中
  • 隐藏其中一个并在stackView中运行layoutIfNeeded()

使用 singleView 项目的工作示例(使用按钮切换)。相同的代码应该在 UITableViewUICollectionView 的单元格内工作。

您必须在单元格内添加UIStackView 并应用topbottomleftright 锚点

在示例中,我使用一个按钮来切换视图,您可以在 imageView 中使用 didSet{} 来隐藏/显示 containerView

import UIKit
class ViewController: UIViewController {
    var stackView: UIStackView!
    var topView: UIView!
    override func viewDidLoad() {
        super.viewDidLoad()
        topView = UIView()
        topView.backgroundColor = .yellow
        let bottomView = UIView()
        bottomView.backgroundColor = .cyan
        stackView = UIStackView(arrangedSubviews: [topView, bottomView])
        stackView.distribution = .fillEqually
        stackView.axis = .vertical
        self.view.addSubview(stackView)
        stackView.centerWithSize(size: CGSize(width: 100, height: 200))
        let imageView = UIImageView()
        imageView.image = UIImage(named: "hexagon.png")
        topView.addSubview(imageView)
        imageView.centerWithSize(size: CGSize(width: 30, height: 30))
        let label = UILabel()
        label.text = "TEXT"
        label.font = UIFont(name: "Helvetica", size: 20)
        bottomView.addSubview(label)
        label.centerWithSize(size: CGSize(width: 100, height: 100))
        addButton()
    }
    func addButton() {
        let button = UIButton(type: UIButtonType.system) as UIButton
        button.titleLabel?.font = UIFont.systemFont(ofSize: 16.0)
        button.setTitle("TOGGLE", for: .normal)
        button.addTarget(self, action: #selector(toggle), for: .touchUpInside)
        button.frame = CGRect(x: 0, y: 50, width: 100, height: 30)
        self.view.addSubview(button)
    }
    @objc func toggle(sender: UIButton) {
        topView.isHidden = topView.isHidden == true ? false : true
        stackView.layoutIfNeeded()
    }
} 
extension UIView {
   func centerWithSize(size: CGSize) {
       self.translatesAutoresizingMaskIntoConstraints = false
       self.widthAnchor.constraint(equalToConstant: size.width).isActive = true
       self.heightAnchor.constraint(equalToConstant: size.height).isActive = true
       self.centerXAnchor.constraint(equalTo: self.superview!.centerXAnchor).isActive = true
       self.centerYAnchor.constraint(equalTo: self.superview!.centerYAnchor).isActive = true
    }
}

【讨论】:

  • 感谢您的解决方案。但由于动态单元格大小,它不起作用。我做了分散的 UICollectionView。单元格大小因图片大小和标题字符串长度而异。
猜你喜欢
  • 2022-08-14
  • 2015-10-26
  • 1970-01-01
  • 2017-10-26
  • 1970-01-01
  • 2022-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多