惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
人人都是产品经理
人人都是产品经理
H
Hacker News: Front Page
Stack Overflow Blog
Stack Overflow Blog
B
Blog
I
InfoQ
GbyAI
GbyAI
T
The Blog of Author Tim Ferriss
F
Fortinet All Blogs
Y
Y Combinator Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
月光博客
月光博客
Hugging Face - Blog
Hugging Face - Blog
爱范儿
爱范儿
F
Full Disclosure
Hacker News - Newest:
Hacker News - Newest: "LLM"
Recent Announcements
Recent Announcements
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Jina AI
Jina AI
T
Tailwind CSS Blog
S
Secure Thoughts
P
Privacy International News Feed
美团技术团队
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
L
LINUX DO - 最新话题
H
Hackread – Cybersecurity News, Data Breaches, AI and More
C
Cybersecurity and Infrastructure Security Agency CISA
Last Week in AI
Last Week in AI
W
WeLiveSecurity
Google Online Security Blog
Google Online Security Blog
P
Privacy & Cybersecurity Law Blog
D
DataBreaches.Net
Engineering at Meta
Engineering at Meta
Know Your Adversary
Know Your Adversary
P
Palo Alto Networks Blog
I
Intezer
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Project Zero
Project Zero
V2EX - 技术
V2EX - 技术
H
Heimdal Security Blog
博客园 - Franky
阮一峰的网络日志
阮一峰的网络日志
D
Darknet – Hacking Tools, Hacker News & Cyber Security
T
Troy Hunt's Blog
V
Vulnerabilities – Threatpost
H
Help Net Security
Martin Fowler
Martin Fowler
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
G
GRAHAM CLULEY
博客园 - 【当耐特】

博客园 - wjwdive

如何使用git submodule拆分大的git项目 MAC多github账号配置步骤 记录一次flutter boost5.0.2示例调试的过程 【算法】可获得的最大点数问题 使用1panel一键建站 关于混合加密(AES+RSA) C++设计模式汇总 设计模式C++005__桥模式 设计模式C++007__抽象工厂方法模式 设计模式C++004__装饰器模式 设计模式C++003__观察者模式 设计模式C++002__策略模式 设计模式C++001__模板方法 使用bundler自动化构建Xcode项目时遇到的问题。 AI图片生成网站 suno AI编曲创作利器 iOS组件化开发之私有库 iOS、Android获取apk公钥MD5信息 阿里图标库批量下载iOS适配的图标 C语言实现回调函数标准方式 swift 可选类型
swift 自定义tabbar为基本结构的项目
wjwdive · 2023-04-18 · via 博客园 - wjwdive

swift 自定义tabbar
1、Xcode新建一个项目,选择布局方式为storyBoard, 语言为swift
2、打开项目,新建一个cocoa文件,继承于TabBarViewController,名字命名为BaseTabBarViewController
3、勾选main storyboard,选中右侧窗口第四个检查项,将Class 关联到BaseTabBarViewController
4、新建4个根模块的字控制器,【HomeViewController,RecomentViewController,FavoriteViewController,MyViewController】编写BaseTabBarViewController的代码

import Foundation
import UIKit

class BaseTabBarViewController : UITabBarController {
//    @IBOutlet weak var baseTabbvar: UITabBar!
    
    override func viewDidLoad() {
        super.viewDidLoad()
//        self.tabBar.tintColor = .lightGray//图片和文字都为成为该颜色,图片原来的颜色会改变
        // 创建四个子视图控制器
        let vc1 = createNavigationController(for: HomeViewController())
        let vc2 = createNavigationController(for: RecomentViewController())
        let vc3 = createNavigationController(for: FavoriteViewController())
        let vc4 = createNavigationController(for: MyViewController())
        // 分别设置子视图控制器的 tabBarItem
        vc1.tabBarItem = UITabBarItem.init(title: "首页", image: UIImage(named: "homepage_blue") ,selectedImage: UIImage(named: "homepage_fill_blue"))
        vc2.tabBarItem = UITabBarItem.init(title: "推荐", image: UIImage(named: "activity_blue"), selectedImage: UIImage(named: "activity_fill_blue"))
        vc3.tabBarItem = UITabBarItem.init(title: "喜欢", image: UIImage(named: "like_blue"), selectedImage: UIImage(named: "like_fill_blue"))
        vc4.tabBarItem = UITabBarItem.init(title: "我的", image: UIImage(named: "people_blue") ,selectedImage: UIImage(named: "people_fill_blue"))
        // 将子视图控制器添加到 tabBarController 中
        viewControllers = [vc1, vc2, vc3, vc4]
    }
    
    // 创建一个包装好的 UINavigationController,传入需要包装的控制器
    func createNavigationController(for viewController: UIViewController) -> UINavigationController {
        let navController = UINavigationController(rootViewController: viewController)
        return navController
    }
}

我们在Home页建立一个tableView,菜单里是自页面的名称。
代码如下: ["HitoryTodayViewController", "HuangLiViewController"]是子页面,我们也要新建对应的控制器

import Foundation
import UIKit

class HomeViewController : BaseViewController, UITableViewDelegate, UITableViewDataSource {
    
    var _tableView: UITableView?
    var meunItems: Array<String> = ["HitoryTodayViewController", "HuangLiViewController"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "首页"
        self.view.backgroundColor = .white
        
//        meunItems.addObject("HitoryTodayViewController")
        
        _tableView = UITableView(frame: self.view.bounds, style: .plain)
        self.view .addSubview(_tableView!)
        _tableView?.delegate = self
        _tableView?.dataSource = self
        _tableView?.frame = self.view.bounds
    }
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return meunItems.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "cellID")
        if(cell == nil) {
            cell = UITableViewCell(style: .default, reuseIdentifier: "cellID")
        }
        cell!.textLabel?.text = "\(indexPath.row)" + "\(meunItems[indexPath.row])"
        return cell!
    }
    
    //didSelectRowAt
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("tableView didSelectRowAt、 \(indexPath.row)")
        
        let classStr = meunItems[indexPath.row]
        // 3.通过Class创建对象
        let vc = vcClassFromString(classStr)
        
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    
    func vcClassFromString(_ className:String) -> UIViewController{
        //1、获swift中的命名空间名
        var name = Bundle.main.object(forInfoDictionaryKey: "CFBundleExecutable") as? String
        //2、如果包名中有'-'横线这样的字符,在拿到包名后,还需要把包名的'-'转换成'_'下横线
        name = name?.replacingOccurrences(of: "-", with: "_")
        //3、拼接命名空间和类名,”包名.类名“
        let fullClassName = name! + "." + className
        //通过NSClassFromString获取到最终的类,由于NSClassFromString返回的是AnyClass,这里要转换成UIViewController.Type类型
        guard let classType = NSClassFromString(fullClassName) as? UIViewController.Type  else{
            fatalError("转换失败")
        }
        return classType.init()
    }
    
    
    override func viewDidLayoutSubviews() {
        
    }
}

其中网络请求和根据字符串返回类名需要做一下Info.plist的配置:

	<key>NSAppTransportSecurity</key>
	<dict>
		<key>NSAllowsArbitraryLoads</key>
		<true/>
	</dict>
	<key>CFBundleExecutable</key>
	<string>swiftUIKitdemo</string>