ios - 尝试为 iOS 编程锚定图像
问题描述
这是我的代码
import UIKit
class PageCell: UICollectionViewCell {
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}
let imageView: UIImageView = {
let iv = UIImageView()
iv.contentMode = .scaleAspectFill
iv.backgroundColor = .yellow
iv.image = UIImage(named: "page1")
return iv
}()
func setupViews() {
backgroundColor = .blue
addSubview(imageView)
imageView.(topAnchor, bottomAnchor, rightAnchor, leftAnchor)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
编译时显示此错误。Value of type 'UIImageView' has no member 'anchorToTop'
顺便说一句,我试图将图像锚定到单个 UIImageViewCell。
解决方案
你可以试试
func setupViews() {
backgroundColor = .blue
imageView.translatesAutoresizingMaskIntoConstraints = false
self.contentView.addSubview(imageView)
imageView.topAnchor.constraint(equalTo: self.contentView.topAnchor, constant: 0 ).isActive = true
imageView.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor, constant: 0 ).isActive = true
imageView.leftAnchor.constraint(equalTo: self.contentView.leftAnchor, constant: 0 ).isActive = true
imageView.rightAnchor.constraint(equalTo: self.contentView.rightAnchor, constant: 0 ).isActive = true
}
推荐阅读
- aws-cdk - 如何使用 AWS CDK 从两个不同的账户获取 SSM 参数
- r - 在 R 中使用 if() 编写函数
- python - 如何获取 argparser 参数名称?
- python - 如何将每个时期中的张量值保留在一层中并将其传递给张量流中的下一个时期
- linux - 删除文件中的重复项而不创建新文件bash
- r - 我不断收到“plot.new() 中的错误:数字边距太大”
- javascript - 函数值内的javascript对象?
- ocaml - 了解 OCaml 中的基本递归
- javascript - Javascript: How to compare two arrays, and return the name of the array with highest value in a certain index
- python - 是否有任何工具支持 Python 包的 Provides-Dist 元数据?如果不是,为什么?