iOS开发中 UITabBarController--标签控制器的使用

2024-06-04 06:38

本文主要是介绍iOS开发中 UITabBarController--标签控制器的使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

这个比较详细 http://blog.csdn.net/zfx5130/article/details/43311617

http://www.linuxidc.com/Linux/2015-12/126156.htm

一、引言

与导航控制器相类似,标签控制器也是用于管理视图控制器的一个UI控件,在其内部封装了一个标签栏,与导航不同的是,导航的管理方式是纵向的,采用push与pop切换控制器,标签的管理是横向的,通过标签的切换来改变控制器,一般我们习惯将tabBar作为应用程序的根视图控制器,在其中添加导航,导航中在对ViewController进行管理。
 
二、创建一个标签控制器

通过如下的步骤,我们可以很简便的创建一个TabBarController:

UITabBarController * tabBar= [[UITabBarController alloc]init];
    NSMutableArray * controllerArray = [[NSMutableArray alloc]init];
    for (int i=0; i<4; i++) {
        UIViewController * con = [[UIViewController alloc]init];
        [con loadViewIfNeeded];
        con.view.backgroundColor = [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1];
        con.tabBarItem.image = [UIImage imageNamed:@"btn_publish_face_a.png"];
        con.tabBarItem.title=[NSString stringWithFormat:@"%d",i+1];
        con.title = [NSString stringWithFormat:@"%d",i+1];
        [controllerArray addObject:con];
    }
    tabBar.viewControllers = controllerArray;
    [self presentViewController:tabBar animated:YES completion:nil];

通过点击下面的标签按钮,可以很方便的切换控制器。如果我们的控制器数超过4个,系统会被我们创建一个more的导航,并且可以通过系统自带的编辑来调整控制器的顺序,如下:

三、UITabBarController的属性和方法

//管理的viewController数组
@property(nullable, nonatomic,copy) NSArray<__kindof UIViewController *> *viewControllers;
- (void)setViewControllers:(NSArray<__kindof UIViewController *> * __nullable)viewControllers animated:(BOOL)animated;
//选中的ViewControlle
@property(nullable, nonatomic, assign) __kindof UIViewController *selectedViewController;
//通过编号设置选中ViewController
@property(nonatomic) NSUInteger selectedIndex;
//当viewController大于4个时,获取"更多"标签的导航控制器
@property(nonatomic, readonly) UINavigationController *moreNavigationController; 
//这个属性设置的是可以进行自定义排列顺序的视图控制器,如上面第二张图中的,默认是全部
@property(nullable, nonatomic, copy) NSArray<__kindof UIViewController *> *customizableViewControllers;
//标签控制器中分装的标签栏
@property(nonatomic,readonly) UITabBar *tabBar NS_AVAILABLE_IOS(3_0);
//代理
@property(nullable, nonatomic,weak) id<UITabBarControllerDelegate> delegate;
 
 
四、关于标签栏TabBar

通过自定义标签栏的一些属性,使我们可以更加灵活的使用tabBar。

1、UITabBar属性和方法

设置标签:

@property(nullable,nonatomic,copy) NSArray<UITabBarItem *> *items;  
//设置选中的标签    
@property(nullable,nonatomic,assign) UITabBarItem *selectedItem; 
- (void)setItems:(nullable NSArray<UITabBarItem *> *)items animated:(BOOL)animated;

设置自定义标签顺序:

//调用这个方法会弹出一个类似上面第二张截图的控制器,我们可以交换标签的布局顺序
- (void)beginCustomizingItems:(NSArray<UITabBarItem *> *)items;  
//完成标签布局
- (BOOL)endCustomizingAnimated:(BOOL)animated;  
//是否正在自定义标签布局
- (BOOL)isCustomizing;

设置tabBar颜色相关:

//设置渲染颜色,会影响选中字体和图案的渲染
@property(null_resettable, nonatomic,strong) UIColor *tintColor;
//设置导航栏的颜色
@property(nullable, nonatomic,strong) UIColor *barTintColor;

设置背景图案:

//设置导航栏背景图案
@property(nullable, nonatomic,strong) UIImage *backgroundImage;
//设置选中一个标签时,标签背后的选中提示图案 这个会出现在设置的item图案的后面
@property(nullable, nonatomic,strong) UIImage *selectionIndicatorImage;
//设置阴影的背景图案
@property(nullable, nonatomic,strong) UIImage *shadowImage

TabBar中标签的宏观属性:

//设置标签item的位置模式
@property(nonatomic) UITabBarItemPositioning itemPositioning;
//枚举如下
typedef NS_ENUM(NSInteger, UITabBarItemPositioning) {
    UITabBarItemPositioningAutomatic,//自动
    UITabBarItemPositioningFill,//充满
    UITabBarItemPositioningCentered,//中心
} NS_ENUM_AVAILABLE_IOS(7_0);
//设置item宽度
@property(nonatomic) CGFloat itemWidth;
//设置item间距
@property(nonatomic) CGFloat itemSpacing;

与导航栏类似,也可以设置tabBar的风格和透明效果:

//风格 分黑白两种
@property(nonatomic) UIBarStyle barStyle;
//是否透明效果
@property(nonatomic,getter=isTranslucent) BOOL translucent;
 
 2、UITabBarDelegate

//选中标签时调用
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item;
//将要开始编辑标签时
- (void)tabBar:(UITabBar *)tabBar willBeginCustomizingItems:(NSArray<UITabBarItem *> *)items;          //已经开始编辑标签时        
- (void)tabBar:(UITabBar *)tabBar didBeginCustomizingItems:(NSArray<UITabBarItem *> *)items;          
//将要进入编辑状态时
- (void)tabBar:(UITabBar *)tabBar willEndCustomizingItems:(NSArray<UITabBarItem *> *)items changed:(BOOL)changed; 
//已经进入编辑状态时
- (void)tabBar:(UITabBar *)tabBar didEndCustomizingItems:(NSArray<UITabBarItem *> *)items changed:(BOOL)changed;
 
五、再看UITabBarItem

和NavigationItem类似,标签栏上的item也可以自定义,一些方法如下。

初始化方法:

//通过标题和图案进行创建
- (instancetype)initWithTitle:(nullable NSString *)title image:(nullable UIImage *)image tag:(NSInteger)tag;
- (instancetype)initWithTitle:(nullable NSString *)title image:(nullable UIImage *)image selectedImage:(nullable UIImage *)selectedImage;
//创建系统类型的
- (instancetype)initWithTabBarSystemItem:(UITabBarSystemItem)systemItem tag:(NSInteger)tag;
 

UITabBarSystemItem的枚举如下:

typedef NS_ENUM(NSInteger, UITabBarSystemItem) {
    UITabBarSystemItemMore,//更多图标
    UITabBarSystemItemFavorites,//最爱图标
    UITabBarSystemItemFeatured,//特征图标
    UITabBarSystemItemTopRated,//高级图标
    UITabBarSystemItemRecents,//最近图标
    UITabBarSystemItemContacts,//联系人图标
    UITabBarSystemItemHistory,//历史图标
    UITabBarSystemItemBookmarks,//图书图标
    UITabBarSystemItemSearch,//查找图标
    UITabBarSystemItemDownloads,//下载图标
    UITabBarSystemItemMostRecent,//记录图标
    UITabBarSystemItemMostViewed,//全部查看图标
};

UITabBarItem常用属性:

//设置选中图案
@property(nullable, nonatomic,strong) UIImage *selectedImage;

下面这个属性可以设置item的头标文字:

con.tabBarItem.badgeValue = @"1";


 
//设置标题的位置偏移
@property (nonatomic, readwrite, assign) UIOffset titlePositionAdjustment;

由于UITabBarItem是继承于UIBarItem,还有下面这个属性可以设置使用:

//标题
@property(nullable, nonatomic,copy)            NSString    *title;  
//图案    
@property(nullable, nonatomic,strong)          UIImage    *image;  
//横屏时的图案      
@property(nullable, nonatomic,strong)          UIImage    *landscapeImagePhone;
//图案位置偏移
@property(nonatomic)                  UIEdgeInsets imageInsets; 
//横屏时的图案位置偏移
@property(nonatomic)                  UIEdgeInsets landscapeImagePhoneInsets ;
//设置和获取标题的字体属性
- (void)setTitleTextAttributes:(nullable NSDictionary<NSString *,id> *)attributes forState:(UIControlState)state;
- (nullable NSDictionary<NSString *,id> *)titleTextAttributesForState:(UIControlState)state;

本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-12/126156.htm


这篇关于iOS开发中 UITabBarController--标签控制器的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1029341

相关文章

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

C#中Guid类使用小结

《C#中Guid类使用小结》本文主要介绍了C#中Guid类用于生成和操作128位的唯一标识符,用于数据库主键及分布式系统,支持通过NewGuid、Parse等方法生成,感兴趣的可以了解一下... 目录前言一、什么是 Guid二、生成 Guid1. 使用 Guid.NewGuid() 方法2. 从字符串创建

Python使用python-can实现合并BLF文件

《Python使用python-can实现合并BLF文件》python-can库是Python生态中专注于CAN总线通信与数据处理的强大工具,本文将使用python-can为BLF文件合并提供高效灵活... 目录一、python-can 库:CAN 数据处理的利器二、BLF 文件合并核心代码解析1. 基础合

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文

Spring IoC 容器的使用详解(最新整理)

《SpringIoC容器的使用详解(最新整理)》文章介绍了Spring框架中的应用分层思想与IoC容器原理,通过分层解耦业务逻辑、数据访问等模块,IoC容器利用@Component注解管理Bean... 目录1. 应用分层2. IoC 的介绍3. IoC 容器的使用3.1. bean 的存储3.2. 方法注

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客

Linux中压缩、网络传输与系统监控工具的使用完整指南

《Linux中压缩、网络传输与系统监控工具的使用完整指南》在Linux系统管理中,压缩与传输工具是数据备份和远程协作的桥梁,而系统监控工具则是保障服务器稳定运行的眼睛,下面小编就来和大家详细介绍一下它... 目录引言一、压缩与解压:数据存储与传输的优化核心1. zip/unzip:通用压缩格式的便捷操作2.

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互

Python中注释使用方法举例详解

《Python中注释使用方法举例详解》在Python编程语言中注释是必不可少的一部分,它有助于提高代码的可读性和维护性,:本文主要介绍Python中注释使用方法的相关资料,需要的朋友可以参考下... 目录一、前言二、什么是注释?示例:三、单行注释语法:以 China编程# 开头,后面的内容为注释内容示例:示例:四