๐ง UIMenu๋ฅผ ๋ง๋ค์ด๋ณผ๊ฑฐ์์!
Step 1
UIAction ๋ฐฐ์ด์ ์์ฑํฉ๋๋ค. ๊ทธ๋ฆฌ๊ณ UIMenu๋ฅผ ์์ฑํ์ฌ children ํ๋ผ๋ฏธํฐ์ ์์ฑํ UIAction ๋ฐฐ์ด์ ์ ์ฉํฉ๋๋ค.
class ViewController: UIViewController {
private lazy var menuItems: [UIAction] = {
return [
UIAction(
title: "Hello World!",
image: UIImage(systemName: "figure.wave"),
handler: { _ in
print("Hello World!")
}),
UIAction(
title: "Morning World!",
image: UIImage(systemName: "cloud.sun.fill"),
handler: { _ in
print("Moring World!")
}),
UIAction(
title: "Night World!",
image: UIImage(systemName: "moon.stars.fill"),
handler: { _ in
print("Night World!")
})
]
}()
private lazy var menu: UIMenu = {
return UIMenu(title: "", options: [], children: menuItems)
}()
...
}
Step 2
TableView๋ฅผ ์์ฑํด์ฃผ์ธ์. ๊ทธ ๋ค์, tableView(_:contextMenuConfigurationForRowAt:point:) ๋ฉ์๋๋ฅผ ์ถ๊ฐํด์ค์๋ค.
Step 3
์์์ ์์ฑํ ๋ฉ์๋์์ UIContextMenuConfiguration๋ฅผ ๋ฆฌํด๋ฐ์์ผํฉ๋๋ค. ์ ๋ identifier์ previewProvider๋ ์์ด ๋ง๋ค๊ฑฐ๋ผ nil๊ฐ์ ์ฃผ๊ธฐ๋ก ํ์ต๋๋ค.
...
extension ViewController: UITableViewDataSource, UITableViewDelegate {
...
func tableView(_ tableView: UITableView, contextMenuConfigurationForRowAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
return UIContextMenuConfiguration(identifier: nil,
previewProvider: nil,
actionProvider: {_ in
return self.menu
})
}
}
Step 1
๋ฒํผ์ ๋ฉ๋ด๋ฅผ ์ ์ฉํ๋ ๊ฒ์ ํ ์ด๋ธ ๋ทฐ์ ์ ์ฉํ๋ ๊ฒ๋ณด๋ค ๋ ์ฝ์ต๋๋ค. ์ฐ์ button: UIButton์ ๋ง๋ค์ด์ฃผ์๊ณ viewDidLoad์ button.menu = menu(ํ ์ด๋ธ ๋ทฐ์ ์ ์ฉํ๊ธฐ Step 1์์ ๋ง๋ menu์ ๋๋ค.)๋ฅผ ์ถ๊ฐํด์ค๋๋ค.
class ViewController: UIViewController {
...
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
button.menu = menu
}
}
...
Step 2
์ฌ๊ธฐ๊น์ง๋งํด๋ ์์ฑ์ด์ง๋ง, ๋ฒํผ์ ์งง๊ฒ ๋๋ ์ ๋ ๋ฐ๋ก ๋ฉ๋ด๋ฅผ ๋ณด์ด๊ฒ ํ๊ธฐ์ํด์๋ viewDidLoad์ button.showsMenuAsPrimaryAction = true ๋ฅผ ์ถ๊ฐํด์ค์๋ค.
class ViewController: UIViewController {
...
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
button.menu = menu
button.showsMenuAsPrimaryAction = true
}
}
...
๐ง(์๋ ์ฐธ๊ณ ๋ด์ฉ์ ํตํด ๋ ์์ธํ ์ ๋ณด๋ฅผ ํ์ธํ ์ ์์ต๋๋ค!)
Apple Developer