How can I recreate this type of menu in Swift using UIkit? Is it possible to play the same animation as well?
-
it's iOS 14 Pull Down menus developer.apple.com/design/human-interface-guidelines/ios/…Kstin– Kstin2020年08月27日 08:36:04 +00:00Commented Aug 27, 2020 at 8:36
-
you can check here: developer.apple.com/documentation/uikit/menus_and_shortcuts/…goat_herd– goat_herd2020年08月27日 08:36:52 +00:00Commented Aug 27, 2020 at 8:36
2 Answers 2
Try setting button.showsMenuAsPrimaryAction = true to your button. And don't forget to assign menu property.
answered Oct 18, 2024 at 17:46
Dewerro
6371 gold badge7 silver badges20 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Okay, enjoy
class ViewController: UIViewController {
let filterButton = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
filterButton.setImage(UIImage(systemName: "ellipsis.circle"), for: .normal)
filterButton.tintColor = .blue
filterButton.addTarget(self, action: #selector(showFilters), for: .touchUpInside)
filterButton.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(filterButton)
NSLayoutConstraint.activate([
filterButton.centerXAnchor.constraint(equalTo: view.centerXAnchor),
filterButton.centerYAnchor.constraint(equalTo: view.centerYAnchor)
])
}
@objc func showFilters() {
let alertController = UIAlertController( title: nil, message: nil, preferredStyle: .actionSheet)
let scanButton = UIAlertAction(title: "Scan document", style: .default) { _ in
print(" 1 selected")
}
let connectButton = UIAlertAction(title: "Connect to Server", style: .default) { _ in
print(" 2 selected")
}
let editButton = UIAlertAction(title: "Edit", style: .default) { _ in
print(" 3 selected")
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alertController.addAction(scanButton)
alertController.addAction(connectButton)
alertController.addAction(editButton)
alertController.addAction(cancelAction)
alertController.popoverPresentationController?.sourceView = filterButton
alertController.popoverPresentationController?.sourceRect = filterButton.bounds
alertController.popoverPresentationController?.permittedArrowDirections = .up
present(alertController, animated: true, completion: nil)
}
}
1 Comment
Chait
Hello, and welcome to Stack Overflow! Please add some explanation to your answer as what the code does and how it fixes the problem.
lang-swift