PopoverView是一種臨時的視圖,以漂浮的形式出如今視圖表面,稱爲浮動層。swift
barButtonItem:指定一個UIBarButtonItem類型按鈕做爲錨點ide
sourceView:指定一個普視圖做爲錨點動畫
sourceRect:指定一個矩形區域做爲錨點spa
permittedArrowDirection:指定錨點箭頭的方向(up,down.lwft,right,any,unkown)code
popoverPresentationControllerShouldDismissPopover:返回true能夠消失,false不可消失事件
popoverPresentationControllerDidDismissPopover:銷燬時調用rem
func prepareForPopoverPresentation(_ popoverPresentationController: UIPopoverPresentationController):顯示時調用it
建立iOS工程io
拖入一個Button到界面中心,並設置點擊事件,點擊按鈕室彈出popover viewclass
代碼實現
// // ViewController.swift // PopoverViewSample // // Created by Michael on 2016/11/17. // Copyright © 2016年 Michael. All rights reserved. // import UIKit class ViewController: UIViewController,UIPopoverPresentationControllerDelegate { @IBOutlet weak var mBtn: UIButton! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } @IBAction func show(_ sender: Any) { //做爲popover View let controller = SelectViewController(); //設置爲popover視圖 controller.modalPresentationStyle = .popover //視圖動畫 controller.modalTransitionStyle = .crossDissolve controller.preferredContentSize = CGSize(width: 300, height: 100) let popController = controller.popoverPresentationController popController?.sourceView = mBtn popController?.sourceRect = mBtn.bounds popController?.delegate = self self.present(controller, animated: true, completion: nil) } func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle { //popover樣式 return .none } func prepareForPopoverPresentation(_ popoverPresentationController: UIPopoverPresentationController) { NSLog("show") } func popoverPresentationControllerDidDismissPopover(_ popoverPresentationController: UIPopoverPresentationController) { NSLog("hide") } func popoverPresentationControllerShouldDismissPopover(_ popoverPresentationController: UIPopoverPresentationController) -> Bool { return true } }
// // SelectViewController.swift // PopoverViewSample // // Created by Michael on 2016/11/17. // Copyright © 2016年 Michael. All rights reserved. // import UIKit class SelectViewController: UIViewController { var label1:UILabel! var label2:UILabel! override func viewDidLoad() { super.viewDidLoad() self.view.frame = CGRect(x: 0, y: 0, width: 200, height: 500) // label1 = UILabel(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 200)) // label2 = UILabel(frame: CGRect(x: 0, y: 300, width: self.view.frame.width, height: 200)) // self.view.addSubview(label1) // self.view.addSubview(label2) self.view.backgroundColor = UIColor.blue } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }