PopoverView是一种临时的视图,以漂浮的形式出现在视图表面,称为浮动层。

API

PopoverPresentationController

  • barButtonItem:指定一个UIBarButtonItem类型按钮作为锚点

  • sourceView:指定一个普视图作为锚点

  • sourceRect:指定一个矩形区域作为锚点

  • permittedArrowDirection:指定锚点箭头的方向(up,down.lwft,right,any,unkown)

UIPopoverPresentationControllerDelegate

  • popoverPresentationControllerShouldDismissPopover:返回true可以消失,false不可消失

  • popoverPresentationControllerDidDismissPopover:销毁时调用

  • func prepareForPopoverPresentation(_ popoverPresentationController: UIPopoverPresentationController):显示时调用

步骤

  1. 创建iOS工程

  2. 拖入一个Button到界面中心,并设置点击事件,点击按钮室弹出popover view

  3. 代码实现

代码实现

//
//  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.
    }

}

效果


刘涤生
243 声望29 粉丝

临渊羡鱼,不如退而结网。