iPhone popup menu like iPad popover?

前端 未结 8 2170
醉话见心
醉话见心 2020-11-27 02:58

How can i implement this popup menu in iphone app like a popover in ipad?

\"alt


ED

相关标签:
8条回答
  • 2020-11-27 03:38

    To get a popover from a right side bar button item on a navigation controller that is part of a tableview controller, the following worked for me for Swift 4 and Xcode 9.

    1. Follow the steps in Suragch answer above (as edited by the Community.)
    2. Do not implement the Segue as shown in the answer above. For some reason, the segue causes the popover to go full screen despite setting the explicit size.
    3. Give your popover view controller a title in Attributes Inspector
    4. Add the following code in the TableView controller where the popup will show.
    5. Modify the string identifier (the one here is referencing a Constant.swift file)
    6. Modify "as! FilterVC" to use the title of the your popover view controller.

      /// Shows a filter popover view
      @IBAction func filterBtnPressed(_ sender: UIBarButtonItem) {
          let popover = storyboard?.instantiateViewController(withIdentifier: FILTER_VC) as! FilterVC
          popover.modalPresentationStyle = UIModalPresentationStyle.popover
          popover.popoverPresentationController?.backgroundColor = UIColor.green
          popover.popoverPresentationController?.delegate = self
          popover.popoverPresentationController?.backgroundColor = ColorPalette.Blue.Medium
          popover.popoverPresentationController?.sourceView = self.view
          popover.popoverPresentationController?.sourceRect = CGRect(x: self.view!.bounds.width, y: 0, width: 0, height: 0)
          popover.popoverPresentationController?.permittedArrowDirections = .up
          self.present(popover, animated: true)
      } }
      
      
      func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
          return UIModalPresentationStyle.none
      }
      
    0 讨论(0)
  • 2020-11-27 03:38

    You can check WYPopoverController: https://github.com/sammcewan/WYPopoverController

    0 讨论(0)
  • 2020-11-27 03:40

    The screenshot above is not a UIActionSheet. It looks like a simple UIView subclass with custom UIButtons on top of it. So go ahead and create the subclass according to your needs and then add it as a subview to your view every time you need it.

    0 讨论(0)
  • 2020-11-27 03:41

    You would have to manually instantiate a UIView using a custom background image or drawing with transparency, add some UIButtons (or other type of custom view) on top, and also somehow handle all touches outside that view.

    Note that is is non-standard UI. An actionsheet would be more HIG compliant.

    0 讨论(0)
  • 2020-11-27 03:47

    There is one that is even better than WEPopover. Developed by a company called 50pixels, it is called FPPopover.

    You can download FPPopover at https://github.com/50pixels/FPPopover

    0 讨论(0)
  • 2020-11-27 03:58

    iOS 8 and later

    Beginning with iOS 8, you can use UIPopoverPresentationController for iPhones in addition to iPads.

    Setup

    • Add a UIBarButtonItem to your main View Controller.
    • Add another View Controller to the storyboard. Change it to the size that you want the popover to be and add any content that you want it to have. For my example I just added a UILabel. If you want a whole menu, then just add a table view or list of buttons.
    • Add a segue from the bar button item to the view controller that you will use as the popover. Rather than show, choose Present as Popover.

    • Select the segue in the storyboard and set the identifier to popoverSegue (or whatever string you called it in the code).

    • In the Attributes inspector for the popover view controller, check Use Preferred Explicit Size and confirm that it is the size you want it to be.

    Code

    This is the code for the main view controller that has the bar button item in it.

    class ViewController: UIViewController, UIPopoverPresentationControllerDelegate {
    
        override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
            if segue.identifier == "popoverSegue" {
    
                let popoverViewController = segue.destinationViewController
                popoverViewController.modalPresentationStyle = UIModalPresentationStyle.Popover
                popoverViewController.popoverPresentationController!.delegate = self
    
            }
        }
    
        // MARK: - UIPopoverPresentationControllerDelegate method
    
        func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
    
            // Force popover style
            return UIModalPresentationStyle.None
        }
    }
    

    Popover at an arbitrary anchor point

    If you want to set the popover to appear somewhere besides a bar button item (on a UIButton for example) then you need to set the sourceView and sourceRect. See this answer for details.

    Further reading

    The above example comes mostly from the first link.

    • iPad Style Popovers on the iPhone with Swift
    • iOS 8 Popover Presentations
    • UIPopoverPresentationController on iOS 8 iPhone
    • General overview of popup options in iOS
    0 讨论(0)
提交回复
热议问题