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:UITabBarSystemItemContac
self.navigationItem.rightBarButtonItem =anotherButton;
[anotherButton release];
其中按钮的样式可以有多种,具体的可以参考:https://developer.apple.com/library/ios/prerelease/#documentation/UIKit/Reference/UIBarButtonItem_Class/
在有些项目中要在右面添加两个按钮,实现的样式如下图:
实现的代码如下图:
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]initWithBarButtonSystemI
tem:UIBarButtonSystemItemAdd
target:self action:@selector(clickSettings:)]; UIBarButtonItem *anotherButton1 = [[UIBarButtonItem alloc]initWithTitle:@"Edit"style:UITabBarSystemItemContac
ts
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 {
UIImage *img = [UIImage imageNamed:@"navbar_background.png"];
CGPoint point = {0,0};
[img drawAtPoint:point];
}
@end
方法3:
@implementation
- (void)drawRect:(CGRect)rect {
//加入旋转坐标系代码
UIImage
CGContextRef
CGContextTranslateCTM(context,
CGContextScaleCTM(context,
CGPoint
CGImageRef
CGContextDrawImage(context,
CGContextDrawImage(context,
CGContextDrawImage(context,
}
@end
old code
CGContextDrawImage(context,
方法4:
有了这个,你还会扩展drawRect自定义导航栏背景吗?
查了很多资料,网上的自定义导航栏的方法,清一色的是扩展navigationBar的drawRect方法.然而这样的扩展会影响到工程里所有的navigationBar.或许你并不想这么做,而且很多不合常规的UI用这种方法根本没法实现.
做了无数实验,尝试了各种方法,今天跟大家分享一个新方法:
#import
@interface
}
@end
@implementation
- (id)initWithRootViewControll
}
- (void)loadView{
}
#pragma mark -
#pragma mark UINavigationControllerDe
- (void)navigationController:(UINavigationController
}
@end