2017-08-27 94 views
0

我想將UIButton作爲UICollectionViewCell中另一個自定義UIView的子視圖添加 但是,我添加的這兩個按鈕不響應觸摸並且不執行任何操作。在UICollectionViewCell中以編程方式添加UIButton不起作用

我在這裏初始化按鈕:

let likeButton: UIButton = { 
    let button = UIButton(type: .system) 
    button.anchor(width: 40, height: 40) 
    button.backgroundColor = UIColor(red: 239.0/255.0, green: 83.0/255.0, blue: 80.0/255.0, alpha: 0.8) 
    button.layer.cornerRadius = 20 
    button.layer.masksToBounds = true 
    button.isUserInteractionEnabled = true 
    return button 
}() 

let commentButton: UIButton = { 
    let button = UIButton(type: .system) 
    button.backgroundColor = UIColor(red: 38.0/255.0, green: 37.0/255.0, blue: 37.0/255.0, alpha: 0.8) 
    button.layer.cornerRadius = 20 
    button.layer.masksToBounds = true 
    return button 
}() 

override init(frame: CGRect) { 
    super.init(frame: frame) 
    likeButton.addTarget(self, action: #selector(handleLike), for: .touchUpInside) 
    commentButton.addTarget(self, action: #selector(handleComment), for: .touchUpInside) 
    setupViews() 
} 

func handleLike(_ sender: UIButton) { 
    print("like") 
} 

func handleComment(_ sender: UIButton) { 
    print("comment") 
} 

添加爲子視圖並設置限制條件:

override init(frame: CGRect) { 
    super.init(frame: frame) 

    setupViews() 
} 

func setupViews() { 

    let stackView = UIStackView(arrangedSubviews: [likesLabel, commentsLabel, viewsLabel]) 
    stackView.axis = .horizontal 
    stackView.distribution = .fillProportionally 
    stackView.spacing = 4 

    addSubview(newsImage) 
    newsImage.addSubview(darkView) 
    darkView.addSubview(newsTitleLabel) 
    darkView.addSubview(stackView) 
    darkView.addSubview(timeStampLabel) 
    setButtonsStackView() 

    addConstraintsWithFormaat(format: "H:|[v0]|", views: newsImage) 
    addConstraintsWithFormaat(format: "V:|[v0]|", views: newsImage) 
    newsImage.addConstraintsWithFormaat(format: "H:|[v0]|", views: darkView) 
    newsImage.addConstraintsWithFormaat(format: "V:|[v0]|", views: darkView) 

    stackView.anchor(top: nil, left: leftAnchor, bottom: bottomAnchor, right: nil, paddingTop: 0, paddingLeft: 10, paddingBottom: -10, paddingRight: 0) 
    newsTitleLabel.anchor(top: nil, left: leftAnchor, bottom: stackView.topAnchor, right: nil, paddingTop: 0, paddingLeft: 10, paddingBottom: -5, paddingRight: 0) 
    timeStampLabel.anchor(top: nil, left: nil, bottom: bottomAnchor, right: rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: -10, paddingRight: 10) 
} 

func setButtonsStackView() { 
    let buttonStackView = UIStackView(arrangedSubviews: [likeButton, commentButton]) 
    buttonStackView.axis = .horizontal 
    buttonStackView.distribution = .fillProportionally 
    buttonStackView.spacing = 10 
    darkView.addSubview(buttonStackView) 
    buttonStackView.anchor(top: darkView.topAnchor, left: nil, bottom: nil, right: darkView.rightAnchor, paddingTop: 10, paddingLeft: 10, paddingBottom: 0, paddingRight: 10) 
} 

正如我正在考慮這個問題可能與設置的限制,也因爲我從讀另一篇文章中,當你添加一個按鈕作爲UIImageView的子視圖時,這些按鈕從不起作用。

無論如何,我會很樂意得到任何建議!

PS:我使用自定義擴展來設置約束。他們工作得很好,因爲我經常在我的項目中使用它們。

謝謝!

回答

0

我剛剛意識到我必須設置buttonStackView與cellView的contentView相關的約束。

addSubview(buttonStackView) 
    buttonStackView.anchor(top: topAnchor, left: nil, 
          bottom: nil, right: rightAnchor, 
          paddingTop: 10, paddingLeft: 10, 
          paddingBottom: 0, paddingRight: 10) 

之後,所有的按鈕工作正常,並響應觸摸。

將按鈕設置爲UIImageView的子視圖實際上是主要問題。

0

你可以試試,

默認的UIImageViewUserInteraction屬性設置爲 false。這是我們大多數人犯錯的地方。所以主 東西要加入任何控制UIImageView是檢查 其isUserInteractionEnabled財產true

相關問題