Swift5 Tabbar的使用
Posted 长沙火山
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Swift5 Tabbar的使用相关的知识,希望对你有一定的参考价值。
Swift Tabbar的使用
一、基本使用
1.1 创建Tabbar
import UIKit
class RootTabbar: UITabBarController
override func viewDidLoad()
super.viewDidLoad()
let nav1 = UINavigationController(rootViewController: ControlMainController())
let nav2 = UINavigationController(rootViewController: BusinessMainController())
let nav3 = UINavigationController(rootViewController: OtherMainClassController())
//设置tabBaritem的标题
nav1.tabBarItem.title = "控件"
nav2.tabBarItem.title = "业务"
nav3.tabBarItem.title = "其他"
//设置tabBaritem的普通状态的图标
nav1.tabBarItem.image = UIImage(named:"tabbar_1")
nav2.tabBarItem.image = UIImage(named:"tabbar_2")
nav3.tabBarItem.image = UIImage(named:"tabbar_3")
//设置tabBaritem选中的图标
nav1.tabBarItem.selectedImage = UIImage(named: "tabbar_1a")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
nav2.tabBarItem.selectedImage = UIImage(named: "tabbar_2a")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
nav3.tabBarItem.selectedImage = UIImage(named: "tabbar_3a")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
//设置tabBaritem的标题文字颜色
self.tabBar.tintColor = UIColor.COLOR(r: 87, g: 173, b: 57)
self.viewControllers = [nav1, nav2, nav3]
二、Tabbar中间添加一个按钮
实现思路:
- 创建三个tabbar,设置第二个tabbar不可点击;
- 自定义一个按钮,添加在tab的中间位置。其实就是创建一个按钮,放在第二个tabbar上面。
代码如下:
class RootTabbar: UITabBarController
override func viewDidLoad()
super.viewDidLoad()
let nav1 = UINavigationController(rootViewController: ControlMainController())
let nav2 = UINavigationController(rootViewController: BusinessMainController())
let nav3 = UINavigationController(rootViewController: OtherMainClassController())
//设置tabBaritem的标题
nav1.tabBarItem.title = "控件"
nav2.tabBarItem.title = "业务"
nav3.tabBarItem.title = "其他"
//设置tabBaritem的普通状态的图标
nav1.tabBarItem.image = UIImage(named:"tabbar_1")
nav2.tabBarItem.image = UIImage(named:"tabbar_2")
nav3.tabBarItem.image = UIImage(named:"tabbar_3")
//设置tabBaritem选中的图标
nav1.tabBarItem.selectedImage = UIImage(named: "tabbar_1a")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
nav2.tabBarItem.selectedImage = UIImage(named: "tabbar_2a")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
nav3.tabBarItem.selectedImage = UIImage(named: "tabbar_3a")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
nav2.tabBarItem.isEnabled = false;
let button:UIButton = UIButton.init(frame: CGRect(x: 100, y: 0, width: 50, height: 50))
button.setTitle("+", for: UIControl.State.normal)
button.setTitleColor(UIColor.white, for: UIControl.State.normal)
button.backgroundColor = UIColor.orange
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 35)
button.addTarget(self, action: #selector(buttonClick(button:)), for: .touchUpInside)
button.layer.cornerRadius = 25
button.centerX = SCREEN_WIDTH/2;
self.tabBar.addSubview(button)
//设置tabBaritem的标题文字颜色
self.tabBar.tintColor = UIColor.COLOR(r: 87, g: 173, b: 57)
self.viewControllers = [nav1, nav2, nav3]
@objc func buttonClick(button:UIButton)
button.setTitleColor(UIColor.green, for: UIControl.State.normal)
以上是关于Swift5 Tabbar的使用的主要内容,如果未能解决你的问题,请参考以下文章
Swift5 如何以不同的格式格式化 dateTime 字符串