美文网首页
swift 设置tabbar和导航控制器(navigationC

swift 设置tabbar和导航控制器(navigationC

作者: fulen | 来源:发表于2016-11-28 15:54 被阅读587次
IMG_0001.JPG

1.纯代码搭建一个结构为>Root -> TabBar -> Navigation -> ViewController的小项目

下面看一下appdelegate.swift里面的代码

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        self.window = UIWindow(frame:UIScreen.main.bounds)        
        self.window?.makeKeyAndVisible()

        let rootViewController = MianTabbarController()
        
        self.window?.rootViewController = rootViewController
        
        
        return true
    }

来到MianTabbarController.swift里面,先创建两个空的控制器HomeViewController.swift和SettingViewController.swift,分别设置好颜色

super.viewDidLoad()

        // 创建ViewControllers
        self.creatViewController()
    }

 func creatViewController(){
        let home = HomeViewController()
        let homeNvc = UINavigationController(rootViewController:home)
        home.title = "HomeViewController"

        let setting = SeetingViewController()
        let settingNvc = UINavigationController(rootViewController:setting)
        setting.title = "SettingViewController"
        
        let tabArray = [homeNvc,settingNvc]
        self.viewControllers = tabArray
       
        //底部工具栏背景颜色
        self.tabBar.barTintColor=UIColor.gray;
        //.设置底部工具栏文字颜色(默认状态和选中状态)(选中状态为蓝色)
        UITabBarItem.appearance().setTitleTextAttributes(NSDictionary(object:UIColor.white, forKey:NSForegroundColorAttributeName as NSCopying) as? [String : AnyObject], for:UIControlState.normal);
        UITabBarItem.appearance().setTitleTextAttributes(NSDictionary(object:UIColor.blue, forKey:NSForegroundColorAttributeName as NSCopying) as? [String : AnyObject], for:UIControlState.selected)
        
    }

效果如图1.1


图1.1

二,结构为Navgation->Root -> TabBar -> Navigation -> ViewController,这里是在appdelegate.swift里面创建了一个navigation用于在收到通知的时候点击远程通知处理push(当然不在这里创建navigation也是可以push的),这种结构下只需要隐藏appdelegate.swift里面的navigation即可,代码如下

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        self.window = UIWindow(frame:UIScreen.main.bounds)        
        self.window?.makeKeyAndVisible()

        let main = MianTabbarController()
        let nvc = UINavigationController(rootViewController:main)
        nvc.navigationBar.isHidden = true
        
        self.window?.rootViewController = nvc
       
        return true
    }

效果如图1.1

三.结构为Root -> TabBar -> ViewController,代码如下

appdelegate.swift

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        self.window = UIWindow(frame:UIScreen.main.bounds)        
        self.window?.makeKeyAndVisible()
   
        let main = MianTabbarController()      
        self.window?.rootViewController = main
                
        return true
    }

MianTabbarController.swift

override func viewDidLoad() {
        super.viewDidLoad()

        // 创建ViewControllers
        self.creatViewController()
    }
    
    
    func creatViewController(){
        
        let home = HomeViewController()

        let setting = SeetingViewController()

        let tabArray = [home,setting]
        self.viewControllers = tabArray
        //底部工具栏背景颜色
        self.tabBar.barTintColor=UIColor.gray;
        //.设置底部工具栏文字颜色(默认状态和选中状态)(选中状态为蓝色)
        UITabBarItem.appearance().setTitleTextAttributes(NSDictionary(object:UIColor.white, forKey:NSForegroundColorAttributeName as NSCopying) as? [String : AnyObject], for:UIControlState.normal);
        UITabBarItem.appearance().setTitleTextAttributes(NSDictionary(object:UIColor.blue, forKey:NSForegroundColorAttributeName as NSCopying) as? [String : AnyObject], for:UIControlState.selected)
    }

效果如图1.2


图1.2

四 导航栏的一些基本设置

1. 设置导航栏的背景颜色 如图1.3

1.3 设置导航栏背景颜色
//设置导航栏背景颜色
        let bgColor = UIColor(red: 100/255, green:200/225, blue: 60/255, alpha: 1)
        self.navigationController?.navigationBar.barTintColor = bgColor

2. 设置导航栏文字的颜色为白色, 如图1.3 ,代码如下

// 设置导航栏文字的颜色为白色
        self.navigationController?.navigationBar.titleTextAttributes =
            [NSForegroundColorAttributeName: UIColor.white]

3. 设置导航栏返回按钮的颜色,如图1.4,代码如下

//修改导航栏按钮颜色
        self.navigationController?.navigationBar.tintColor = UIColor.white
1.4 设置导航栏返回按钮的颜色

4. 设置导航栏背景图片,代码如下

// 设置导航栏的背景图片
        self.navigationController?.navigationBar
            .setBackgroundImage(UIImage(named: "bg5"), for: .default)

5. 设置tabbar 的背景颜色,如图1.5,代码如下

//底部工具栏背景颜色
        self.tabBar.barTintColor=UIColor.gray;
1.5 设置tabbar的背景颜色,灰色

5. 设置tabbar文字的颜色,如图1.5 ,代码如下

//.设置底部工具栏文字颜色(默认状态和选中状态)(选中状态为蓝色)
        UITabBarItem.appearance().setTitleTextAttributes(NSDictionary(object:UIColor.white, forKey:NSForegroundColorAttributeName as NSCopying) as? [String : AnyObject], for:UIControlState.normal);
        UITabBarItem.appearance().setTitleTextAttributes(NSDictionary(object:UIColor.blue, forKey:NSForegroundColorAttributeName as NSCopying) as? [String : AnyObject], for:UIControlState.selected)

框架结构以及tabbar和navigationbar就设置到这里,下面创建按钮push到下一页面,代码如下

// 创建push按钮
    func creatPushButton(){
        
        let pushBtn:UIButton = UIButton(frame:CGRect(x:0    ,   y:66   ,width:150    ,height:100    ))
        pushBtn.backgroundColor = UIColor.white
        pushBtn.setTitle("点击push下一页", for: UIControlState.normal)
        pushBtn.setTitleColor(UIColor.black, for: UIControlState.normal)
        self.view .addSubview(pushBtn)
        pushBtn.addTarget(self, action:#selector(pushMine), for: UIControlEvents.touchUpInside)
        
    }

func pushMine() {
        let mine = MineViewController()
        self.navigationController?.pushViewController(mine, animated: true)
        
//present方式
   // let mine = MineViewController()
   //self.present(mine, animated: true, completion: nil)
    }

相关文章

网友评论

      本文标题:swift 设置tabbar和导航控制器(navigationC

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