导航类视图之UINavigationController

1,创建并使用一个UINavigationController

UINavigationController *aNav = [[UINavigationController alloc] init];

然后添加一个视图进去,否则导航栏也没有意义的

UIViewController *aViewCtrl = [[UIView alloc] initWithNibName: (*xib文件名*)];

[aNav pushViewController:aViewCtrl animated:NO];//导航栏的第一个视图不要动画化

aViewCtrl.title = @"标题"; //设置其标题:

2,设置导航栏的左右按钮:设置导航栏的按钮并不是去设置导航栏本身,而是当时被导航的视图控制器,比如我们对aView作设置。

//配置一个按钮,我这里是《我的佛典》上的代码

UIBarButtonItem *callModalViewButton = [[UIBarButtonItem alloc] initWithTitle:@"经文"

                                   style:UIBarButtonItemStyleBordered target:self action:@selector(callModalList)];

self.navigationItem.leftBarButtonItem = callModalViewButton;

[callModalViewButton release]; //由于本地视图会retain它,所以我们可以release了

可以看到,还是很简单的嘛。

3,其他常用方法和属性:

本地视图.navigationItem.leftBarButtonItem //左边栏项目本地视图.

navigationItem.rightBarButtonItem //右边栏项目本地视图.

navigationItem.backBarButtonItem //后退栏项目本地视图.

navigationItem.hidesBackButton //隐藏后退按钮(YES or NO)


在视图的viewWillAppear:方法中添加:[self.tableView reloadData]; 不起作用,viewWillAppear:这个方法根本没有调用,

后来发现原来用了UINavigationController后,viewWillAppear方法是没有效果的,要用UINavigationControllerDelegate的

– navigationController:willShowViewController:animated:

方法才可以达到这个目的。


所以要做到这个,你必须做以下几步:

1. 设置代理类nav.delegate = self;

2. 代理类实现UINavigationControllerDelegate Protocol

3. 在代理类中添加– navigationController:willShowViewController:animated:

方法如:

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated

{

[self.myTableView reloadData];

}


pushViewController:viewController animated:BOOL(加载视图控制器)

– 添加指定的视图控制器并予以显示,后接:是否动画显示


popViewControllerAnimated:BOOL(弹出当前视图控制器)

– 弹出并向左显示前一个视图


popToViewController:viewController animated:BOOL(弹出到指定视图控制器)

– 回到指定视图控制器, 也就是不只弹出一个


popToRootViewControllerAnimated:BOOL(弹出到根视图控制器)

– 比如说你有一个“Home”键,也许就会实施这个方法了。


setNavigationBarHidden:BOOL animated:BOOL(设置导航栏是否显示)

– 如果你想隐藏导航栏,这就是地方了。


参照Picasa的WebApp样式,现pushViewController:animated:的不同页面转换特效

1. 首先要明确的是,不使用pushViewController的默认动画,所以在调用这个函数时,要将animated设置为NO.

2. 使用普通的来CATransition实现转换效果,代码如下:

CATransition *animation = [CATransition animation];

[animation setDuration:0.3];

[animation setType: kCATransitionMoveIn];[animation setSubtype: kCATransitionFromTop];

[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];

[self.navigationController pushViewController:m_poseAddIssueViewController animated:NO];

[self.navigationController.view.layer addAnimation:animation forKey:nil];


经常要在导航栏中添加各种样式的按钮,添加一个按钮很简单,代码如下图:

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

其中按钮的样式可以有多种,具体的可以参考:https://developer.apple.com/library/ios/prerelease/#documentation/UIKit/Reference/UIBarButtonItem_Class/

在有些项目中要在右面添加两个按钮,实现的样式如下图:

image

 

实现的代码如下图:

UIToolbar* tools = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 150, 45)];//使用一个容器去装载多个UIBarButtonItem是实现多个按钮的最核心的部分
[tools setTintColor:[self.navigationController.navigationBartintColor]]; 
[tools setAlpha:[self.navigationController.navigationBaralpha]]; 
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];


隐藏当前页的navigationBar:
    [self.navigationController setNavigationBarHidden:YES animated:YES];


给 UINavigationBar 设置背景图片

方法1:

+ (UINavigationBar *)createNavigationBarWithBackgroundImage:(UIImage *)backgroundImage title:(NSString *)title {


    UINavigationBar *customNavigationBar = [[[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)] autorelease];
    UIImageView *navigationBarBackgroundImageView = [[UIImageView alloc] initWithImage:backgroundImage];
    [customNavigationBar addSubview:navigationBarBackgroundImageView];
    UINavigationItem *navigationTitle = [[UINavigationItem alloc] initWithTitle:title];
    [customNavigationBar pushNavigationItem:navigationTitle animated:NO];
    [navigationTitle release];
    [navigationBarBackgroundImageView release];
    return customNavigationBar;



调用的时候:
self.navigationController.navigationBar.hidden = YES;
    UIImage *navigationBarBackgroundImage =[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"topbar-bg" ofType:@"png"]];
    UINavigationBar *customNavigationBar = [YOUR_Util_Class createNavigationBarWithBackgroundImage:navigationBarBackgroundImage title:nil];
    [self.view addSubview:customNavigationBar];



方法2:

利用objective-c的Category语法扩展UINavigationBar 类

具体代码为

@implementation UINavigationBar(UINavigationBarCategory)

- (void)drawRect:(CGRect)rect {
    // Drawingcode 
UIImage *img = [UIImage imageNamed:@"navbar_background.png"];
CGPoint point = {0,0};

[img drawAtPoint:point];


}
@end

 

方法3:

@implementation UINavigationBar(UINavigationBarCategory)

 

- (void)drawRect:(CGRect)rect {

//加入旋转坐标系代码

   // Drawing code

UIImage *navBarImage =[UIImage imageNamed:@"LOGO_320×44.png"];

CGContextRef context= UIGraphicsGetCurrentContext();

CGContextTranslateCTM(context, 0.0, self.frame.size.height);

CGContextScaleCTM(context, 1.0,-1.0);

CGPoint center=self.center;

 

CGImageRef cgImage= CGImageCreateWithImageInRect(navBarImage.CGImage,CGRectMake(0, 0, 1, 44));

CGContextDrawImage(context, CGRectMake(center.x-160-80, 0, 80,self.frame.size.height),cgImage);

CGContextDrawImage(context, CGRectMake(center.x-160, 0, 320,self.frame.size.height),navBarImage.CGImage);

CGContextDrawImage(context, CGRectMake(center.x+160, 0, 80,self.frame.size.height),cgImage);


}

@end


old code

CGContextDrawImage(context, CGRectMake(0, 0, self.frame.size.width,self.frame.size.height),navBarImage.CGImage);

方法4:

有了这个,你还会扩展drawRect自定义导航栏背景吗?

查了很多资料,网上的自定义导航栏的方法,清一色的是扩展navigationBar的drawRect方法.然而这样的扩展会影响到工程里所有的navigationBar.或许你并不想这么做,而且很多不合常规的UI用这种方法根本没法实现.

做了无数实验,尝试了各种方法,今天跟大家分享一个新方法:

#import <QuartzCore/QuartzCore.h>

 

@interface DDNavigationViewController: UINavigationController<UINavigationControllerDelegate>{

    CALayer *_barBackLayer;

}

 

@end

@implementation DDNavigationViewController

 

- (id)initWithRootViewController:(UIViewController *)rootViewController{

    self =[super initWithRootViewController:rootViewController];

    self.delegate self;

   return self;

}

- (void)loadView{

    [super loadView];

    

   UINavigationBar *bar= self.navigationBar;

 

    CALayer*layer= [CALayer layer];

   UIImage *navBarImage= [UIImage imageNamed:@"navigationBarBackground.png"];

    layer.contents =(id)navBarImage.CGImage;

    layer.frameCGRectMake(0,0, 320,navBarImage.size.height);

    [bar.layer insertSublayer:layer atIndex:0];

    _barBackLayer =layer;

}

 

#pragma mark -

#pragma mark UINavigationControllerDelegate

- (void)navigationController:(UINavigationController *)navigationControllerdidShowViewController:(UIViewController *)viewControlleranimated:(BOOL)animated{

    [_barBackLayer removeFromSuperlayer];

   [navigationController.navigationBar.layer insertSublayer:_barBackLayeratIndex:0];

}

@end


欢迎喜欢交流和热心的iphone开发朋友加入qq群参与讨论:186739796,验证码:csdn。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值