IOS 11 通用Base控制器封装

整体规划

BaseController:把viewDidLoad逻辑拆分为三个方法,方便管理。

BaseCommonController:不同项目可以复用的逻辑,例如:设置背景颜色方法等

BaseLogicController:本项目的通用逻辑,主要是创建界面布局容器,例如:快速初始化一个四边都在安全区的垂直方向布局;快速初始化四边都在安全区的TableView布局等。

BaseTitleController:自定义标题相关,例如:快速添加左侧按钮,右侧按钮。

BaseController

Swift 复制代码
//
//  BaseController.swift
//  MyCloudMusic
//
//  Created by jin on 2024/8/19.
//

import UIKit

class BaseController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        initViews()
        initDatum()
        initListeners()
    }
    
    /// 找控件
    func initViews() {

    }

    /// 设置数据
    func initDatum() {

    }

    /// 设置监听器
    func initListeners()  {

    }

}

BaseCommonController

Swift 复制代码
//
//  BaseCommonController.swift
//  MyCloudMusic
//
//  Created by jin on 2024/8/19.
//

import UIKit

class BaseCommonController: BaseController {

    /// 设置背景颜色
    /// - Parameter color: <#color description#>
    func setBackgroundColor(_ color:UIColor) {
        view.backgroundColor = color
    }

}

BaseLogicController

Swift 复制代码
//
//  BaseLogicController.swift
//  MyCloudMusic
//
//  Created by jin on 2024/8/19.
//

import UIKit

//提供类似Android中更高层级布局框架
import TangramKit

class BaseLogicController: BaseCommonController {
    
    /// 根容器
    var rootContainer: TGBaseLayout!
    
    /// 头部容器
    var superHeaderContainer: TGBaseLayout!
    var superHeaderContentContainer: TGBaseLayout!
    
    /// 容器
    var container: TGBaseLayout!
    
    /// 底部容器
    var superFooterContainer: TGBaseLayout!
    var superFooterContentContainer: TGBaseLayout!

    /// 初始化RelativeLayout容器,四边都在安全区内
    func initRelativeLayoutSafeArea() {
        initLinearLayout()
        
        //header
        initHeaderContainer()
        
        //中间内容容器
        container = TGRelativeLayout()
        container.tg_width.equal(.fill)
        container.tg_height.equal(.fill)
        container.backgroundColor = .clear
        rootContainer.addSubview(container)
        
        //footer
        initFooterContainer()
    }
    
    /// 初始化垂直方向LinearLayout容器
    func initLinearLayout() {
        rootContainer = TGLinearLayout(.vert)
        rootContainer.tg_width.equal(.fill)
        rootContainer.tg_height.equal(.fill)
        rootContainer.backgroundColor = .clear
        view.addSubview(rootContainer)
    }
    
    /// 头部容器,安全区外,一般用来设置头部到安全区外背景颜色
    func initHeaderContainer() {
        superHeaderContainer = TGLinearLayout(.vert)
        superHeaderContainer.tg_width.equal(.fill)
        superHeaderContainer.tg_height.equal(.wrap)
        superHeaderContainer.backgroundColor = .clear
        
        //头部内容容器,安全区内
        superHeaderContentContainer = TGLinearLayout(.vert)
        superHeaderContentContainer.tg_height.equal(.wrap)
        superHeaderContentContainer.tg_top.equal(TGLayoutPos.tg_safeAreaMargin)
        superHeaderContentContainer.tg_leading.equal(TGLayoutPos.tg_safeAreaMargin)
        superHeaderContentContainer.tg_trailing.equal(TGLayoutPos.tg_safeAreaMargin)
        superHeaderContentContainer.backgroundColor = .clear
        
        superHeaderContainer.addSubview(superHeaderContentContainer)
        rootContainer.addSubview(superHeaderContainer)
    }

    func initFooterContainer() {
        superFooterContainer = TGLinearLayout(.vert)
        superFooterContainer.tg_width.equal(.fill)
        superFooterContainer.tg_height.equal(.wrap)
        superFooterContainer.backgroundColor = .clear
        
        //底部内容容器,安全区内
        superFooterContentContainer = TGLinearLayout(.vert)
        superFooterContentContainer.tg_height.equal(.wrap)
        superFooterContentContainer.tg_bottom.equal(TGLayoutPos.tg_safeAreaMargin)
        superFooterContentContainer.tg_leading.equal(TGLayoutPos.tg_safeAreaMargin)
        superFooterContentContainer.tg_trailing.equal(TGLayoutPos.tg_safeAreaMargin)
        superFooterContentContainer.backgroundColor = .clear
        
        superFooterContainer.addSubview(superFooterContentContainer)
        rootContainer.addSubview(superFooterContainer)
    }
    
    override func initViews() {
        super.initViews()
        setBackgroundColor(.colorBackground)
    }
}

使用

启动界面使用BaseLogicController,因为他不需要标题控制器。

Swift 复制代码
class SplashController: BaseLogicController {
    override func initViews() {
        super.initViews()
        self.initRelativeLayoutSafeArea()

        ...
    }
}
相关推荐
小小8程序员1 分钟前
iOS 开发核心知识点全解析(面试必备)
ios·面试·职场和发展
2501_9159214331 分钟前
Fiddler抓包工具详解,HTTPHTTPS调试、代理配置与接口分析实战教程
服务器·ios·小程序·fiddler·uni-app·php·webview
AirDroid_cn37 分钟前
小米手机如何开启儿童空间?家长用小米,孩子用iPhone,怎样限制玩手机?
ios·智能手机·iphone·ipad
HX4361 小时前
Swift - Sendable (not just Sendable)
人工智能·ios·全栈
00后程序员张2 小时前
数据流抓包实战指南,TCPUDP 流量分析、HTTPS 解密与多工具协同方案
网络协议·http·ios·小程序·https·uni-app·iphone
小小8程序员2 小时前
iOS开发的面试经验
ios·面试·cocoa
2501_915921432 小时前
iOS 性能分析工具全景解析,构建从底层诊断到真机监控的多层级性能分析体系
android·ios·小程序·https·uni-app·iphone·webview
2501_915909062 小时前
如何防止 IPA 被反编译,从攻防视角构建一套真正有效的 iOS 成品保护体系
android·macos·ios·小程序·uni-app·cocoa·iphone
2501_916007472 小时前
专业的 IPA 处理工具 构建可维护、可回滚的 iOS 成品加工与加固流水线
android·ios·小程序·https·uni-app·iphone·webview