Swift 4 Qui è completamente codice di lavoro. Così qui si vedrà finestra pop-up con dimensioni di 250x250:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var button: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// in case if you don't want to make it via IBAction
button.addTarget(self, action: #selector(tapped), for: .touchUpInside)
}
@objc
private func tapped() {
guard let popVC = storyboard?.instantiateViewController(withIdentifier: "popVC") else { return }
popVC.modalPresentationStyle = .popover
let popOverVC = popVC.popoverPresentationController
popOverVC?.delegate = self
popOverVC?.sourceView = self.button
popOverVC?.sourceRect = CGRect(x: self.button.bounds.midX, y: self.button.bounds.minY, width: 0, height: 0)
popVC.preferredContentSize = CGSize(width: 250, height: 250)
self.present(popVC, animated: true)
}
}
// This is we need to make it looks as a popup window on iPhone
extension ViewController: UIPopoverPresentationControllerDelegate {
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return .none
}
}
tener attenzione che è necessario fornire identificatore popVC a uno viewController si vuole presentare come un popup.
Spero che questo aiuti!
fonte
2018-01-26 03:23:35
Grazie per questo! Il primo esempio funziona, ma sembra che non sia la dimensione. Il popover appare a schermo intero. Penso che il secondo modo in cui hai spiegato sarebbe un modo migliore .. Ma ancora una volta ho ricevuto un errore non potendo rispettare la connessione di ancoraggio IBCocoaTouchoutlet View. Potresti darmi un codice che corrisponda al secondo modo in cui hai spiegato? – user4671001
@ user4671001 Non capisco, in secondo luogo non è necessario impostare l'anchoView per nulla –
popover si presenta come modal a schermo intero su iPhone, è possibile disabilitare questo aggiungendo questa funzione: 'func adaptivePresentationStyleForPresentationController (PC: UIPresentationController!) - > UIModalPresentationStyle { // Questo * impone * la visualizzazione di un popover su iPhone return .Nessuno } ' – user1076813