iOS开发⑧PopoverView

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

API

PopoverPresentationController

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

  • sourceView:指定一个普视图做为锚点动画

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

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

UIPopoverPresentationControllerDelegate

  • popoverPresentationControllerShouldDismissPopover:返回true能够消失,false不可消失事件

  • popoverPresentationControllerDidDismissPopover:销毁时调用rem

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

步骤

  1. 建立iOS工程io

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

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

}

效果

相关文章
相关标签/搜索