一直只用他的跳转视图方式,没用过他的按钮之类的,今天用起来挺纠结的。回过头来再看看
创建一个navigationController 并给他个视图控制器
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]];
这样的话 ViewController 就可以在 ViewDidLoad 里面进行操作 比如
//设定标题
self.title = @"首页";
//修改颜色
self.navigationController.navigationBar.tintColor = [UIColor redColor];
//修改为自定义图片
采用继承自UINavigationController 然后重写 - (void)drawRect:(CGRect)rect
//修改右侧的BarButtonItem 同理可修改左侧按钮
UIBarButtonItem *callModalViewButton = [[UIBarButtonItem alloc] initWithTitle:@"按钮"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(doClicked)];
self.navigationItem.rightBarButtonItem = callModalViewButton;
如果想更改推出来的子类的返回按钮的话 可以更改 self.navigationItem.backBarButtonItem;
在看 jinglijun 的博客的时候http://blog.csdn.net/jinglijun/article/details/7362329
他提到
用了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];
}
但是 我做这个测试了下 我的无论是父视图还是子视图 都会调用 ViewWillAppear.所以辩证性的看这个问题,等遇到了和jinglijun 一样的情况就注意下。
隐藏NavigationBar
[self.navigationController setNavigationBarHidden:YES animated:YES];
navigation的推转动画是可以更改的
CATransition *transition = [CATransitionanimation];
transition.duration = 0.3;
transition.timingFunction = [CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionDefault];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromLeft;
[self.navigationController.view.layeraddAnimation:transition forKey:nil];
[self.navigationControllerpopViewControllerAnimated:NO];
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]; //回到根视图控制器