ios tabbar图片素材-IOS自定义TabBar(OC和Swift)
ios开发中tabbar上的图片设置图片渲染模式无效怎么办

写一个控制器继承自UITabBarController,
在该控制器的ViewDidLoad方法中加入如下代码
// tabBarItem的图片名称
NSArray *images = @[@"tabBarItem_allTest_",@"tabBarItem_allTest_",@"tabBarItem_oneStation_",@"tabBarItem_oneStation_",@"tabBarItem_testResult_",@"tabBarItem_testResult_",@"tabBarItem_userCenter_",@"tabBarItem_userCenter_"];
for(int i= 0;i< 8;){
// 分别给tabBarItem设置图片,图片模式为源图片
UITabBarItem *item = [ objectAtIndex:i/2];
UIImage *normalImage = [UIImage imageNamed:images[i+1]];
normalImage = [normalImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *selectImage = [UIImage imageNamed:images[i]];
selectImage = [selectImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[item setFinishedSelectedImage:normalImage
withFinishedUnselectedImage:selectImage];
i+=2;
}
// 设置tabbar的选中和未选中的字体颜色
[[UITabBarItem appearance] setTitleTextAttributes:@{
NSForegroundColorAttributeName:[UIColor lightGrayColor]
} forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:@{
NSForegroundColorAttributeName:[UIColor lightGrayColor]
} forState:UIControlStateSelected];
IOS开发以及Tab Bar使用讲解

为了更好理解使用用tabbar和切换视图,我们创建一个Empty Application。
1、
打开Xcode ,新建项目
2、
创建View Controller
在项目上按花键+N创建新文件,创建 Objective-C class 文件,按Next按钮,subClass 选UIViewController 。勾选上xib选项
以同样方式创建另外三个ViewController ,RedViewController ,GreyViewController,YellowViewController。四个View准备好了。那么Tabbar呢?
3、
创建文件,选择创建Empty文件
这时候你发现创建的xib文件是空白的,不用慌,去右下角控件栏中把TabBar Controller拖过来就Ok了。
4、
关联 ,tabbarAppDelegate这两个文件
在上图中选择File’s Owner,打开Identity Inspector,在Class一栏选择tabbarAppDelegate
这样,我们就可以创建 文件指向tabbarAppDelegate 文件的Outlet映射了。
5、
在Xcode中的工具栏的View菜单找到 打开Assistant Editor,使tabbarAppDelegate.h和 同时打开。
在xib文件上按住control键,往tabbarAppDelegate.h,创建Outlet.
弹出窗口输入 rootController,点connect。
6、
添加代码
打开tabbarAppDelegate.m,在didFinishLaunchingWithOptions方法中添加代码:
1.- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
2. = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
3. // Override point for customization after application launch.
4. [[NSBundle mainBundle] loadNibNamed:@"TabBarController" owner:self options:nil];
5. [ addSubview:];
6. = [UIColor whiteColor];
7. [ makeKeyAndVisible];
8. return YES;
7、
往上添加Tab Bar Item,
把控件栏上的Tab Bar Item控件往上拖拽即可,一个放4个。
8
、关联Tab Bar Item和***ViewController。
选择其中一个Tab Bar Item,在右上角打开Identity Inspector,在Class中选择BlueViewController:
然后,打开Attribute,在NIB Name选择BlueViewController:
其他3个tab item重复类似的操作,选中对应的ViewController,这样在切换Tab标签时,就可以切换到对应的页面。
9、
设置tab item的属性
选中其中一个tab item ,会在右上角的属性栏里看到如下信息
Badge是红色圈圈里面有数字 ,表示有多少条信息的属性
Identifier 是tab item的样式,选custom是自定义,下面的是系统的样式。我选了其中四种。
bar ITem 的title image在custom的样式下能设置。
10
、剩下的3个Tab Item也做类似的设置即可。
现在基本完工,运行看看结果如何。好吧,其实和第一第二个图是一样的`,这里就不放了。
11
、在viewDidLoad方法加Log观察切换View
可以加写日志看看对应的View是什么时候运行的。第一个运行的View是BlueViewController,点击其他的tab项时,加载其他的view,加载一次之后下次点击不再调用viewDidLoad。
1.- (void)viewDidLoad
2. [super viewDidLoad];
3. NSLog(@"BlueViewController");
4. // Do any additional setup after loading the view from its nib.
IOS自定义TabBar(OC和Swift)

OC Demo : 传送门
SwfitDemo : 传送门
效果图
-
果盘绘画图片大全简单创作方法快速制作技巧2024-01-02 16:57:54
-
小年插画图片创作方法快速制作技巧2024-01-02 16:57:20
-
小年绘画作品制作技巧2024-01-02 16:55:45
-
小年插画图片创作方法快速制作技巧2024-01-02 16:55:02
-
立春插画图片如何制作2024-01-02 16:54:19
-
立春插画图片创作方法快速制作技巧2024-01-02 16:53:32






扫描二维码添加客服微信
扫描二维码关注