5G系统之家网站 - 操作系统光盘下载网站!

当前位置: 首页  >  教程资讯  >  系统教程 swift3.0自定义tabbar-Swift3.0自定义tabbar实现方法大揭秘

swift3.0自定义tabbar-Swift3.0自定义tabbar实现方法大揭秘

时间:2024-03-06 来源:网络整理 人气:

自定义tabbar是很多iOS开发者在开发中常遇到的问题。在swift3.0中,自定义tabbar的实现方法有很多种,下面我将为大家介绍一种简单而实用的方法。

1.使用UITabBarControllerDelegate

首先,在你的ViewController类中,需要声明并实例化一个UITabBarControllerDelegate对象,用于监听tabbar的点击事件。然后,通过实现UITabBarControllerDelegate协议中的方法,来自定义tabbar的行为。

swift
class ViewController: UIViewController, UITabBarControllerDelegate {
    override func viewDidLoad(){
        super.viewDidLoad()
        //实例化UITabBarControllerDelegate对象
        let tabBarControllerDelegate = TabBarControllerDelegate()
        //设置代理
        self.tabBarController?.delegate = tabBarControllerDelegate
//实现UITabBarControllerDelegate协议
class TabBarControllerDelegate: NSObject, UITabBarControllerDelegate {
    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController){
        //自定义tabbar点击事件的处理逻辑
        //...

2.自定义TabBarView

接下来,我们需要创建一个自定义的TabBarView,并将其添加到UITabBarControler中。这个自定义的TabBarView可以包含多个按钮,每个按钮对应一个视图控制器。

```swift

class TabBarView: UIView {

override init(frame: CGRect){

super.init(frame: frame)

//添加按钮

let button1 = UIButton(frame: CGRect(x:0,y:0, width: frame.width/3, height: frame.height))

button1.setTitle("Tab1", for:.normal)

button1.addTarget(self, action:#selector(button1Clicked), for:.touchUpInside)

self.addSubview(button1)

let button2 = UIButton(frame: CGRect(x: frame.width/3,y:0, width: frame.width/3, height: frame.height))

button2.setTitle("Tab2", for:.normal)

whatsapp官方下载中文版:https://cjge-manuscriptcentral.com/software/65959.html

教程资讯

系统教程排行

系统教程

    标签arclist报错:指定属性 typeid 的栏目ID不存在。