Iphone各种栏的设置

 一、NavigationBar(导航栏)

1、修改导航栏文字颜色。

默认的文字颜色为白色,可以通过方法修改为自己想要的颜色: 

[cpp]  view plain copy
  1. //设置一个label  
  2. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,100,30)];  
  3. //设置label属性  
  4. label.font = [UIFont systemFontOfSize:20.0];  
  5. label.textColor = [UIColor blueColor];//设置为蓝色  
  6. label.backgroundColor = [UIColor clearColor];  
  7. label.textAlignment = UITextAlignmentCenter;  
  8. label.text = @"Mytest";  
  9. self.navigationItem.titleView = label;  
  10. [label release];  

效果如图一所示:

                                         图一

2、修改导航栏背景颜色。

 默认的导航栏颜色如上图所示,这里的修改颜色包含修改为目标颜色及设置自己的背景图。

1)、设置为目标颜色,可以通过如下方法实现:

[cpp]  view plain copy
  1. self.navigationController.navigationBar.tintColor = [UIColor greenColor];//设置为绿色  

效果如图二所示:

                                                图二

2)、设置目标图片作为背景,可以通过如下方法:

一种方法是通过重载UINavigationBar的drawRect方法,具体如下:

[cpp]  view plain copy
  1. @implementation UINavigationBar (CustomImage)     
  2. - (void )drawRect:(CGRect)rect {     
  3.     UIImage *image = [UIImage imageNamed: @"background.png" ];     
  4.     [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];     
  5. }     
  6. @end   


不过这样会导致后面所有的页面都是一个风格。

在实际中,假如应用程序前面有些页面是图三的导航栏,后面的页面是图四的导航栏,则我们可以为UINavigationBar添加新的方法,实现如下:

                                图三


                           图四

新建两个文件,分别为CustomNav.h和CustomNav.m,对应的内容分别为

[cpp]  view plain copy
  1. #define kSCNavBarImageTag 6183746  
  2.   
  3. //CustomNavigationBar.h  
  4. @interface UINavigationBar (UINavigationBarCategory)   
  5. - (void)setBackgroundImage:(UIImage*)image;  
  6. @end  


//////////////////////////////////////////////////////////////////////////////////////////////////////////

[cpp]  view plain copy
  1. #import "CustomNav.h"  
  2.   
  3. @implementation UINavigationBar (UINavigationBarCategory)  
  4.   
  5. -(void)setBackgroundImage:(UIImage*)image  
  6. {  
  7.     UIImageView *bgView = (UIImageView *)[self viewWithTag:kSCNavBarImageTag];  
  8.     if (bgView != nil) {  
  9.         [bgView removeFromSuperview];  
  10.     }  
  11.   
  12.     if (image != nil) {  
  13.         bgView = [[UIImageView alloc] initWithImage:image];  
  14.         [bgView setTag:kSCNavBarImageTag];  
  15.         bgView.frame = CGRectMake(0.f, 0.f, self.frame.size.width, self.frame.size.height);  
  16.         bgView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;  
  17.         [self insertSubview:bgView atIndex:0];  
  18.         [self sendSubviewToBack:bgView];  
  19.         [bgView release];  
  20.     }  
  21. }  
  22. @end  


在对应的页面添加头文件,使用方法为:

[cpp]  view plain copy
  1. [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed: @"background.png" ]] ;  


则显示结果如下:

                                             图五

页面显示了该导航栏效果,如果想取消该效果,显示另外的导航栏,则在viewWillAppear添加如下代码:

[cpp]  view plain copy
  1. [self.navigationController.navigationBar setBackgroundImage:nil];  


二、TabBar(标签页)

1、修改标签页背景颜色。

默认的标签页颜色为黑色,如图六所示:

                                         图六

则可以通过以下方法修改颜色,假如修改为蓝色,如图七所示:

                                       图七

新建两个文件,分别为CustomUITabBarController.h和CustomUITabBarController.m,对应的内容分别为

[cpp]  view plain copy
  1. #import <Foundation/Foundation.h>  
  2.   
  3. @interface CustomUITabBarController:UITabBarController{  
  4.   
  5. }  
  6. @end  


//////////////////////////////////////////////////////////////////////////////////////////////////////////////

[cpp]  view plain copy
  1. #import "CustomUITabBarController.h"  
  2.   
  3.    
  4.   
  5. @interface UITabBarController (customTabBar)  
  6.   
  7. -(UITabBar *) tabBar;  
  8.   
  9. @end;  
  10.   
  11.    
  12.   
  13. @implementation CustomUITabBarController  
  14.   
  15. -(void) viewDidLoad{  
  16.   
  17.        [super viewDidLoad];   
  18.   
  19.        CGRect frame=CGRectMake(0.0,0.0,self.view.bounds.size.width,48);   
  20.       UIView *view=[[UIView alloc] initWithFrame:frame];   
  21.       [view setBackgroundColor:kMainColor];   
  22.       [view setAlpha:0.5];   
  23.       [[self tabBar] insertSubView:viewatIndex:0];   
  24.       [view release];   
  25.   
  26. }  
  27.   
  28. @end  


使用方法,首先添加该头文件,方法与UITabBarController一致。

三、StatusBar(状态栏)

1、修改状态栏颜色。

状态栏默认的颜色如图八,为Gray style(default)

可以通过修改工程中的Info.plist,该文件主要是用于应用程序的图标、状态栏(缺省样式、黑色、隐藏)、应用的方向要求、是否需要WIFI网络等。在里面选择“Status bar style”为“Opaque black style”;或者通过代码

[application setStatusBarStyle:UIStatusBarStyleBlackOpaque];

通过设置,则显示如图九效果:

2、隐藏状态栏。

状态栏的默认状态是显示的,可以通过修改Info.plist,在里面添加一行设置“Status bar is initially hidden”,里面选择“YES”,则可以隐藏状态栏;同样也可以在viewLoad中,添加代码:

[[UIApplication sharedApplication] setStatusBarHidden:YES];

如图十效果:

我的话费充值店-各种面额

电信100元仅售98.60 
联通100仅售99.00
移动100仅售99.30

猜你喜欢

转载自wenzongliang.iteye.com/blog/1669072