[UIKit] UIMenu 사용기

2024. 12. 10. 02:36·📱 iOS/UIKit

문제 상황

버튼을 클릭했을 때, UIMenu를 띄워봅시당


문제 해결

UIButton의 menu

/// An optional menu for the button to display. The button will automatically enable or disable its contextMenuInteraction when a non-nil or nil menu is set. Defaults to nil.
@available(iOS 14.0, *)
@NSCopying open var menu: UIMenu?

UIButton에는 Menu라는 프로퍼티가 있다.

iOS 14버전 이상부터 쓸 수 있음 !!

 

dropDownButton.menu = UIMenu(
    title: "",
    children: [
        UIAction(
            title: "책 커버 수정",
            image: UIImage(systemName: "pencil"),
            handler: { _ in dropDownButtonEditAction() }
        ),
        UIAction(
            title: "책 커버 삭제",
            image: UIImage(systemName: "trash"),
            attributes: .destructive,
            handler: { _ in dropDownButtonDeleteAction() }
        )
    ]
)

근데 기본적으로 버튼에 들어가는 menu는 꾹 눌러야 메뉴가 뜬다.

그러면 나처럼 클릭했을 때 바로 menu가 보이게 하려면 어떻게 할까 ??

처음에 addAction이나 addTarget에 넣어야 하나 했지만, 이런게 있었다

얘를 true로 설정해주면 버튼을 꾹 누르지 않고도, 클릭 한 번으로 메뉴를 나오게 할 수 있다 !!

/// If the contextMenuInteraction is the primary action of the control, invoked on touch-down. NO by default.
@available(iOS 14.0, *)
open var showsMenuAsPrimaryAction: Bool

 

수정된 나의 코드

dropDownButton.showsMenuAsPrimaryAction = true
dropDownButton.menu = UIMenu(
    title: "",
    children: [
        UIAction(
            title: "책 커버 수정",
            image: UIImage(systemName: "pencil"),
            handler: { _ in dropDownButtonEditAction() }
        ),
        UIAction(
            title: "책 커버 삭제",
            image: UIImage(systemName: "trash"),
            attributes: .destructive,
            handler: { _ in dropDownButtonDeleteAction() }
        )
    ]
)

배운 점

  • 버튼 안에 있는 UIMenu 사용
  • 꾹 눌러야 나오는 UIMenu를 클릭 한 번으로 할 수 있게 하는 showsMenuAsPrimaryAction를 알게 됨

참조 링크

https://ios-daniel-yang.tistory.com/entry/SwiftTIL-15-UIMenu%EB%A5%BC-%EC%82%AC%EC%9A%A9%ED%95%B4%EB%B3%B4%EC%9E%90

 

저작자표시 (새창열림)

'📱 iOS > UIKit' 카테고리의 다른 글

[문제해결] UITableViewCell에서 CornerRadius 개별 설정 시 초기 레이아웃 문제를 비동기 재귀로 해결하기  (1) 2025.02.28
[UIKit] 뷰의 Corner Radius 각각 다르게 처리하기 (with CACornerMask, UIBezierPath, CAShapeLayer)  (0) 2025.02.28
[UIKit] CollectionViewCell 드래그 앤 드랍 구현하기  (0) 2024.12.01
[UIKit] iOS 15.0 이상에서 UIButton 안에 있는 이미지 사이즈 조절하기  (4) 2024.11.15
[UIKit] 런타임 시점에 Constraint를 조절하여 애니메이션 구현하기  (2) 2024.11.14
'📱 iOS/UIKit' 카테고리의 다른 글
  • [문제해결] UITableViewCell에서 CornerRadius 개별 설정 시 초기 레이아웃 문제를 비동기 재귀로 해결하기
  • [UIKit] 뷰의 Corner Radius 각각 다르게 처리하기 (with CACornerMask, UIBezierPath, CAShapeLayer)
  • [UIKit] CollectionViewCell 드래그 앤 드랍 구현하기
  • [UIKit] iOS 15.0 이상에서 UIButton 안에 있는 이미지 사이즈 조절하기
kyxxn
kyxxn
컴퓨터공학을 좋아하는 대학생의 공부 일기
  • kyxxn
    컴공 학부생의 공부 일기
    kyxxn
  • 전체
    오늘
    어제
    • 분류 전체보기 (156)
      • 📱 iOS (64)
        • Xcode (10)
        • Swift (17)
        • Swift Concurrency (12)
        • UIKit (21)
        • SwiftUI (0)
      • 🖥️ Computer Science (57)
        • 🏛️ Software Architecture Pa.. (2)
        • 👨🏻‍🎨 Design Pattern (3)
        • Data Structure (4)
        • Algorithm (10)
        • Computer Architecture (4)
        • Operating System (19)
        • Network (15)
      • ✍🏻 회고록 (9)
      • 🎸 기타 (25)
        • 해커톤 (1)
        • git (6)
        • 세미나 (1)
        • 책을 읽고 (1)
        • AOS, Kotlin (6)
        • Reinforcement Learning (9)
  • 블로그 메뉴

    • 링크

      • 깃허브
      • 일상 블로그
    • 공지사항

    • 인기 글

    • 최근 댓글

    • 최근 글

    • hELLO· Designed By정상우.v4.10.0
    kyxxn
    [UIKit] UIMenu 사용기
    상단으로

    티스토리툴바