单击自定义集合视图单元格中的按钮时未突出显示(optionsButton)。...我添加的任何动作都有效但是,触摸按钮时,系统高亮显示不出来。
下面的代码...
class HomeFeedCell: UICollectionViewCell {
public lazy var userProfilePhotoimageView: UIImageView = {
let iv = UIImageView()
iv.translatesAutoresizingMaskIntoConstraints = false
iv.clipsToBounds = true
iv.backgroundColor = .systemBlue
iv.contentMode = .scaleAspectFill
iv.layer.cornerRadius = 40 / 2
return iv
}()
public lazy var profileNameLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.clipsToBounds = true
label.text = "UserName"
return label
}()
public lazy var imageView: UIImageView = {
let iv = UIImageView()
iv.translatesAutoresizingMaskIntoConstraints = false
iv.clipsToBounds = true
iv.image = UIImage(systemName: "photo")
iv.contentMode = .scaleAspectFill
return iv
}()
public lazy var optionsButton: UIButton = {
let button = UIButton(type: .system)
button.translatesAutoresizingMaskIntoConstraints = false
button.titleLabel?.font = UIFont.preferredFont(forTextStyle: .body)
button.setTitle("Done", for: .normal)
return button
}()
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
private func commonInit() {
setupProfilePhotoConstraints()
setupImageViewConstraints()
setupOptionsButtonConstraints()
setupProfileNameLabelConstraints()
}
private func setupProfilePhotoConstraints() {
addSubview(userProfilePhotoimageView)
NSLayoutConstraint.activate([
userProfilePhotoimageView.topAnchor.constraint(equalTo: topAnchor, constant: 8),
userProfilePhotoimageView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 8),
userProfilePhotoimageView.widthAnchor.constraint(equalToConstant: 40),
userProfilePhotoimageView.heightAnchor.constraint(equalTo: userProfilePhotoimageView.widthAnchor)
])
}
private func setupProfileNameLabelConstraints() {
addSubview(profileNameLabel)
NSLayoutConstraint.activate([
profileNameLabel.topAnchor.constraint(equalTo: topAnchor),
profileNameLabel.leadingAnchor.constraint(equalTo: userProfilePhotoimageView.trailingAnchor, constant: 8),
profileNameLabel.bottomAnchor.constraint(equalTo: imageView.topAnchor),
profileNameLabel.trailingAnchor.constraint(equalTo: optionsButton.leadingAnchor)
])
}
private func setupImageViewConstraints() {
addSubview(imageView)
NSLayoutConstraint.activate([
imageView.topAnchor.constraint(equalTo: userProfilePhotoimageView.bottomAnchor, constant: 8),
imageView.leadingAnchor.constraint(equalTo: leadingAnchor),
imageView.bottomAnchor.constraint(equalTo: bottomAnchor),
imageView.trailingAnchor.constraint(equalTo: trailingAnchor)
])
}
private func setupOptionsButtonConstraints() {
addSubview(optionsButton)
NSLayoutConstraint.activate([
optionsButton.topAnchor.constraint(equalTo: topAnchor),
optionsButton.bottomAnchor.constraint(equalTo: imageView.topAnchor),
optionsButton.trailingAnchor.constraint(equalTo: trailingAnchor),
optionsButton.widthAnchor.constraint(equalToConstant: 44),
])
}
public func configureCellPhoto(_ imageURL: String) {
guard let url = URL(string: imageURL) else { return }
imageView.kf.setImage(with: url)
}
@objc private func handleOptions() {
print("tapped")
}
}
我已经尝试了我能想到的一切。..甚至使用聊天gpt,这是无用的lol任何帮助将不胜感激!
3条答案
按热度按时间ozxc1zmp1#
每次你说
addSubview
,都是错的。将其更改为contentView.addSubview
,您的按钮将开始工作。oiopk7p52#
试试这个选项:
hfsqlsce3#
更新:所以高亮确实有效。当我在模拟器或手机上点击按钮时,不会像其他情况下那样立即出现高亮响应。所以我什么都没看到然而,当我坚定地按下按钮,有点像用力按下(比轻敲更长),然后放手,我看到一个亮点。所以我不知道到底发生了什么,但有一个回应。我的理论是,可能有一些限制使触摸面积太小。然而,按键尺寸大于苹果推荐的44 x44。IDK.我确实试过改变。addSubview到contentView。addSubview,但我相信,这两种方法的工作(测试和相同的结果)时,添加内容的CV单元格的超级视图。然而,The。当按钮被牢牢按下时,系统功能工作!