UINavigationController

自定义导航栏上的返回按钮

导航栏的按钮,右边的按钮是可以自己随意添加的。但左边的返回按钮怎么定制?你会说,添加一个自己的按钮呗!你可以试试看,这样行不行。

正确的答案是重载UINavigationController类的pushViewController:animated方法。

#import <UIKit/UIKit.h>

@interface MyNavigationController: UINavigationController {

}

@end

 

#import "MyNavigationController.h"

 

@implementation MyNavigationController

-(void)popself

{

    [self popViewControllerAnimated:YES];

}

-(UIBarButtonItem*) createBackButton

return [[UIBarButtonItem alloc]

initWithTitle:@"返回"

style:UIBarButtonItemStyleBordered

target:self

action:@selector(popself)];

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated 

    [super pushViewController:viewControlleranimated:animated]; 

if (viewController.navigationItem.leftBarButtonItem== nil && [self.viewControllers count] > 1) { 

        viewController.navigationItem.leftBarButtonItem =[self createBackButton];

    } 

@end

 *******************************************************************************

使用MyNavigationController替换UINavigationController。或者直接创建一个UINavigationController的新类别


self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(popself)];添加在对应的UIViewController上即可。
不过这个只会影响当前的UIViewController,即当navigationController导航该UIViewController时,显示的回退才会是“返回”,导般其它的还是会是上一个view controller的title名称~!

自定义UINavigationController的返回按钮标题

使用pushViewController切换到下一个视图时,navigation controller按照以下3条顺序更改导航栏的左侧按钮。

1、如果B视图有一个自定义的左侧按钮(leftBarButtonItem),则会显示这个自定义按钮;

2、如果B没有自定义按钮,但是A视图的backBarButtonItem属性有自定义项,则显示这个自定义项;

3、如果前2条都没有,则默认显示一个后退按钮,后退按钮的标题是A视图的标题。

把UIBarButtonItem *backItem……这段代码放在A视图的pushViewController语句之前。
B视图的后退按钮的标题变成back了。
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"back"style:UIBarButtonItemStyleBordered target:nil action:nil][self.navigationItem setBackBarButtonItem:backItem][backItem release][self.navigationController pushViewController:self.bView animated:YES];
 
 

1.UINavigationController是用于构建分层应用程序的主要工具,它维护了一个视图控制器栈,任何类型的视图控制器都可以放入.

  它在管理,以及换入和换出多个内容视图方面,与UITabBarController(标签控制器)类似. 两者间的主要不同在于: UINavigationController是作为栈来实现,它更适合用于处理分层数据.

 它还有一个作用是用作顶部菜单。

2. 根控制器,子控制器.

    在设计导航控制器时,需要指定用户看到的第一个视图,该视图处在导航栈的最底层,其对应的控制器称为根控制器.所以不要把根控制器理解为导航控制器.根控制器也是导航控制器的一个子控制器.

    在术语上把栈中的除了根控制器其余的控制器称为子控制器.

    一般地,根控制器对应的视图上有各个子控制器视图的入口,回退到根视图然后再切换各个子视图.

    默认地,导航控制器会自动在当前子控制器对应视图的导航栏的右端加一个返回按钮,名称是上一个视图的标题.

 

3. 导航按钮.

   导航按钮类似于网页上的后退按钮,当点击时,当前的视图控制器出栈,栈中的下一个视图成为当前视图.

 

4. 其它术语:

    扩展图标(accessory icon)称为扩展指示器(disclosure indicator),告知将切换到另一个视图,它以一个灰色箭头表示.

    细节展示按钮(detail disclosure button)不仅是一个图标,它还是一个可单击的控件.

 

5.静态使用UINavigationController

   <<iphone开发基础教程>>第9章,新建window-based application,使用IB向mainwindow.xib添加了一个UINavigationController,

 

6.控制器与其成员间的关系

(1).UINavigationController

主要成员:

   UINavigationBar *navigationBar;应该是管理了所有子控制器的navigationItem,显示在窗口顶部;

   UIToolbar *toolbar;工具栏,显示在窗口底部;


(2).UINavigationBar管理了一组 UINavigationItem.

     navigationBar.frame.size.height=44;

 

(4).UITabBar管理了一组 UITabBarItem.

     UITabBarController

     tabBar.frame.size.height=49;

 

(5).UIViewController提供了基础的view管理模型.

  主要成员:

  UINavigationItem *navigationItem;用于展现视图控制,第一次被访问时创建,被添加到一个navigationBar中.

  NSArray *toolbarItems;一组UIBarButtonItem,被添加到一个toolBar中.

  UITabBarItem *tabBarItem;被添加到一个tabBar中.

 

(6).UINavigationItem是一个视图顶部上的导航条

   有如下主要成员:

   UIView* titleView:显示在导航栏中间.有解释说This property is ignored if leftBarButtonItem is not nil.,使用后发现不是这样的.

   UIBarButtonItem *leftBarButtonItem

   UIBarButtonItem *rightBarButtonItem


   系统的刷新图标,但是它带有边框。

   self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refresh)];
    

   NavigationController导航栏中添加多个UIBarButtonItem
   http://wangjun.easymorse.com/?p=975

 

(7).UIBarButtonItem封装了一些属性和行为,它被添加到UIToolBar中.

   主要成员

   SEL action;

   UIView* customView;

 

7.一些高级控制器

  UIImagePickerController,

  UITableViewController,

 

8.http://blog.zhaojie.me/2010/12/iphone-composition-resistant-uitabbarcontroller.html

   发现UITabBarController不能放入其他的视图内,而只能直接放在Window上(或Window里的UINavigationController里),否则会出现界面向下偏移的情况。对于UITabBarController抗拒组合的情况,深表叹息。

  并且UITabBarController中的tabBar如何隐藏??

 

9.在UIViewController中访问导航:

    通常在UIViewController的子类中调用self.navigationController来调用视图的推入和弹出。不能弹出根,

    要改变导航栏的样式或颜色,访问self.navigationController.navigationBar

    要改变上面的左右按钮或标题内容,访问self.navigationItem,它描述导航栏上的控件内容。

10.UINavigationItem:

     UINavigationItem存储导航项属性,包括:左右按钮,标题,标题视图,返回本页的按钮。还有一个prompt,以导航栏上方再显示一行提示,把导航栏位置向下挤。

 11.定制标题的3种方法:

     self.title=@"标题文字";self.title=[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"];

     self.navigationItem.title=@"标题文字"

     self.navigationItem.titleView=

12.在UIViewController中,使用self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.20392f green:0.19607f blue:0.61176f alpha:1.0f];改变导航栏的颜色,它会形成以中间向上下两边的渐变。其它颜色值不确定能否形成这样的效果。

     使用self.navigationItem.rightBarButtonItem等控制导航栏上面的内容。


13.可以继承出一个UINavigationController的子类 ,重写pushViewController,popViewControllerAnimated方法,

     比如可以实现点击默认返回按钮时,二次确认是否执行返回操作。

 

14.presentModalViewController弹出一个UIViewController时,可能需要它有导航栏,以摆放功能按钮。

    只要再包一层UINavigationController即可。

        LocalPhotoViewController* viewController=[[LocalPhotoViewController alloc] initWithImageArray:arr currentIndex:0];
        UINavigationController* nav=[[UINavigationController alloc] initWithRootViewController:viewController];
        [self presentModalViewController:nav animated:YES];
        [nav release];
        [viewController release];

        取消时使用[self dismissModalViewControllerAnimated:YES];,viewController在内部执行这个也有效。


15.可以使用比标题栏高的背景图片,在UINavigationBar的drawrect中,把它向下延伸一点画出来,可以达到最下边有层半透明的效果。


16.加下阴影

     #import "QuartzCore/CALayer.h"

     CALayer* navLayer = self.navigationController.navigationBar.layer;
    navLayer.masksToBounds = NO;
    navLayer.shadowColor = [UIColor blackColor].CGColor;
    navLayer.shadowOffset = CGSizeMake(0.0, 5.0);
    navLayer.shadowOpacity = 0.25f;
    navLayer.shouldRasterize = YES;//加了这句,可能会导致导航栏内容错乱。

17.如果在push后立即pop,若使用了animited,则要延0.4秒再pop,否则可能会导航变为了上一层的,而内容还是本层的。如果animited为NO,则可以立即pop。


UINavigationController的相关设置
显示导航条的背景色:

 

navigation.navigationBar.tintColor = [UIColorredColor];

添加一个按钮:

UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Setting" style:UITabBarSystemItemContacts 
                                                                 target:self action:@selector(clickSettings:)];          
self.navigationItem.rightBarButtonItem = anotherButton; 
[anotherButton release];

右面添加两个按钮:

 

UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 150, 45)]; 
[tools setTintColor:[self.navigationController.navigationBar tintColor]]; 
[tools setAlpha:[self.navigationController.navigationBar alpha]]; 
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:2];

UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd 
                        target:self action:@selector(clickSettings:)];

UIBarButtonItem *anotherButton1 = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UITabBarSystemItemContacts 
                                                        target:self action:@selector(clickEdit:)]; 
[buttons addObject:anotherButton]; 
[anotherButton release]; 
[buttons addObject:anotherButton1]; 
[anotherButton1 release]; 
[tools setItems:buttons animated:NO]; 
[buttons release]; 
UIBarButtonItem *myBtn = [[UIBarButtonItem alloc] initWithCustomView:tools]; 
self.navigationItem.rightBarButtonItem = myBtn;

[myBtn release]; 
[tools release];

下面方法可以通过Apple审核,导航上的按钮背景需要做,否则看起来不那么和之又谐


//CustomNavigationBar.h   
@interface UINavigationBar (UINavigationBarCategory)   
UIImageView *backgroundView;   
(void)setBackgroundImage:(UIImage*)image;   
(void)insertSubview:(UIView *)view atIndex:(NSInteger)index;   
@end   
  
//CustomNavigationBar.m   
@implementation UINavigationBar (UINavigationBarCategory)   
-(void)setBackgroundImage:(UIImage*)image   
{   
    if(image == nil)   
    {   
        [backgroundView removeFromSuperview];   
    }   
    else   
    {   
        backgroundView [[UIImageView alloc] initWithImage:image];   
        backgroundView.tag 1;   
        backgroundView.frame CGRectMake(0.f, 0.f, self.frame.size.width, self.frame.size.height);   
        backgroundView.autoresizingMask UIViewAutoresizingFlexibleWidth UIViewAutoresizingFlexibleHeight;   
        [self addSubview:backgroundView];   
        [self sendSubviewToBack:backgroundView];   
        [backgroundView release];   
    }   
}   
  
//for other views   
(void)insertSubview:(UIView *)view atIndex:(NSInteger)index   
{   
    [super insertSubview:view atIndex:index];   
    [self sendSubviewToBack:backgroundView];   
}   
@end   
  
//YourViewController.m   
(void)viewWillAppear:(BOOL)animated   
{   
    [super viewWillAppear:animated];   
    [self.navigationController.navigationBar   
        setBackgroundImage:[UIImage imageNamed:@"navigation_bar_bg.png"]];   
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值