ios - 如果另一个项目不存在,Xcode 将项目居中对齐

标签 ios swift xcode alignment cell

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

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

如何使标签居中?当然我可以设置约束值= cellHeight/2 但还有其他方法可以做到这一点吗?或者我可以在 Xcode 编辑器中执行此操作吗?

最佳答案

试试这个。 swift 4.1

  • 在 stackView 中添加 containerView(在单元格内)。
  • 将 imageView 和标签添加到容器 View
  • 隐藏其中一个并在 stackView 中运行 layoutIfNeeded()

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

您必须在单元格内添加 UIStackView 并应用 topbottomleft anchor

在示例中,我使用按钮来切换 View ,您可以在 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
    }
}

关于ios - 如果另一个项目不存在,Xcode 将项目居中对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52172998/

相关文章:

ios - 字符串数组的安全数据持久性

ios - pod install 在 Jenkins 上给出错误

ios - 自定义 View - self.frame 不正确?

快速异步网络。出现错误时如何重试?

iphone - 基于服务器的数据库

iOS 内部应用程序 - SSL 证书的分发

ios - 是否可以创建多个 DispatchQueue?

ios - 如何在 swift 中使用现有的 SQLite 数据库?

iphone - 在 Xcode 中创建 Objective-C++ 静态库

ios - 使用 AVAudioPlayer 播放多个音频文件