常用代码收集:
//设置Navigation Bar颜色 self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:(218.0/255.0) green:(228.0 / 255.0) blue:(250.0 / 255.0) alpha:1];
//自定义标题 titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0 , 100, 44)]; titleLabel.backgroundColor = [UIColor clearColor]; //设置Label背景透明 titleLabel.font = [UIFont boldSystemFontOfSize:20]; //设置文本字体与大小 titleLabel.textColor = [UIColor colorWithRed:(0.0/255.0) green:(255.0 / 255.0) blue:(0.0 / 255.0) alpha:1]; //设置文本颜色 titleLabel.textAlignment = UITextAlignmentCenter; titleLabel.text = @"自定义标题"; //设置标题 self.navigationItem.titleView = self.titleLabel;
UIView * tview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 150, 25)];
UIImageView * titleView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"mainTitle.png"]];
[titleView setFrame:CGRectMake(0, 0, 150, 22)];
[tview addSubview:titleView];
self.navigationItem.titleView=tview;
创建一个navigationController 并给他个视图控制器
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]];
//设定标题
self.title = @"首页";
//修改为自定义图片 源码例子可以去我那资源里下载
采用继承自UINavigationController 然后重写 - (void)drawRect:(CGRect)rect
隐藏NavigationBar
[self.navigationController setNavigationBarHidden:YES animated:YES];
navigation的推转动画是可以更改的
navigaiton的出栈有这么几种方式:
[self.navigationController popViewControllerAnimated:YES]; //回到上一个视图控制器
[self.navigationController popToViewController:viewController animated:YES]; //回到某一个视图控制器
[self.navigationController popToViewController: [self.navigationController.viewControllers objectAtIndex: ([self.navigationController.viewControllers count] -层次)] animated:YES];
[self.navigationController popToRootViewControllerAnimated:YES]; //回到根视图控制器
用了UINavigationController后,viewWillAppear方法是没有效果的,要用UINavigationControllerDelegate的
– navigationController:willShowViewController:animated: 方法才可以达到这个目的。
所以要做到这个,你必须做以下几步:
1. 设置代理类 nav.delegate = self;
2. 代理类实现UINavigationControllerDelegate Protocol
3. 在代理类中添加– navigationController:willShowViewController:animated:方法
如: