美文网首页
iOS 重构解耦: UIViewController 高级自定义

iOS 重构解耦: UIViewController 高级自定义

作者: SoaringHeart | 来源:发表于2020-04-17 18:08 被阅读0次

随着项目中的各种自定义弹窗越来越多(底部,右侧,左侧,中心),如果仍然采用 UIView 自定义,虽能实现但是会导致对应的 UIViewController 代码行数剧增,如果业务代码实现在UIView 里,又违背了视图复用原则,遂研究 UIViewController 容器的自定义,经过一段时间的学习,最终完美实现半屏显示:HFNavigationController
在此库基础上,可以将 UIViewController 视图化,实现各种各样的效果,而且代码完美的分布在各自的控制器中。

WechatIMG375.jpeg
import SwiftExpand
import HFNavigationController

class ThirdViewController: UIViewController{

    let frameCenter = CGRect(x: UIScreen.main.bounds.width*0.05,
                             y: UIScreen.main.bounds.height*0.25,
                             width: UIScreen.main.bounds.width*0.9,
                             height: 300)
    
    lazy var textController: NNAgreementAlertController = {
        let controller = NNAgreementAlertController()
//        controller.actionTitles = ["one", "two", "three"]
//        controller.actionTitles = ["one", ]
        controller.actionTitles = ["暂不使用", "同意"]
        controller.delegate = self
        return controller
    }()
    
    lazy var navController: HFNavigationController = {
        let controller = HFNavigationController(rootViewController: textController)
        controller.modalPresentationStyle = .custom
        controller.modalTransitionStyle = .crossDissolve
        controller.transitioningDelegate = controller as UIViewControllerTransitioningDelegate
        
        controller.view.layer.cornerRadius = 18
        controller.view.layer.masksToBounds = true
        
        controller.setupDefaultFrame(self.frameCenter)

        return controller;
    }()
    
    // MARK: -lifecycle
    override func viewDidLoad() {
        super.viewDidLoad()
        
        createBarItem( .done, isLeft: false) { (sender: AnyObject) in
//            self.goController("IOPAuthDetailController", obj: nil, objOne: nil)
            guard let rootViewController = UIApplication.shared.keyWindow?.rootViewController else { return }
            rootViewController.present(self.navController, animated: true, completion: nil)
        }
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
                
    }
}

extension ThirdViewController: NNAgreementAlertControllerDelegate{
    func agreementAlertVC(_ controller: NNAgreementAlertController, sender: UIButton) {
        DDLog(sender.currentTitle, sender.tag)
        switch sender.tag {
        case 0:
            exit(0)
        default:
            controller.dismiss(animated: true, completion: nil)
        }
    }
    
    func agreementAlertTextView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {
        DDLog(URL.absoluteString)
        if URL.scheme?.contains("_") {
            guard let urlString = URL.absoluteString.components(separatedBy: "_").last else { return false}
            UIApplication.openURLStr(urlString, prefix: "http://")
        }
 
        return true
    }
  
}

github:调用

github:实现

相关文章

网友评论

      本文标题:iOS 重构解耦: UIViewController 高级自定义

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