1

我有一個UICollectionViewCell子類,它有一個UIStackView。 StackView填充了UIButtons,一切看起來都很好 但是這些按鈕是不可用的。iOS Swift 3 - UICollectionViewCell中的UIStackView上的UIButton沒有響應輕擊事件(無交互)

如果我佈局上UIScrollView同一按鈕(用於實驗的緣故),而不是在stackview,所以它看起來像是與stackview導致了問題

任何想法的按鈕罰款迴應?

My Layout

下面是我如何將按鈕添加到StackView

func prepareStackView(buttonsArray: Array<UIButton>) { 


    var rect:CGRect = (stackView?.frame)! 
    rect.size.width = btn.frame.size.width * buttonsArray.count 
    stackView?.frame = rect //The frame of the stackview is set as such so that it looks exactly like I want 


    //Add all the buttons 
    for btn in buttonsArray { 
      //The buttons already have their selectors set 
      stackView?.addArrangedSubview(btn) 
    } 


} 
+0

禁用stackview交互stackview.isUserInteractionEnabled =假 –

+0

爲什麼?然後它的任何排列的視圖都無法互動 – Zigglzworth

+0

我認爲stackview會覆蓋button的interactable屬性。那麼,我曾經說過,你試過了嗎? –

回答

0

使用此代碼的UIButton響應單擊事件的代碼:

override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { 

    if clipsToBounds || isHidden || alpha == 0 { 
     return nil 
    } 

    for subview in subviews.reversed() { 
     let subPoint = subview.convert(point, from: self) 
     if let result = subview.hitTest(subPoint, with: event) { 
      return result 
     } 
    } 

    return nil 
} 

有用的鏈接:

Capturing touches on a subview outside the frame of its superview using hitTest:withEvent:

我也遇到類似的問題,並能夠解決。請檢查我的帖子描述了這種類型的原因,並試圖解釋我是如何解決的。

UIButton is not clickable after first click