大家做一些App应用时,很多时候会用到UINavigationController。下面我简单谈谈,欢迎指正:
1.使用UINavigationController:
<p class="p1"><span class="s1">ViewController</span><span class="s2"> *vc=[[</span><span class="s1">ViewController</span><span class="s2"> </span><span class="s3">alloc</span><span class="s2">]</span><span class="s3">init</span><span class="s2">];</span></p><p class="p2"><span class="s1">UINavigationController</span><span class="s2"> *navVc=[[</span><span class="s1">UINavigationController</span><span class="s2"> </span><span class="s3">alloc</span><span class="s2">]</span><span class="s3">initWithRootViewController</span><span class="s2">:vc];</span></p><p class="p2"><span class="s4">self</span><span class="s2">.</span><span class="s5">window</span><span class="s2">.</span><span class="s1">rootViewController</span><span class="s2">=navVc;</span></p>
跳转到下一个控制器:<p class="p1"><span class="s1">[</span><span class="s2">self</span><span class="s1">.navigationController pushViewController:下一个控制器 animated:</span><span class="s2">YES</span><span class="s1">];</span></p>
2.自定义UINavigationController,用于整个项目界面风格的统一控制
//设置导航栏的主题
+(void)setupNavBarTheme
{
//1.去除apperance对象
UINavigationBar *navBar=[UINavigationBar appearance];
navBar.tintColor = [UIColor whiteColor];
[navBar setBarTintColor:[UIColor blackColor]];
navBar.translucent = YES;
//2.设置标题属性
NSMutableDictionary *textAttrs=[NSMutableDictionary dictionary];
textAttrs[NSForegroundColorAttributeName]=[UIColor whiteColor];
textAttrs[NSFontAttributeName]=[UIFont systemFontOfSize:18];
textAttrs[UITextAttributeTextShadowOffset]=[NSValue valueWithUIOffset:UIOffsetZero];
textAttrs[UITextAttributeFont]=[UIFont boldSystemFontOfSize:20];
[navBar setTitleTextAttributes:textAttrs];
//去掉导航栏但会按钮自带的文字
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)
forBarMetrics:UIBarMetricsDefault];
}
//设置导航栏按钮主题
+(void)setupBarButtonItemTheme
{
UIBarButtonItem *item=[UIBarButtonItem appearance];
//设置文字属性
NSMutableDictionary *textAttrs=[NSMutableDictionary dictionary];
textAttrs[UITextAttributeTextColor]=[UIColor whiteColor];
textAttrs[UITextAttributeTextShadowOffset]=[NSValue valueWithUIOffset:UIOffsetZero];
textAttrs[UITextAttributeFont]=[UIFont systemFontOfSize:16];
[item setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
[item setTitleTextAttributes:textAttrs forState:UIControlStateHighlighted];
}
3.如果对苹果系统自带的返回按钮不满意,想用自己的返回按钮(我以图片按钮为例)
UIButton *leftButton=[[UIButton alloc]init];
[leftButton setImage:[UIImage imageNamed:@"ct_icon_leftbutton"] forState:UIControlStateNormal];
leftButton.frame=CGRectMake(0, 0, 25, 25);
[leftButton setImageEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 0)];
[leftButton addTarget:self action:@selector(leftBtnClicked) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftItem=[[UIBarButtonItem alloc]initWithCustomView:leftButton];
<p class="p1"><span class="s1"> </span><span class="s2">self</span><span class="s1">.</span><span class="s3">navigationItem</span><span class="s1">.</span><span class="s3">leftBarButtonItem</span><span class="s1"> = leftItem;</span></p>4.自定义返回按钮会失去系统自带的从左往右,从右往左切换效果。要想实现:<pre name="code" class="objc">- (void)leftBtnClicked
{
for (UIViewController *controller in self.navigationController.viewControllers)
{
if ([controller isKindOfClass:[上一个控制器 class]])
{
[self.navigationController popToViewController:controller animated:YES];
}
}
}
上面代码也可以实现从一个控制器返回上上个控制器,而不是上一个控制器。
5.如果想在导航栏放自己想要放的东西:
- (void)setNaviBarItemButton{
UILabel *titleView=[[UILabel alloc]init];
[titleView setText:@"家居"];
titleView.frame=CGRectMake(0, 0, 100, 16);
titleView.font=[UIFont systemFontOfSize:16];
[titleView setTextColor:[UIColor whiteColor]];
titleView.textAlignment=NSTextAlignmentCenter;
self.navigationItem.titleView=titleView;
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"删除"
style:UIBarButtonItemStyleDone
target:self
action:@selector(rightBtnClicked)];
UIButton *leftButton=[[UIButton alloc]init];
[leftButton setImage:[UIImage imageNamed:@"ct_icon_leftbutton"] forState:UIControlStateNormal];
leftButton.frame=CGRectMake(0, 0, 25, 25);
[leftButton setImageEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 0)];
[leftButton addTarget:self action:@selector(leftBtnClicked) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftItem=[[UIBarButtonItem alloc]initWithCustomView:leftButton];
rightButton.tintColor = [UIColor whiteColor];
self.navigationItem.rightBarButtonItem = rightButton;
self.navigationItem.leftBarButtonItem = leftItem;
}