码迷,mamicode.com
首页 > 其他好文 > 详细

031实现仿iPhone的底部选项卡

时间:2015-06-15 01:41:34      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:

效果如下:

技术分享

 

ViewController.h

1 #import <UIKit/UIKit.h>
2 
3 @interface ViewController : UITabBarController
4 @end

ViewController.m

 1 #import "ViewController.h"
 2 #import "SampleScene.h"
 3 
 4 @interface ViewController ()
 5 - (void)layoutUI;
 6 @end
 7 
 8 @implementation ViewController
 9 
10 - (void)viewDidLoad {
11     [super viewDidLoad];
12     
13     [self layoutUI];
14 }
15 
16 - (void)didReceiveMemoryWarning {
17     [super didReceiveMemoryWarning];
18     // Dispose of any resources that can be recreated.
19 }
20 
21 /**
22  *  加载的UI内容
23  */
24 - (void)layoutUI {
25     self.title = @"UITabBarController";
26     /*
27     id scene1 = [[SampleScene alloc] initWithSystemItem:UITabBarSystemItemMore badge:nil];
28     id scene2 = [[SampleScene alloc] initWithSystemItem:UITabBarSystemItemFavorites badge:nil];
29     id scene3 = [[SampleScene alloc] initWithSystemItem:UITabBarSystemItemFeatured badge:nil];
30     id scene4 = [[SampleScene alloc] initWithSystemItem:UITabBarSystemItemTopRated badge:nil];
31     id scene5 = [[SampleScene alloc] initWithSystemItem:UITabBarSystemItemRecents badge:nil];
32     id scene6 = [[SampleScene alloc] initWithSystemItem:UITabBarSystemItemContacts badge:nil];
33     id scene7 = [[SampleScene alloc] initWithSystemItem:UITabBarSystemItemHistory badge:nil];
34     */
35     id scene8 = [[SampleScene alloc] initWithSystemItem:UITabBarSystemItemBookmarks badge:nil];
36     id scene9 = [[SampleScene alloc] initWithSystemItem:UITabBarSystemItemSearch badge:nil];
37     id scene10 = [[SampleScene alloc] initWithSystemItem:UITabBarSystemItemDownloads badge:@"2"];
38     id scene11 = [[SampleScene alloc] initWithSystemItem:UITabBarSystemItemMostRecent badge:nil];
39     id scene12 = [[SampleScene alloc] initWithSystemItem:UITabBarSystemItemMostViewed badge:nil];
40     
41     id scene13 = [[SampleScene alloc] initWithFileName:@"Smile.png" title:@"微 笑" badge:@"1"];
42     [self setViewControllers:@[scene10, scene11, scene12, scene13, scene8, scene9] animated:NO]; //self必须是继承UITabBarController,而不是UIViewController才可以调用setViewControllers方法;iOS8显示是4个tabBarItem
43     self.customizableViewControllers = @[scene10, scene11, scene12, scene13, scene8, scene9]; //没啥用
44     
45     self.selectedIndex = 3; //默认值是0
46 }
47 
48 @end

SampleScene.h

 1 #import <UIKit/UIKit.h>
 2 
 3 @interface SampleScene : UIViewController
 4 @property (nonatomic, strong) UILabel *lblMessage;
 5 @property (nonatomic, assign) NSUInteger tabBarItemType;
 6 
 7 - (id)initWithSystemItem:(UITabBarSystemItem)item badge:(NSString *)badge;
 8 - (id)initWithFileName:(NSString *)fileName title:(NSString *)title badge:(NSString *)badge;
 9 
10 @end

SampleScene.m

 1 #import "SampleScene.h"
 2 
 3 @interface SampleScene ()
 4 @end
 5 
 6 @implementation SampleScene
 7 
 8 - (void)viewDidLoad {
 9     [super viewDidLoad];
10     _lblMessage = [[UILabel alloc] initWithFrame:self.view.bounds];
11     _lblMessage.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
12     switch (_tabBarItemType) {
13         case 1:
14             _lblMessage.backgroundColor = [UIColor orangeColor];
15             break;
16         case 2:
17             _lblMessage.backgroundColor = [UIColor brownColor];
18             break;
19         case 3:
20             _lblMessage.backgroundColor = [UIColor cyanColor];
21             break;
22         default:
23             _lblMessage.backgroundColor = [UIColor lightGrayColor];
24             break;
25     }
26     
27     [self.view addSubview:_lblMessage];
28     
29     NSLog(@"viewDidLoad");
30 }
31 
32 - (void)didReceiveMemoryWarning {
33     [super didReceiveMemoryWarning];
34     // Dispose of any resources that can be recreated.
35 }
36 
37 /**
38  通过系统图标加载UITabBarItem
39  */
40 - (id)initWithSystemItem:(UITabBarSystemItem)item badge:(NSString *)badge {
41     if (self = [super init]) {
42         self.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:item tag:0];
43         self.tabBarItem.badgeValue = badge;
44         
45         if (item == UITabBarSystemItemMostRecent) {
46             _tabBarItemType = 1;
47         } else if (item == UITabBarSystemItemMostViewed) {
48             _tabBarItemType = 2;
49         }
50     }
51     NSLog(@"initWithSystemItem");
52     return self;
53 }
54 
55 /**
56  通过自定义图标加载UITabBarItem
57  */
58 - (id)initWithFileName:(NSString *)fileName title:(NSString *)title badge:(NSString *)badge {
59     if (self = [super init]) {
60         UIImage *imgIcon = [UIImage imageNamed:fileName];
61         self.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:imgIcon tag:0];
62         self.tabBarItem.badgeValue = badge;
63         
64         _tabBarItemType = 3;
65     }
66     return self;
67 }
68 
69 @end

 

031实现仿iPhone的底部选项卡

标签:

原文地址:http://www.cnblogs.com/huangjianwu/p/4576037.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!