swift-UINavigationController纯代码自定义导航控制器及底部工具栏的使用

   2023-02-09 学习力0
核心提示:step1:自定义一个类  NTViewController,该类继承UITabBarController:////NTViewController.swift//Housekeeper////Created by 卢洋 on 15/10/20.//Copyright © 2015年 奈文摩尔. All rights reserved.//import Foundationimport UIKitclass NTViewContro

step1:自定义一个类  NTViewController,该类继承UITabBarController:

//
//  NTViewController.swift
//  Housekeeper
//
//  Created by 卢洋 on 15/10/20.
//  Copyright © 2015年 奈文摩尔. All rights reserved.
//

import Foundation
import UIKit
class NTViewController:UITabBarController{
   
    var items=[];
    //页面初始化
    override func viewDidLoad() {
        super.viewDidLoad();
        //1.创建首页导航控制器
        let vwIndex=index();
        let navIndex=UINavigationController(rootViewController: vwIndex);
        navIndex.title="首页";
     //设置工具栏默认显示的图片 navIndex.tabBarItem.image
=UIImage(named: "home")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);
     //设置工具栏选中后的图片 navIndex.tabBarItem.selectedImage
=UIImage(named: "homes.png")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal); //2.创建活动信息导航控制器 let vwActivityInfo=activityInfo(); let navActivityInfo=UINavigationController(rootViewController: vwActivityInfo); navActivityInfo.title="活动信息"; navActivityInfo.tabBarItem.image=UIImage(named: "Activity-information.png")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal); navActivityInfo.tabBarItem.selectedImage=UIImage(named: "Activity-informations.png")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal); //3.创建车辆展示导航控制器 let vwCarDisplay=carDisplay(); let navCarDisplay=UINavigationController(rootViewController: vwCarDisplay); navCarDisplay.title="车辆展示"; navCarDisplay.tabBarItem.image=UIImage(named: "Vehicle-display.png")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal); navCarDisplay.tabBarItem.selectedImage=UIImage(named: "Vehicle-displays.png")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal); //4.创建个人中心导航控制器 let vwPersonalCenter=personalCenter(); let navPersonalCenter=UINavigationController(rootViewController: vwPersonalCenter); navPersonalCenter.title="个人中心"; navPersonalCenter.tabBarItem.image=UIImage(named: "Personal-Center.png")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal); navPersonalCenter.tabBarItem.selectedImage=UIImage(named: "Personal-Centers.png")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal); //5.添加到工具栏 items=[navIndex,navActivityInfo,navCarDisplay,navPersonalCenter]; self.viewControllers=items as? [UIViewController]; self.navigationController?.navigationBar.tintColor=UIColor.whiteColor(); //6.自定义工具栏 self.tabBar.backgroundColor=UIColor.clearColor(); //底部工具栏背景颜色 self.tabBar.barTintColor=UIColor.appMainColor(); //7.设置底部工具栏文字颜色(默认状态和选中状态) UITabBarItem.appearance().setTitleTextAttributes(NSDictionary(object:UIColor.whiteColor(), forKey:NSForegroundColorAttributeName) as? [String : AnyObject], forState:UIControlState.Normal); UITabBarItem.appearance().setTitleTextAttributes(NSDictionary(object:UIColor.blueWithTabbar(), forKey:NSForegroundColorAttributeName) as? [String : AnyObject], forState:UIControlState.Selected) } }

step2:打开 AppDelegate.swift

//
//  AppDelegate.swift
//  Housekeeper
//
//  Created by 卢洋 on 15/10/14.
//  Copyright © 2015年 奈文摩尔. All rights reserved.
//

import Foundation
import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // 应用程序启动后
        //1.声明一个空视图
        self.window = UIWindow(frame: UIScreen.mainScreen().bounds);
        self.window!.backgroundColor=UIColor.whiteColor();
        
        //2.1导航背景颜色
        UINavigationBar.appearance().barTintColor=UIColor.appMainColor();
        
        UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(CGFloat(NSInteger.min),CGFloat(NSInteger.min)), forBarMetrics:UIBarMetrics.Default);
        //2.2导航标题文字颜色
        UINavigationBar.appearance().titleTextAttributes=NSDictionary(object:UIColor.whiteColor(), forKey:NSForegroundColorAttributeName) as? [String : AnyObject];
        
     //楼主的状态栏颜色改变了,设置为了白色,如果有需要,打开 info.plist  增加Key(View controller-based status bar appearance,Value为 no
//2.3将状态栏变为白色 UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent; //2.4设置返回按钮颜色 UINavigationBar.appearance().tintColor=UIColor.whiteColor(); //3.指定根视图 let rootView=NTViewController(); self.window!.rootViewController=rootView; self.window!.makeKeyAndVisible(); return true } func applicationWillResignActive(application: UIApplication) { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. } func applicationDidEnterBackground(application: UIApplication) { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } func applicationWillEnterForeground(application: UIApplication) { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. } func applicationDidBecomeActive(application: UIApplication) { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } func applicationWillTerminate(application: UIApplication) { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } }

效果图如下:中间显示的内容是我的项目,你们忽略不计就好了,哈哈。

swift-UINavigationController纯代码自定义导航控制器及底部工具栏的使用

 
反对 0举报 0 评论 0
 

免责声明:本文仅代表作者个人观点,与乐学笔记(本网)无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
    本网站有部分内容均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,若因作品内容、知识产权、版权和其他问题,请及时提供相关证明等材料并与我们留言联系,本网站将在规定时间内给予删除等相关处理.

  • swift 命令行工具初探
    亲爱的同学们好,今天我们要介绍这么一个东西。相信有过解释型语言(PHP,Ruby,等)使用经验的同学会更加熟悉,就是 Swift 也为我们提供了命令行运行工具,俗称 REPL。好了,我们进入正题,在安装好 Swift 开发环境的机器上,打开命令行,输入 swift 命令,就进
    03-16
  • [Swift]冒泡排序 | Bubble sort
    [Swift]冒泡排序 | Bubble sort
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)➤GitHub地址:https://github.com/strengthen/LeetCode➤原文
    03-08
  • [Swift] 自定义在 SwiftUI 中实现的可搜索的外观
    [Swift] 自定义在 SwiftUI 中实现的可搜索的外
    首先我找遍了,似乎找不到任何信息......(我遇到了许多根本不起作用的事情......)终于在详细的英文文献中找到了,我会保留下来,希望以后有机会。关于 SwiftUI 中的可搜索searchable是iOS15新增的易于实现的搜索字段。关于这种情况有一个参数placement,您
    03-08
  • [Swift] 检测 SwiftUI ScrollView 中的偏移量
    [Swift] 检测 SwiftUI ScrollView 中的偏移量
    首先你可以用ScrollViewReader做一些可以从iOS14使用的事情。但是,我不能做我想做的事情,所以我想我还能做些什么。再次,可重复使用我尝试过了。执行我将首先发布实现的图像。 (Swift Playgrounds 演示)您可以像这样根据滚动获取坐标。让我们看看实现。1.
    03-08
  • Swift3.0 UICollectionView 删除,拖动
    Swift3.0 UICollectionView 删除,拖动
    UICollectionView实现了一下常见的新闻分类.  附有效果图 近期一直在深入学习swift,实现了CollectionView item的头东与删除,用的都是系统的一些函数方法,看起来比较直观. 第一步:class HotViewController: UIViewController,UICollectionViewDelegate,UICo
    02-09
  • swift -懒加载创建view
     // 只有外界访问到headerView的时候才会去执行闭包, 然后将闭包的返回值赋值给headerView    // 注意: 一定要记住闭包后面需要写上(), 代表执行闭包    //懒加载创建UIView    lazy var headerView: UIView = {        let view = UIView()
    02-09
  • Swift--非常好用的适合局部的代码约束
    // 哪个控件的哪个属性等于(大于、小于)另外一个控件的哪个属性乘以多少再加上多少 eg:let widthContraint = NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLa
    02-09
  • iOS打包framework - Swift完整项目打包Framework,嵌入OC项目使用
    iOS打包framework - Swift完整项目打包Framewor
    场景说明:-之前做的App,使用Swift框架语言,混合编程,内含少部分OC代码。-需要App整体功能打包成静态库,完整移植到另一个App使用,该App使用OC。-所以涉及到一个语言互转的处理,以及一些AppDelegate的代码减除变化。 --------------------------------
    02-09
  • Swift -- 官方文档Swift-Guides的学习笔记
    在经历的一段时间的郁闷之后,我发现感情都是虚伪的,只有代码是真实的(呸)因为看了swift语法之后依然不会用swift,然后我非常作死的跑去看官方文档,就是xcode里自带的help》documentation and API reference其中的swift里的guide这里主要总结一下里面每一
    02-09
  • Swift - 进度条(UIProgressView)的用法
     1,创建进度条1234var progressView=UIProgressView(progressViewStyle:UIProgressViewStyle.Default)progressView.center=self.view.centerprogressView.progress=0.5 //默认进度50%self.view.addSubview(progressView); 2,设置进度,同时有动画效果1p
    02-09
点击排行