UIViewController 视图化 popover 箭头形式呈现。
效果图.jpeg
class FourthViewController: UIViewController {
@objc func showPopoverAction(_ sender: UIButton) {
let popoverContentVC = PopoverViewController()
popoverContentVC.preferredContentSize = CGSize(width: kScreenWidth - 20, height: 400)
popoverContentVC.modalPresentationStyle = .popover
guard let popoverPresentationVC = popoverContentVC.popoverPresentationController else { return }
popoverPresentationVC.permittedArrowDirections = .up
popoverPresentationVC.sourceView = self.view
popoverPresentationVC.sourceRect = sender.frame
popoverPresentationVC.delegate = self
present(popoverContentVC, animated: true, completion: nil)
}
}
extension FourthViewController: UIPopoverPresentationControllerDelegate {
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return .none
}
func popoverPresentationControllerShouldDismissPopover(_ popoverPresentationController: UIPopoverPresentationController) -> Bool {
return true
}
func popoverPresentationControllerDidDismissPopover(_ popoverPresentationController: UIPopoverPresentationController) {
}
func prepareForPopoverPresentation(_ popoverPresentationController: UIPopoverPresentationController) {
}
}
import UIKit
import SwiftExpand
import SnapKit
class PopoverViewController: UIViewController {
lazy var button: UIButton = {
let sender = UIButton(type: .custom)
sender.setTitle("UIButton", for: .normal)
sender.setTitleColor(UIColor.yellow, for: .normal)
sender.addActionHandler({ (control) in
guard let sender = control as? UIButton else { return }
DDLog(sender.currentTitle)
}, for: .touchUpInside)
return sender
}()
// MARK: -lifecycle
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.lightGreen
view.addSubview(button)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
button.snp.makeConstraints { (make) in
make.centerX.equalToSuperview().offset(0)
make.top.equalToSuperview().offset(130);
make.width.equalTo(100);
make.height.equalTo(25);
}
}
}
网友评论