isUserInteractionEnabled

원동진·2024년 1월 24일

iOS-Etc

목록 보기
3/3

사이드프로젝트 진행중 Button의 action이 동작하지 않음 처음에는 Button을 CustomButton으로 생성하여 addTarget이 작동되지 않는다고 생각하였으나
view hierarchy로 계층을 살펴봤을때 다른 부분에서 문제가 생김을 깨달음.

문제점

self.view.addSubview(UIImageView)
startBackImgView.addSubView(UIButton)

위의 해당 코드로 인해 addTarget으로 지정해준 버튼 액션이 실행되지 않았음

isUserInteractionEnabled

사용자 상호 작용 활성화에 대한 인스턴스 속성

  • false 설정시 touch,press,keyboard,focus 이벤트 무시 되고 이벤트 큐에서 제거됨
  • true 설정시 해당 뷰가 정상적으로 전달
  • 애니메이션 중에는 속성의 값에 관계없이 애니메이션과 관련된 모든 Views에 대해 사용자 상호작용이 일시적으로 비활성화
  • 일부 UIKIT 하위 클래스는 true가 기본이 아닌 false를 반환하기도 한다
    ex)UIImageView ,UILabel

UIViewController의 view Default : true

-> 실제로 빌드한 결과 : true 출력

UIImageView의 Default : false

-> //UIImageView.isUserInteractionEnabled - Definition
@MainActor open class UIImageView : UIView {
    open var isUserInteractionEnabled: Bool // default is NO
}

-> UILabel defalut : no

문제 해결 설명

  • 위에서 보는것과 같이 자연스럽게 쓰던 코드인 self.view.addSubview(UIButton)의 action이 가능했던 이유는 UIViewController의 view Default : true 이기 때문

  • UIImageView의 isUserInteractionEnabled의 default는 false이기 때문에 UIImageView에 추가한 Button이 클릭되지 않는것 <- 겪은 문제점

해결법

UIImageView.isUserInteractionEnabled의 = true

Reference

애플 공식 Document

궁금한 점 또는 추가하면 좋을 점, 지적할 점이 있다면 댓글 부탁드립니다.

도움이 됬으면 좋겠습니다.

profile
iOS 개발자

0개의 댓글