再读斋

iOS开发之08PopoverView

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. 代码实现

代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//
// 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
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//
// 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.
}
}

效果

刘涤生 wechat