2015-01-07 9 views
18

Ho un'applicazione compatibile sia con i layout iPhone che iPad. Per il layout di iPhone, ho creato Action Sheet e Pop-over per iPad. Il problema è che la freccia di pop-over non punta sul pulsante su cui ho fatto clic. Qui di seguito è il mio codice ....Il pop-over non punta sul pulsante

let actionSheet = UIAlertController(title: "Choose an option", 
      message: "Message", 
      preferredStyle: .ActionSheet) 
... 

if UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Pad 
{ 
    // for iPad 
    actionSheet.popoverPresentationController?.sourceView = self.view 
    actionSheet.popoverPresentationController?.sourceRect = self.view.bounds; 
    actionSheet.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.allZeros; 
} 

self.presentViewController(actionSheet, animated: true, completion: nil) 

risposta

69

Impostare la sourceView e sourceRect come button e button.bounds.
È possibile scegliere le DirettiveArrow consentite in base al layout della vista.

actionSheet.popoverPresentationController?.sourceView = button 
actionSheet.popoverPresentationController?.sourceRect = button.bounds; 
actionSheet.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.Left; 

Se il pulsante è un oggetto BarButton, utilizzare questo codice.

actionSheet.popoverPresentationController?.barButtonItem = button 
actionSheet.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.Up; 
+0

Ok che non ha funzionato o. Il pulsante è in realtà un 'UIBarButtonItem'. Ho cambiato la funzione come '@IBAction func userOptions (button: UIBarButtonItem)' e ha generato un errore: '[UIBarButtonItem bounds]: selettore non riconosciuto inviato' –

+0

controlla la mia risposta modificata @SrujanSimha – rakeshbs

+0

Perfetto! Grazie uomo :) –

2

Per me ha lavorato utilizzando il mittente e il casting come UIView.

alertController.popoverPresentationController?.sourceView = sender as! UIView 
alertController.popoverPresentationController?.sourceRect = sender.bounds 

alertController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.Up 
0

SWIFT 3

questo ha lavorato per me quando il mio tasto è stato un UIBarButtonItem:

if UIDevice.current.userInterfaceIdiom == .pad { 

    if controller.responds(to: "popoverPresentationController") { 
     controller.popoverPresentationController?.barButtonItem = YourUIBarButtonName 
    } 

} 

codice intero snippet di seguito:

func presentActivitySheet() { 

    let controller = UIActivityViewController(activityItems: [document.fileURL], applicationActivities: nil) 

     if UIDevice.current.userInterfaceIdiom == .pad { 

      if controller.responds(to: "popoverPresentationController") { 
      controller.popoverPresentationController?.barButtonItem = YourUIBarButtonName 
      } 

     } 

    present(controller, animated: true, completion: nil) 
} 
Problemi correlati