UIPopoverPresentationController on iPhone doesn't produce popover

后端 未结 4 1522
孤独总比滥情好
孤独总比滥情好 2020-11-27 15:37

I\'m trying to implement the new UIPopoverPresentationController in my iPhone app (using Objective C). What I want is a simple popover with a tableview that ema

4条回答
  •  孤独总比滥情好
    2020-11-27 16:14

    Apparently the above method no longer works with iOS9/Xcode7. This is because if you set the segue style to "Popover" using Interface Builder, Xcode ignores it when it compiles your application. Furthermore, it automatically sets the segue back to "Push" the next time you open your project. If you have version control software like Git, you'll be able to observe this unwanted change being made.

    However, it's still possible to get iPad-style popovers on the iPhone if you manually present the view controller that you want to show as a popover. Example Swift code:

    //  ViewController.swift
    //  PopoverDemo
    //
    //  Created by bhnascar on 12/2/15.
    //  Copyright © 2015 bhnascar. All rights reserved.
    //
    
    import UIKit
    
    class ViewController: UIViewController, UIPopoverPresentationControllerDelegate {
    
        /* The bar button item that will present the popover. */
        var popoverButton: UIBarButtonItem?
    
        override func viewDidLoad() {
            super.viewDidLoad()
            popoverButton = UIBarButtonItem(title: "Pop!", style: UIBarButtonItemStyle.Plain, target: self, action: "presentPopover")
            self.navigationItem.rightBarButtonItem = popoverButton
        }
    
        // Mark: - UIPopoverPresentationControllerDelegate
    
        func prepareForPopoverPresentation(popoverPresentationController: UIPopoverPresentationController) {
            popoverPresentationController.permittedArrowDirections = .any
            popoverPresentationController.barButtonItem = popoverButton
        }
    
        func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
            return .none
        }
    
        // Mark: - Callback function for popover button.
    
        func presentPopover() {
            let popoverContentController = UIViewController()
            popoverContentController.view.backgroundColor = .blue
    
            // Set your popover size.
            popoverContentController.preferredContentSize = CGSize(width: 300, height: 300)
    
            // Set the presentation style to modal so that the above methods get called.
            popoverContentController.modalPresentationStyle = .popover
    
            // Set the popover presentation controller delegate so that the above methods get called.
            popoverContentController.popoverPresentationController!.delegate = self
    
            // Present the popover.
            self.present(popoverContentController, animated: true, completion: nil)
        }
    
    }
    

提交回复
热议问题