2012-11-26 10 views
8

UICollectionView 보충 뷰 (바닥 글)에 UIButton을 배치하려고합니다. 스토리 보드를 사용하여 UICollectionViewCell의 하위 클래스에 UIButton을 연결했으며 프로그래밍 방식으로 배경 이미지를 포함하여 속성을 변경할 수 있습니다. 그러나 버튼의 이벤트 내부에있는 터치를 메서드에 연결하면 발사되지 않습니다. 사실, 버튼은 사용자의 터치에 시각적으로 반응하는 것조차 보이지 않습니다. 문제를 해결할 때 컬렉션 뷰의 헤더에 UIButton을 추가하여 동일한 동작을 시도했습니다. 비 관련보기에 단추를 추가하면 눌렀을 때 상호 작용 효과가 발생합니다.UICollection 보조보기에서 UIButton을 어떻게 사용할 수 있습니까?

UICollection 보완 뷰에서 UIButton을 구현하기 위해해야 ​​할 특별한 것이 있습니까?

답변

16

보조보기는 UICollectionViewCell이 아닌 UICollectionReusableView (또는 그 하위 클래스)이어야합니다.

어쨌든 버튼이 응답하지 않으면 먼저 모든 조상 뷰에 userInteractionEnabledYES으로 설정되어 있는지 확인해야합니다. 버튼이 화면에 나타나면 디버거에서 일시 중지하고 다음을 수행하십시오.

(lldb) po [[UIApp keyWindow] recursiveDescription] 

목록에서 단추를 찾아서 복사하십시오. 그런 다음 각 수퍼 뷰를 확인할 수 있습니다. 예 :

(lldb) p (BOOL)[0xa2e4800 isUserInteractionEnabled] 
(BOOL) $4 = YES 
(lldb) p (BOOL)[[0xa2e4800 superview] isUserInteractionEnabled] 
(BOOL) $5 = YES 
(lldb) p (BOOL)[[[0xa2e4800 superview] superview] isUserInteractionEnabled] 
(BOOL) $6 = YES 
(lldb) p (BOOL)[[[[0xa2e4800 superview] superview] superview] isUserInteractionEnabled] 
(BOOL) $8 = YES 
(lldb) p (BOOL)[[[[[0xa2e4800 superview] superview] superview] superview] isUserInteractionEnabled] 
(BOOL) $9 = YES 
(lldb) p (BOOL)[[[[[[0xa2e4800 superview] superview] superview] superview] superview] isUserInteractionEnabled] 
(BOOL) $10 = NO 
(lldb) po [[[[[0xa2e4800 superview] superview] superview] superview] superview] 
(id) $11 = 0x00000000 <nil> 

는 여기에 루트합니다 ( UIWindow)까지 모든 뷰가 isUserInteractionEnabled에서 YES을 반환 것을 발견했다. 창 슈퍼 뷰는 nil입니다.

+1

@robmayhoff : 문제는 내가 잘못된 부모를 서브 클래 싱했다는 사실이었습니다. 이걸 가져 주셔서 감사합니다! – markdorison

+0

@markdorison 하하하 흥미 롭습니다. 그게 정말로 문제라고 생각하지 않았습니다. –

+0

@robmayhoff : 나는 하나의 변화를 만들어 냈을 때처럼 놀랐습니다! – markdorison

0

하위 클래스가 UIControl이고 UICollectionReusableView 인 맞춤 버튼이 있습니다. 작동 시키려면 컨트롤의 모든 하위 뷰를 만들고 하위 뷰의 하위 뷰는 사용자 상호 작용을 처리하지 않습니다.

func disableUserInteractionInView(view: UIView) { 
    view.userInteractionEnabled = false 
    for view in view.subviews { 
     self.disableUserInteractionInView(view) 
    } 
} 

// My control has a subview called contentView which serves as a container 
// of all my custom button's subviews. 
self.disableUserInteractionInView(self.contentView) 

그 코드를 추가하기 때문에, .TouchUpInside 모든 이벤트 리스너는 해고, 아래로 누르면 컨트롤이 시각적으로 강조 표시됩니다.

관련 문제