iOS UITabBar 详解

来源:互联网 发布:js输出对象的内容 编辑:程序博客网 时间:2024/05/16 12:27

http://blog.sina.com.cn/s/blog_63578f140100w56m.html

UITabBar* tabBar = [[UITabBar alloc] initWithFrame:CGRectMake(40,0.0,240,30)];

    [mainView addSubview:tabBar];

    [tabBar release];

    

    UITabBarItem *tabBarItem1 = [[UITabBarItem allocinitWithTitle:@"排队人数" image:nil tag:0];

    UITabBarItem * tabBarItem2 = [[UITabBarItem allocinitWithTitle:@"人均" image:nil tag:1];

    UITabBarItem * tabBarItem3 = [[UITabBarItem allocinitWithTitle:@"距离" image:nil tag:2];

    UITabBarItem * tabBarItem4 = [[UITabBarItem allocinitWithTitle:@"好评" image:nil tag:3];

    NSArray *tabBarItemArray = [[NSArray allocinitWithObjectstabBarItem1, tabBarItem2, tabBarItem3, tabBarItem4,nil];

    [tabBar setItemstabBarItemArray];

    [tabBarItemArray release];

    好像tabBar.backgroundColor = [UIColor blueColor];设置背景不起作用

    alpha可以起作用


http://www.2cto.com/kf/201403/289348.html

push页面时,可调用hidesBottomBarWhenPushed进行隐藏。

第一步,我们需要一些图片:

\

各个选项的图标和tabbar的背景图片,最后还要一个透明的1x1像素的图片。

第二步,新建一个工程,在工程内建一个继承于UITabBarController的类。

\

第三步,首先写一个方法,返回一个UINavigationController

?
1
2
3
4
5
6
7
8
-(UINavigationController*) viewControllerWithTitle:(NSString*) title image:(UIImage*)image
{
    UIViewController* viewController = [[UIViewController alloc] init];
    viewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:image tag:0];
    viewController.title = title;
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:viewController];
    returnnav;
}

然后在viewDidLoad里面创建TabbarController的viewControllers

?
1
2
3
4
5
6
self.viewControllers = [NSArray arrayWithObjects:
                           [self viewControllerWithTitle:@"1"image:IMG(@"1")],
                           [self viewControllerWithTitle:@"2"image:IMG(@"2")],
                           [self viewControllerWithTitle:nil image:nil],
                           [self viewControllerWithTitle:@"3"image:IMG(@"3")],
                           [self viewControllerWithTitle:@"4"image:IMG(@"4")], nil];

看到没有,比较猥琐的就是第三个ViewController什么都没设置。

因为我们要在那个位置放一个自己的按钮,继续在viewDidLoad写:

?
1
2
3
4
5
UIButton* button = [[UIButton alloc]initWithFrame:CGRectMake(0.0,0.0,65,65)];
button.center = CGPointMake(160,20);
[button setBackgroundImage:IMG(@"add") forState:UIControlStateNormal];
[button addTarget:self action:@selector(add:) forControlEvents:UIControlEventTouchUpInside];
[self.tabBar addSubview:button];

然后设置背景图片:

?
1
[self.tabBar setBackgroundImage:IMG(@"tabbarbg")];

运行之后是这样的:

\

会发现按钮上面有一条横线,然后再设置这个阴影运行后就没有人发现你猥琐的行径。

?
1
[self.tabBar setShadowImage:IMG(@"transparent")];
最后效果图(iOS7和iOS6):


\<

http://blog.csdn.net/likendsl/article/details/7620726

UITabBarController是IOS中很常用的一个viewController,例如系统的闹钟程序,ipod程序等。UITabBarController通常作为整个程序的rootViewController,而且不能添加到别的container viewController中。

  首先我们看一下它的view层级图:

一、手动创建UITabBarController

  最常见的创建UITabBarController的地方就是在application delegate中的 applicationDidFinishLaunching:方法,因为UITabBarController通常是作为整个程序的rootViewController的,我们需要在程序的window显示之前就创建好它,具体步骤如下:

  1、创建一个UITabBarController对象

  2、创建tabbarcontroller中每一个tab对应的要显示的对象

  3、通过UITabBarController的viewController属性将要显示的所有content viewcontroller添加到UITabBarController中

  4、通过设置UITabBarController对象为window.rootViewController,然后显示window

  下面看一个简单的例子:

[cpp] view plain copy
 print?
  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
  2. {  
  3.     self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];  
  4.     // Override point for customization after application launch.  
  5.       
  6.    SvTabBarFirstViewController *viewController1, *viewController2;  
  7.   
  8.     viewController1 = [[SvTabBarFirstViewController alloc] initWithNibName:nil bundle:nil];  
  9.     viewController1.title = @"First";  
  10.       
  11.     viewController2 = [[SvTabBarFirstViewController alloc] initWithNibName:nil bundle:nil];  
  12.     viewController2.title = @"Second";  
  13.       
  14.     self.tabBarController = [[[UITabBarController alloc] init] autorelease];  
  15.     self.tabBarController.delegate = self;  
  16.     self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];  
  17.   
  18.     [viewController1 release];  
  19.     [viewController2 release];  
  20.       
  21.     self.window.rootViewController = self.tabBarController;  
  22.     [self.window makeKeyAndVisible];  
  23.       
  24.     return YES;  
  25. }  

二、UITabBarItem

  UITabBar上面显示的每一个Tab都对应着一个ViewController,我们可以通过设置viewcontroller.tabBarItem属性来改变tabbar上对应的tab显示内容。否则系统将会根据viewController的title自动创建一个,该tabBarItem只显示文字,没有图像。当我们自己创建UITabBarItem的时候,我们可以显示的指定显示的图像和对应的文字描述。当然还可以通过setFinishedSelectedImage:withFinishedUnselectedImage:方法给选中状态和飞选中状态指定不同的图片。下面看个自己创建UITabBarItem的小例子:

[cpp] view plain copy
 print?
  1. UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"Second" image:nil tag:2];  
  2. [item setFinishedSelectedImage:[UIImage imageNamed:@"second.png"]   
  3.    withFinishedUnselectedImage:[UIImage imageNamed:@"first.png"]];  
  4. viewController2.tabBarItem = item;  
  5. [item release];  

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

 

三、moreNavigationController

  UITabBar上最多可以显示5个Tab,当我们往UITabBarController中添加超过的viewController超过5个时候,最后一个一个就会自动变成,按照设置的viewControlles的顺序,显示前四个viewController的tabBarItem,后面的tabBarItem将不再显示。当点击more时候将会弹出一个标准的navigationViewController,里面放有其它未显示的的viewController,并且带有一个edit按钮,通过点击该按钮可以进入类似与ipod程序中设置tabBar的编辑界面。编辑界面中默认所有的viewController都是可以编辑的,我们可以通过设置UITabBarController的customizableViewControllers属性来指定viewControllers的一个子集,即只允许一部分viewController是可以放到tabBar中显示的。但是这块儿要注意一个问题就是每当UITabBarController的viewControllers属性发生变化的时候,customizableViewControllers就会自动设置成跟viewControllers一致,即默认的所有的viewController都是可以编辑的,如果我们要始终限制只是某一部分可编辑的话,记得在每次viewControlles发生改变的时候,重新设置一次customizableViewControllers。

  

四、UITabBarController的Rotation

  UITabBarController默认只支持竖屏,当设备方向放生变化时候,它会查询viewControllers中包含的所有ViewController,仅当所有的viewController都支持该方向时,UITabBarController才会发生旋转,否则默认的竖向。

  此处需要注意当UITabBarController支持旋转,而且发生旋转的时候,只有当前显示的viewController会接收到旋转的消息。

 

五、UITabBar

   UITabBar自己有一些方法是可以改变自身状态,但是对于UITabBarController自带的tabBar,我们不能直接去修改其状态。任何直接修改tabBar的操作将会抛出异常,下面看一个抛出异常的小例子:

[cpp] view plain copy
 print?
  1. self.tabBarController = [[[UITabBarController alloc] init] autorelease];  
  2. self.tabBarController.delegate = self;  
  3. self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, viewController3, nil];  
  4.       
  5. self.window.rootViewController = self.tabBarController;  
  6. [self.window makeKeyAndVisible];  
  7.       
  8. self.tabBarController.tabBar.selectedItem = nil;  

  上面代码的最后一行直接修改了tabBar的状态,运行程序回得到如下结果:

 

六、Change Selected Viewcontroller

  改变UITabBarController中当前显示的viewController,可以通过一下两种方法:

  1、selectedIndex属性

  通过该属性可以获得当前选中的viewController,设置该属性,可以显示viewControllers中对应的index的viewController。如果当前选中的是MoreViewController的话,该属性获取出来的值是NSNotFound,而且通过该属性也不能设置选中MoreViewController。设置index超出viewControllers的范围,将会被忽略。

  2、selectedViewController属性

  通过该属性可以获取到当前显示的viewController,通过设置该属性可以设置当前选中的viewController,同时更新selectedIndex。可以通过给该属性赋值

tabBarController.moreNavigationController可以选中moreViewController。

  3、viewControllers属性

  设置viewControllers属性也会影响当前选中的viewController,设置该属性时UITabBarController首先会清空所有旧的viewController,然后部署新的viewController,接着尝试重新选中上一次显示的viewController,如果该viewController已经不存在的话,会接着尝试选中index和selectedIndex相同的viewController,如果该index无效的话,则默认选中第一个viewController。

 

七、UITabBarControllerDelegate

  通过代理我们可以监测UITabBarController的当前选中viewController的变化,以及moreViewController中对编辑所有viewController的编辑。通过实现下面方法:


  该方法用于控制TabBarItem能不能选中,返回NO,将禁止用户点击某一个TabBarItem被选中。但是程序内部还是可以通过直接setSelectedIndex选中该TabBarItem。

  下面这三个方法主要用于监测对moreViewController中对view controller的edit操作

[cpp] view plain copy
 print?
  1. - (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray *)viewControllers;  
  2.   
  3. - (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed;  
  4.   
  5. - (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed;  

本文转载自:http://www.cnblogs.com/smileEvday/archive/2012/05/10/2495153.html#2374395




0 0
原创粉丝点击