美文网首页
iOS 重构解耦: UIViewController 以 pop

iOS 重构解耦: UIViewController 以 pop

作者: SoaringHeart | 来源:发表于2020-04-22 22:10 被阅读0次

    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);
            }
        }
    }
    

    github

    相关文章

      网友评论

          本文标题:iOS 重构解耦: UIViewController 以 pop

          本文链接:https://www.haomeiwen.com/subject/vmpgihtx.html