实现这种效果:
popView是一个VC,里边搞了一个tableView ,主视图是ViewController,在里边对这个popVC进行折腾。在这里这个popView的大小是通过调整UIViewController的这个属性:
//The preferred size for the view controller’s view.
self.preferredContentSize = CGSizeMake(150, 305)
接下来是ViewController.swift
import UIKit
class ViewController: UIViewController,UIPopoverPresentationControllerDelegate {
// 自定义的popVC
var itemPopVC:XXPopViewController!
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.whiteColor()
self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(barButtonSystemItem: UIBarButtonSystemItem.Add, target: self, action: #selector(ViewController.actionPop))
}
// nav的右边按钮添加的action
func actionPop() {
self.itemPopVC = XXPopViewController()
// 设置显示的模态
self.itemPopVC.modalPresentationStyle = .Popover
// 设置弹出的角放在哪个位置,也可以使用sourceView与sourceRect指定位置
self.itemPopVC.popoverPresentationController?.barButtonItem = self.navigationItem.rightBarButtonItem
// 设置尖尖角指向
self.itemPopVC.popoverPresentationController?.permittedArrowDirections = .Up
// 设置代理
self.itemPopVC.popoverPresentationController?.delegate = self
self.presentViewController(itemPopVC, animated: true, completion: nil)
}
// UIPopoverPresentationControllerDelegate代理方法
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
print(controller)
return .None
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}