搞搞UITabBar——iOS从零单排

版权声明:本文为博主Atany原创文章,未经博主允许不得转载。博客地址:http://blog.csdn.net/yang8456211 https://blog.csdn.net/yang8456211/article/details/12980957

学了几个月iOS了还不会UITabBar,心想那不能啊,不能忍啊,虽然负责的模块用不着,但将来总会用到的把,于是乎今天逼自己抽几个小时来看看UITabBar~走起~


让咱先喝口茶、真苦,精神。

有位伟人说过,学习iOS先看SDK。咳咳,感谢我的球迷!

这图我挺喜欢,很直白的说明了TabBarController view由两个部分组成,Tab bar(标签栏)与Custom content(自定义内容)。


心中就"噌”的冒出了几个问题。

1)怎么添加Tab bar?

2)怎么添加Custom content?

扫描二维码关注公众号,回复: 3187460 查看本文章

3)怎么设置标签栏里面的图标与文字?

4)这玩意肯定符合委托的设计模式,那有哪些委托方法?

5)怎么和其他view结合起来,比如说与NavigationController(导航)?


SDK其他的介绍性的东西咱就不理了(我才不会承认我看不懂),喝口茶,按按太阳穴,再在Sample Code中找找demo。

很容易就找到一个Tabster的例子。介绍是:

Showshow to use "customizableViewControllers" property, customizes theappearance of the tab bar.

Ok,得来全不费工夫。


咱喜欢以问题为导向搞代码,所以咱就直捣黄龙!但发现其他功能咱们也顺便记录记录~

1)  怎么添加Tab bar?

例子里面的Tab bar是用代码添加的,方法跟其他控件差不多,几句代码。

//AppDelegate.h
@property (nonatomic, retain) IBOutlet UITabBarController *myTabBarController;

//AppDelegate.m
@synthesize myTabBarController;

//didFinishLaunchingWithOptions:
self.tabBarController = [[[UITabBarController alloc] init] autorelease];

[window addSubview:myTabBarController.view];

定义+合成+addview的模式。但是这个是没有用的,因为没有显示内容。


2)  怎么在里面添加Custom content呢?

可以通过设置TabBar的viewControllers实现。

self.myTabBarController.viewControllers = controllers;
当然这个controllers是个数组哦~把自己需要的controllers实例化后放到一个数组中,赋给tabbar的viewController就OK,把页面添加进去了。


3)怎么设置图标与文字?

可以通过设置viewcontroller.tabBarItem属性来改变标签的显示内容。(默认的话是采用子viewController的title做为文字,没有图像),当自己创建UITabBarItem的时候,我们可以显示的指定显示的图像和对应的文字描述。

UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"Second" image:nil tag:1];//设置文字

//设置图片
[item setFinishedSelectedImage:[UIImage imageNamed:@"second.png"] 
   withFinishedUnselectedImage:[UIImage imageNamed:@"first.png"]];

//给需要用的viewController设置
viewController.tabBarItem = item;

此外UITabBarItem还有一个属性badgeValue,通过设置该属性可以在其右上角显示一个小的角标,通常用于提示用户有新的消息。

item.badgeValue = @"hello";//注意是字符串

TabBar构建起来之后,说说辅助的功能。

  • 修改TabBar以及点击之后的颜色

self.myTabBarController.tabBar.tintColor = [UIColor darkGrayColor];
self.myTabBarController.tabBar.selectedImageTintColor = [UIColor yellowColor];


  • 修改点击more后出现的导航栏颜色

如果添加多个controller的话,超过5个,标签栏就会以More的方式呈现。点击More按钮剩下的回以导航栏的方式出现。

self.myTabBarController.moreNavigationController.navigationBar.tintColor = [UIColor blueColor];

  • 设置被选中的标签项

self.myTabBarController.selectedIndex = [[NSUserDefaults standardUserDefaults] integerForKey:kWhichTabPrefKey];

NSUserDefaults后面那段是读取储存的数据方式,不用管。


  •  编辑标签项(item)

如果添加多个item的话。超过5个,标签栏就会以More的方式呈现,点击More在右上角会出现一个edit按钮,点击之后,可以把自己喜欢的标签换到标签栏。


而下面这句的作用是,设定编辑模式下可选的标签组,如果不在customizeableViewControllers里面的话就不会显示在edit里面咯。

self.myTabBarController.customizableViewControllers = customizeableViewControllers;

4) 有哪些委托方法?

委托方法的话还是要看SDK中的UITabBarControllerDelegate才行。

Managing Tab Bar Selections

       – tabBarController:shouldSelectViewController:

       – tabBarController:didSelectViewController:


Managing Tab Bar Customizations

       – tabBarController:willBeginCustomizingViewControllers:

       – tabBarController:willEndCustomizingViewControllers:changed:

       – tabBarController:didEndCustomizingViewControllers:changed:

出乎意料,方法不多也,想起咱总结tableViewDelegate和tableViewDataSourceDelegate的时候,加起来30多个委托方法了。。


tabBarController:shouldSelectViewController:

tabBarController:didSelectViewController:

这两个一起说,点击一个标签时,should先调用,did后调用。

Should的作用是

You can use thismethod to dynamically decide whether a given tab should be made the active tab.(决定是否激活该标签),通俗来讲,return YES的话就能被点击选中,NO的话就不能。。

Did的作用是:

Tells the delegate that the user selected an item in thetab bar.

In iOS v3.0 and later, the tab bar controller calls thismethod regardless of whether the selected view controller changed. In addition,it is called only in response to user taps in the tab bar and is not calledwhen your code changes the tab bar contents programmatically.

通知委托现在选项被点了,需要注意的是,只有用户点击才会触发,而在代码里面设置selectedIndex是不会触发该方法的。



tabBarController:willBeginCustomizingViewControllers:

       tabBarController:willEndCustomizingViewControllers:changed:

tabBarController:didEndCustomizingViewControllers:changed:

这三个方法看上去就像编辑模式下的。即编辑模式的“即将开始”“即将结束”与“结束”。

5)  怎么与导航栏结合呢?

一般软件肯定有个初始界面(登录界面?),然后才从初始的界面再跳转到标签栏(主界面)。

那Tab bar的生成肯定就不会放在AppDelegate里面咯,自己写个demo测试了一下发现也不难~

在AppDelegate里面设置rootvViewController为navigationController。

然后在需要标签栏的viewController里面的ViewDidLoad添加就tabbar。


UITabBarController *tabBar = [[UITabBarController alloc] init];
    tabBar.delegate = self;
    First *first = [[First alloc] init];
    Second *second = [[Second alloc] init];
    NSArray *viewControllerArray = [NSArray arrayWithObjects:first,second,nil];
    tabBar.viewControllers = viewControllerArray;
    tabBar.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[self.view addSubview:tabBar.view];

OK一气呵成。

面朝大海,春暖花开。


杨光(atany)原创,转载请注明博主与博文链接,未经博主允许,禁止任何商业用途。

博文地址:http://blog.csdn.net/yang8456211/article/details/12980957

博客地址:http://blog.csdn.net/yang8456211

—— by atany

本文遵循“署名-非商业用途-保持一致”创作公用协议

猜你喜欢

转载自blog.csdn.net/yang8456211/article/details/12980957