iOS:CYLTabBarController的具体使用实例:实现新浪微博的主流框架

简介:

使用CocoaPods或者手动集成将CYLTabBarController这个第三方框架导入项目后,截图如下:

 

在AppDelegate.m类中实现的代码如下:

复制代码
//  AppDelegate.m
//  CYLTabBarController
//
//  Created by mac on 16/1/28.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "AppDelegate.h"

#import <CYLTabBarController.h>
#import <CYLTabBar.h>

#import "HomeViewController.h"
#import "MessageViewController.h"
#import "ComposeViewController.h"
#import "DiscoverViewController.h"
#import "MineViewController.h"



@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    //创建CYLTabBarController的对象
    CYLTabBarController *CYLtabVC = [[CYLTabBarController alloc]init];
    
    //设置CYLTabBarController对象的标签栏属性按钮
    CYLtabVC.tabBarItemsAttributes = [self createTabBarItemsAttributes];
    
    //设置CYLTabBarController对象的标签栏子控制器数组
    CYLtabVC.viewControllers = [self createTabBarViewControllers];
    
    //设置tabbar按钮的文字颜色
    [self customizeInterface];
    
    //设置添加按钮的事件,模态出发布控制器
    if (CYLExternPushlishButton) {
        
        [CYLExternPushlishButton addTarget:self action:@selector(composeButtonClcked:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    //设置CYLTabBarController的对象的根控制器
    [self.window setRootViewController:CYLtabVC];

    return YES;
}

#pragma mark - 模态出发布控制器
-(void)composeButtonClcked:(id)sender{
    
    ComposeViewController *composeVC = [[ComposeViewController alloc]init];
    [self.window.rootViewController presentViewController:composeVC animated:YES completion:nil];
}


#pragma mark - 创建标签栏子控制器数组
-(NSArray *)createTabBarViewControllers{
    
    //主页
    HomeViewController *homeVC = [[HomeViewController alloc] init];
    UINavigationController *homeNaV = [[UINavigationController alloc]initWithRootViewController:homeVC];
    
    //消息
    MessageViewController *messageVC = [[MessageViewController alloc] init];
    UINavigationController *messageNaV = [[UINavigationController alloc]initWithRootViewController:messageVC];
    
    //发现
    DiscoverViewController *foundVC = [[DiscoverViewController alloc]init];
    UINavigationController *foundNaV = [[UINavigationController alloc]initWithRootViewController:foundVC];
    
    //我的
    MineViewController *mineVC = [[MineViewController alloc]init];
    UINavigationController *mineNaV = [[UINavigationController alloc]initWithRootViewController:mineVC];
    
    return @[homeNaV,messageNaV,foundNaV,mineNaV];
}

#pragma mark - 创建标签栏按钮item数组
-(NSArray *)createTabBarItemsAttributes{
    
    NSDictionary *dict1 = @{
                            CYLTabBarItemTitle : @"首页",
                            CYLTabBarItemImage : @"tabbar_home",
                            CYLTabBarItemSelectedImage : @"tabbar_home_selected",
                            };
    NSDictionary *dict2 = @{
                            CYLTabBarItemTitle : @"消息",
                            CYLTabBarItemImage : @"tabbar_message_center",
                            CYLTabBarItemSelectedImage : @"tabbar_message_center_selected",
                            };
    NSDictionary *dict3 = @{
                            CYLTabBarItemTitle : @"发现",
                            CYLTabBarItemImage : @"tabbar_discover",
                            CYLTabBarItemSelectedImage : @"tabbar_discover_selected",
                            };
    NSDictionary *dict4 = @{
                            CYLTabBarItemTitle : @"我的",
                            CYLTabBarItemImage : @"tabbar_profile",
                            CYLTabBarItemSelectedImage : @"tabbar_profile_selected",
                            };
    return @[ dict1, dict2, dict3, dict4];
}

#pragma mark - 设置tabbar按钮的文字颜色
- (void)customizeInterface {
    
    // 普通状态下的文字属性
    NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
    normalAttrs[NSForegroundColorAttributeName] = [UIColor grayColor];
    
    // 选中状态下的文字属性
    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
    selectedAttrs[NSForegroundColorAttributeName] = [UIColor orangeColor];
    
    // 设置文字属性
    UITabBarItem *tabBar = [UITabBarItem appearance];
    [tabBar setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
    [tabBar setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
    
    // 设置背景图片
    UITabBar *tabBarAppearance = [UITabBar appearance];
    [tabBarAppearance setBackgroundImage:[UIImage imageNamed:@"tabbar_background"]];
}
@end
复制代码

 

在需要添加的按钮类ComposeButton类中,实现的代码如下:

ComposeButton.h文件:

复制代码
//  ComposeButton.h
//  CYLTabBarController
//
//  Created by mac on 16/1/28.
//  Copyright © 2016年 mac. All rights reserved.
//

#import <CYLPlusButton.h>

@interface ComposeButton : CYLPlusButton<CYLPlusButtonSubclassing>

@end
复制代码

ComposeButton.h文件:

复制代码
//  ComposeButton.m
//  CYLTabBarController
//
//  Created by mac on 16/1/28.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ComposeButton.h"

@implementation ComposeButton

+(void)load{
    [super registerSubclass];
}

+(instancetype)plusButton{
    
    ComposeButton *composeButton = [[ComposeButton alloc]initWithFrame:CGRectMake(0, 0, 44, 44)];
    
    //设置背景
    [composeButton setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
    [composeButton setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
    
    //设置按钮
    [composeButton setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
    [composeButton setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateSelected];
    
    return composeButton;
}


//返回插入位置,因为是奇数,所以不用指定安放的位置,它会自动调整位置
//+ (NSUInteger)indexOfPlusButtonInTabBar{
//    
//    return 2;
//}

@end
复制代码

 

在发布控制器中实现的是代码:ComposeViewController.m

复制代码
//  ComposeViewController.m
//  CYLTabBarController
//
//  Created by mac on 16/1/28.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ComposeViewController.h"

@interface ComposeViewController ()
@property (strong,nonatomic)UIToolbar *topToolBar;
@end

#define SCREEN_WIDTH  [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

@implementation ComposeViewController


/**
 *  设置工具栏
*/
-(void)viewWillAppear:(BOOL)animated{
    
    [[UIToolbar appearance]setTintColor:[UIColor brownColor]];
    
    self.topToolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftItemBack:)];
    
    UIBarButtonItem *flexItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    
    UIBarButtonItem *midItem = [[UIBarButtonItem alloc]initWithTitle:@"发布新微薄" style:UIBarButtonItemStylePlain target:nil action:nil];
    [midItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]} forState:UIControlStateNormal];
    [midItem setEnabled:NO];
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithTitle:@"发送" style:UIBarButtonItemStylePlain target:self action:@selector(rightItemSend:)];
    
    self.topToolBar.items = @[leftItem,flexItem,midItem,flexItem,rightItem];
    
    [self.view addSubview:self.topToolBar];
}

- (void)viewDidLoad {
    [super viewDidLoad];
 
    self.title = @"发布";
    self.view.backgroundColor = [UIColor lightGrayColor];
}

#pragma mark - 返回按钮事件,关闭模态的发布控制器
-(void)leftItemBack:(UIBarButtonItem *)sender{
    
    [self.view.window.rootViewController dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - 发布按钮事件,发布新的微博
-(void)rightItemSend:(UIBarButtonItem *)sender{
    
    NSLog(@"%s",__func__);
}

@end
复制代码

 

 

演示截图如下:                                         点击发布按钮时:

 

 

程序猿神奇的手,每时每刻,这双手都在改变着世界的交互方式!
本文转自当天真遇到现实博客园博客,原文链接:http://www.cnblogs.com/XYQ-208910/p/5166778.html ,如需转载请自行联系原作者

相关文章
|
10月前
|
搜索推荐 数据管理 定位技术
iOS应用开发中有多种主流框架
iOS应用开发中有多种主流框架
607 60
|
9月前
|
iOS开发 开发者 MacOS
深入探索iOS开发中的SwiftUI框架
【10月更文挑战第21天】 本文将带领读者深入了解Apple最新推出的SwiftUI框架,这一革命性的用户界面构建工具为iOS开发者提供了一种声明式、高效且直观的方式来创建复杂的用户界面。通过分析SwiftUI的核心概念、主要特性以及在实际项目中的应用示例,我们将展示如何利用SwiftUI简化UI代码,提高开发效率,并保持应用程序的高性能和响应性。无论你是iOS开发的新手还是有经验的开发者,本文都将为你提供宝贵的见解和实用的指导。
275 66
|
开发框架 前端开发 Android开发
安卓与iOS开发中的跨平台框架解析
在移动应用开发的广阔舞台上,安卓和iOS一直是两大主角。随着技术的进步,开发者们渴望能有一种方式,让他们的应用能同时在这两大平台上运行,而不必为每一个平台单独编写代码。这就是跨平台框架诞生的背景。本文将探讨几种流行的跨平台框架,包括它们的优势、局限性,以及如何根据项目需求选择合适的框架。我们将从技术的深度和广度两个维度,对这些框架进行比较分析,旨在为开发者提供一个清晰的指南,帮助他们在安卓和iOS的开发旅程中,做出明智的选择。
|
开发工具 Swift iOS开发
探索iOS开发中的SwiftUI框架
在数字时代的浪潮中,iOS应用开发的舞台日益扩展,其中SwiftUI作为苹果推出的新型用户界面框架,正逐渐改变着开发者构建应用的方式。本文将深入介绍SwiftUI的核心概念和实际应用,探讨其如何简化代码、提升效率并推动设计创新,同时也会触及SwiftUI在当前技术生态中所面临的挑战与未来的发展潜力。
|
物联网 区块链 vr&ar
未来已来:探索区块链、物联网与虚拟现实技术的融合与应用安卓与iOS开发中的跨平台框架选择
【8月更文挑战第30天】在科技的巨轮下,新技术不断涌现,引领着社会进步。本文将聚焦于当前最前沿的技术——区块链、物联网和虚拟现实,探讨它们各自的发展趋势及其在未来可能的应用场景。我们将从这些技术的基本定义出发,逐步深入到它们的相互作用和集成应用,最后展望它们如何共同塑造一个全新的数字生态系统。
|
10月前
|
iOS开发 开发者
探索iOS开发中的SwiftUI框架
【10月更文挑战第39天】在苹果的生态系统中,SwiftUI框架以其声明式语法和易用性成为开发者的新宠。本文将深入SwiftUI的核心概念,通过实际案例展示如何利用这一框架快速构建用户界面,并探讨其对iOS应用开发流程的影响。
132 1
|
11月前
|
移动开发 网络协议 小程序
基于开源IM即时通讯框架MobileIMSDK:RainbowChat-iOS端v9.1版已发布
RainbowChat是一套基于开源IM聊天框架 MobileIMSDK 的产品级移动端IM系统。RainbowChat源于真实运营的产品,解决了大量的屏幕适配、细节优化、机器兼容问题
234 5
|
机器学习/深度学习 搜索推荐 数据处理
探索iOS应用开发的新趋势:SwiftUI和Combine框架
【8月更文挑战第6天】随着Apple不断推动其操作系统的进化,iOS开发领域也迎来了新的变革。本文将深入探讨SwiftUI和Combine框架如何革新iOS应用开发流程,提升开发者的工作效率,并改善最终用户的体验。我们将从这两个框架的基本概念出发,分析它们的核心优势,并预测它们将如何塑造iOS开发的未来。
|
11月前
|
Swift iOS开发 开发者
探索iOS开发中的SwiftUI框架
【10月更文挑战第21天】在苹果生态系统中,SwiftUI的引入无疑为iOS应用开发带来了革命性的变化。本文将通过深入浅出的方式,带领读者了解SwiftUI的基本概念、核心优势以及如何在实际项目中运用这一框架。我们将从一个简单的例子开始,逐步深入到更复杂的应用场景,让初学者能够快速上手,同时也为有经验的开发者提供一些深度使用的技巧和策略。
158 1
|
10月前
|
开发框架 Dart Android开发
安卓与iOS的跨平台开发:Flutter框架深度解析
在移动应用开发的海洋中,Flutter作为一艘灵活的帆船,正引领着开发者们驶向跨平台开发的新纪元。本文将揭开Flutter神秘的面纱,从其架构到核心特性,再到实际应用案例,我们将一同探索这个由谷歌打造的开源UI工具包如何让安卓与iOS应用开发变得更加高效而统一。你将看到,借助Flutter,打造精美、高性能的应用不再是难题,而是变成了一场创造性的旅程。