Skip to content

Instantly share code, notes, and snippets.

@cruinh
Last active December 3, 2017 22:33
Show Gist options
  • Save cruinh/7e76334fbc21aa319a5bfb9fd6900db9 to your computer and use it in GitHub Desktop.
Save cruinh/7e76334fbc21aa319a5bfb9fd6900db9 to your computer and use it in GitHub Desktop.
//Add this to a view controller
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 75, height: 20))
var bbItem : UIBarButtonItem!
extension SettingsViewController : UIPopoverPresentationControllerDelegate {
fileprivate func _setupPopoverTest() {
bbItem = UIBarButtonItem(customView: button)
button.setTitle("Popover", for: .normal)
button.setTitleColor(.black, for: .normal)
button.addTarget(self, action: #selector(showPopover), for: .touchUpInside)
self.navigationItem.setRightBarButton(bbItem, animated: true)
}
func showPopover() {
let vc = UIViewController()
vc.modalPresentationStyle = .popover
vc.view.backgroundColor = UIColor.magenta
vc.preferredContentSize = CGSize(width: 200, height: 200)
vc.popoverPresentationController?.delegate = self
vc.popoverPresentationController?.permittedArrowDirections = .up
// vc.popoverPresentationController?.barButtonItem = bbItem
vc.popoverPresentationController?.sourceView = button
vc.popoverPresentationController?.sourceRect = button.bounds
self.present(vc, animated: false, completion: nil)
}
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return .none
}
}
@cruinh
Copy link
Author

cruinh commented Mar 13, 2017

screen shot 2017-03-13 at 6 02 37 pm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment