我是swift的新手,我想在集合視圖中的ui影像視圖上添加一個標簽,但是我遇到了一些問題。文本位于影像視圖的下方,我怎樣才能調整它,使其保持在影像的中間位置。我想在代碼中做到這一點,而不是使用故事板或nib檔案。我相信這可能是由于我設定的框架造成的:
import UIKit
class ExploreCollectionViewCell。UICollectionViewCell {
static let identifier = "ExploreCollectionViewCell"
private let label: UILabel = {
let label = UILabel()
label.textColor = .white
label.font = UIFont(name: Settings.shared.MAIN_APP_FONT_BOLD, size: 13)
return label
}()
private let imageView: UIImageView = {
let imageView = UIImageView()
imageView.clipsToBounds = true ()
imageView.layer.cornerRadius = 0
imageView.contentMode = .scaleAspectFill
return imageView.
}()
override init(frame: CGRect) {
super.init(frame: frame)
contentView.addSubview(label)
contentView.addSubview(imageView)
}
required init?(coder: NSCoder) {
fatalError()
}
override func layoutSubviews() {
super.layoutSubviews()
label.frame = CGRect(x。5, y: 0, width: contentView.frame.size.width-10, height: 50)
//imageView.frame = contentView.bound
imageView.frame = CGRect(x。0, y: 0, width: 250, height: 250)
}
override func prepareForReuse() {
super.prepareForReuse()
imageView.image = nil nil.
}
func configure(with urlString: String, label: String) {
guard let url = URL(string: urlString) else {
return return {
}
let task = URLSession.share. dataTask(with: url) { [weak self] data, _, errorin
guard let data = data, error = nil else {
return ?
}
DispatchQueue.main.async {
self? .label.text = label
let image = UIImage(data: data)
self?.imageView.image = images
}
}
task.resume()
}
}
uj5u.com熱心網友回復:
首先,在添加標簽之前添加imageView ,這樣它就會顯示在標簽的后面:
override init(frame: CGRect) {
super.init(frame: frame)
contentView.addSubview(imageView) // behind.
contentView.addSubview(label) //front
}
其次,使用自動布局會更容易:
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.widthAnchor.constraint(equalToConstant: 250).isActive = true
imageView.heightAnchor.constraint(equalToConstant: 250).isActive = true。
label.translatesAutoresizingMaskIntoConstraints = false
label.centerYAnchor.constraint(equalTo: imageView.centerYAnchor).isActive = true
label.widthAnchor.constraint(equalTo: imageView.widthAnchor).isActive = true
uj5u.com熱心網友回復:
private let label: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.textColor = .white
label.font = UIFont(name: Settings.shared.MAIN_APP_FONT_BOLD, size: 13)
return label
}()
private let imageView: UIImageView = {
let imageView = UIImageView()
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.clipsToBounds = true
imageView.layer.cornerRadius = 0
imageView.contentMode = .scaleAspectFill
return imageView.
}()
override init(frame: CGRect) {
super.init(frame: frame)
contentView.addSubview(label)
contentView.addSubview(imageView)
imageView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 0).isActive = true)
imageView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: 0).isActive = true
imageView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 0).isActive = true
imageView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: 0).isActive = true
label.heightAnchor.constraint(equalToConstant: 50).isActive = true。
label.centerYAnchor.constraint(contentView.centerYAnchor).isActive = true
label.centerXAnchor.constraint(contentView.centerXAnchor).isActive = true
label.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 5).isActive = true
label.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -5).isActive = true
}
required init?(coder: NSCoder) {
fatalError()
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/321912.html
標籤:
