Reputation: 8787
How can I create this popover ViewController
style in iOS? How can I make it so it fits it's content and does not exceed the contents frame?
I tried to change the modalPresentation
to .popover
but it only works on iPad and macOS and not on the iPhone as far as I tried. I hope someone can help
Upvotes: 4
Views: 14073
Reputation: 230
This worked for me. This may help you.
var dropDownView : DropDownView?
Setup the dropdown view
dropDownView = Storyboard.Main.instantiateViewController(withIdentifier: "DropDownView") as? DropDownView
self.dropDownView.delegate = self
self.dropDownView?.preferredContentSize = CGSize(width: 200, height: CGFloat((dropDownView.listArr.count) * 35))
let presentationController = AlwaysPresentAsPopover.configurePresentation(forController: self.dropDownView!)
presentationController.sourceView = sender
presentationController.sourceRect = sender.bounds
presentationController.permittedArrowDirections = [.down, .up]
self.present(self.dropDownView!, animated: true)
//MARK:- Delegate method of present controller : Opens Dropdown view on click of view more icon
class AlwaysPresentAsPopover : NSObject, UIPopoverPresentationControllerDelegate {
private static let sharedInstance = AlwaysPresentAsPopover()
private override init() {
super.init()
}
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return .none
}
static func configurePresentation(forController controller : UIViewController) -> UIPopoverPresentationController {
controller.modalPresentationStyle = .popover
let presentationController = controller.presentationController as! UIPopoverPresentationController
presentationController.delegate = AlwaysPresentAsPopover.sharedInstance
return presentationController
}
}
Upvotes: -3
Reputation: 3291
You have to present a new ViewController in .popover presentation.
Then you customize the presented view controller like you want.
The main View controller should look like this:
class ViewController: UIViewController {
@IBAction func buttonClicked(_ sender: Any) {
//get the button frame
/* 1 */
let button = sender as? UIButton
let buttonFrame = button?.frame ?? CGRect.zero
/* 2 */
//Configure the presentation controller
let popoverContentController = self.storyboard?.instantiateViewController(withIdentifier: "PopoverContentController") as? PopoverContentController
popoverContentController?.modalPresentationStyle = .popover
/* 3 */
// Present popover
if let popoverPresentationController = popoverContentController?.popoverPresentationController {
popoverPresentationController.permittedArrowDirections = .up
popoverPresentationController.sourceView = self.view
popoverPresentationController.sourceRect = buttonFrame
popoverPresentationController.delegate = self
if let popoverController = popoverContentController {
present(popoverController, animated: true, completion: nil)
}
}
}
}
extension ViewController: UIPopoverPresentationControllerDelegate {
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return .none
}
func popoverPresentationControllerDidDismissPopover(_ popoverPresentationController: UIPopoverPresentationController) {
}
func popoverPresentationControllerShouldDismissPopover(_ popoverPresentationController: UIPopoverPresentationController) -> Bool {
return true
}
}
PopoverContentController where you will add your TableView for example
class PopoverContentController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
// Custom design&implementation
}
Upvotes: 16