【问题标题】:Center UIImegaeView in CollectionViewCell在 CollectionViewCell 中居中 UIImegaeView
【发布时间】:2021-12-04 18:52:57
【问题描述】:

我尝试创建一个 collectionView 单元格,其中心有一个图像

我的班级看起来像:

import Foundation
import UIKit

class MenuCell: UICollectionViewCell{
    

    let image:UIImageView = {
        
        let i = UIImageView()
        i.image = (UIImage(named: "mainMenu"))
        i.contentMode = .scaleAspectFit
        return i
        
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        

    }
    
    required init?(coder: NSCoder) {
        fatalError()
    }
    
    func setupCell(){
        addSubview(image)

        let centerXConst = NSLayoutConstraint(item: image, attribute: .centerX, relatedBy: .equal, toItem: self.contentView, attribute: .centerY, multiplier: 1.0, constant: 0.0)
        let centerYConst = NSLayoutConstraint(item: image, attribute: .centerY, relatedBy: .equal, toItem: self.contentView, attribute: .centerY, multiplier: 1.0, constant: 0.0)
        
    
        image.addConstraints([centerXConst, centerYConst])
        NSLayoutConstraint.activate([image.centerYAnchor.constraint(equalTo: self.contentView.centerYAnchor), image.centerXAnchor.constraint(equalTo: contentView.centerXAnchor)])


    }
    
}

但是当我运行它时它给了我一个错误

当添加到视图时,约束的项必须是 该视图(或视图本身)。如果约束,这将崩溃 需要在组装视图层次结构之前解决。休息

如我所见,我通常添加常量。

更新: 更改函数的代码。不知道=(

func setupCell(){
    contentView.addSubview(image)

    let centerXConst = NSLayoutConstraint(item: image, attribute: .centerX, relatedBy: .equal, toItem: self.contentView, attribute: .centerX, multiplier: 1.0, constant: 0.0)
    let centerYConst = NSLayoutConstraint(item: image, attribute: .centerY, relatedBy: .equal, toItem: self.contentView, attribute: .centerY, multiplier: 1.0, constant: 0.0)

    NSLayoutConstraint.activate([centerXConst, centerYConst])


}

【问题讨论】:

  • 对于 centerXConst var 属性是 centerX 而不是 centerY。也只添加 NSLayoutConstraint.activate 行。不需要 image.addConstraints 代码。

标签: ios swift view uiview uiimageview


【解决方案1】:

如我所见,我通常添加常量。

嗯,你看错了。您将图像视图固定到单元格内容视图,但没有将图像视图 放入 内容视图。你需要。改变

addSubview(image)

contentView.addSubview(image)

此外,正如评论中所指出的,将中心 x 固定到中心 y 是没有意义的。更正如下:

let centerXConst = NSLayoutConstraint(item: image, attribute: .centerX, relatedBy: .equal, toItem: self.contentView, attribute: .centerX, multiplier: 1.0, constant: 0.0)
let centerYConst = NSLayoutConstraint(item: image, attribute: .centerY, relatedBy: .equal, toItem: self.contentView, attribute: .centerY, multiplier: 1.0, constant: 0.0)

另外,您忘记告诉图像视图不要将其自动调整掩码转换为约束。

最后,不要同时添加和激活约束。激活添加,您显然不知道将约束添加到哪个视图。所以删除这一行:

image.addConstraints([centerXConst, centerYConst])

【讨论】:

  • 改一下,还是一样。 =((. func setupCell(){ contentView.addSubview(image) let centerXConst = NSLayoutConstraint(item: image, attribute: .centerX, relatedBy: .equal, toItem: self.contentView, attribute: .centerX, multiplier: 1.0, constant : 0.0) let centerYConst = NSLayoutConstraint(item: image, attribute: .centerY, relatedBy: .equal, toItem: self.contentView, attribute: .centerY, multiplier: 1.0, constant: 0.0) NSLayoutConstraint.activate([centerXConst, centerYConst] ) }
  • 对不起,我不知道如何在评论中发送代码
  • 已更新问题
  • 您需要停止编译器从自动调整大小的掩码创建自己的布局约束。在应用约束之前添加image.translatesAutoresizingMaskIntoConstraints = false
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-02
  • 2021-04-19
  • 1970-01-01
  • 2021-01-08
  • 2021-05-02
  • 2015-12-11
  • 1970-01-01
相关资源
最近更新 更多