美文网首页
iOS 封装:极简/轻量级下拉列表按钮 NNPopoverBut

iOS 封装:极简/轻量级下拉列表按钮 NNPopoverBut

作者: SoaringHeart | 来源:发表于2020-04-23 13:38 被阅读0次

基于系统原生 UIPopoverController 极简封装,关联到按钮(UIPopoverController是一个容器控制器,其中需要承载一个ViewControler作为内容视图)

WechatIMG388.jpeg
WechatIMG389.jpeg
WechatIMG391.jpeg
WechatIMG392.jpeg
import UIKit
import NNPopoverButton

class HomeViewController: UIViewController {

    lazy var leftBtn: NNPopoverButton = {
        let button = NNPopoverButton(type: .custom)
        button.setTitle("Left", for: .normal)
        button.setTitleColor(UIColor.systemBlue, for: .normal)
        button.sizeToFit()
        button.parentVC = self
        button.contentWidth = 200
        button.list = ["0", "1", "2", "3", "4", "5", "6",]
                
        button.addTarget(self, action: #selector(showPopoverAction(_:)), for: .touchUpInside)
        return button
    }()
    
    lazy var rightBtn: NNPopoverButton = {
        let button = NNPopoverButton(type: .custom)
        button.setTitle("Right", for: .normal)
        button.setTitleColor(UIColor.systemBlue, for: .normal)
        button.sizeToFit()
        button.parentVC = self
        button.list = ["00", "11", "22", "33", "44", "55", "66",]

        button.addTarget(self, action: #selector(showPopoverAction(_:)), for: .touchUpInside)
        return button
    }()
    
    lazy var btn: NNPopoverButton = {
        let button = NNPopoverButton(type: .custom)
        button.setTitle("UIButton", for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        button.setTitleColor(UIColor.white, for: .normal)
        button.backgroundColor = UIColor.systemBlue
        
        button.sizeToFit()
        button.parentVC = self
        button.list = ["0", "1", "2", "3", "4", "5", "6","7", "8", "9", "10", "11", "12","13", "14", "15", "16", "17", "18", "19", "20",]

        button.addTarget(self, action: #selector(showPopoverAction(_:)), for: .touchUpInside)
        return button
    }()
    
    // MARK: -lifecycle
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
//        title = "主界面"
//        title = NSLocalizedString("主界面", comment: "")
        title = Bundle.localizedString(forKey: "主界面")
        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: leftBtn)
        navigationItem.rightBarButtonItem = UIBarButtonItem(customView: rightBtn)

        btn.frame = CGRect(x: 0, y: 0, width: 80, height: 40)
        btn.center = view.center
        view.addSubview(btn)
        
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    // MARK: -funtions
    @objc func showPopoverAction(_ sender: NNPopoverButton) {
        sender.presentPopover()
    }

}

extension HomeViewController: NNPopoverButtonDelegate {
    public func popoverButton(_ popoverBtn: NNPopoverButton, tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("\(#function):\(indexPath.row)")
    }
}

NNPopoverButton
备注:如需要自定义cell, 将popoverBtn.popoverContentVC.tableView 的代理自己实现即可,别忘了调用代理回调,非常简单。

相关文章

网友评论

      本文标题:iOS 封装:极简/轻量级下拉列表按钮 NNPopoverBut

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