标签:
导航控制器
一.设置字体大小,背景等。
二.自定义返回按钮。
三.设置手势。
一.导航中也有个appearance属性,通过它可以设置所有导航的颜色。
二.自定义返回按钮。
1.首先需要知道的是,要把一个控制器加到导航上,导航做的操作是首先push到这个控制器上。
2.设置返回按钮的思路:创建按钮->调整按钮内容的边距->设置导航Item的左Item上->导航的子控制器的个数必须大于等于1
三.设置手势。
1.首先设置导航手势的代理。
2.然后再调用手势代理的方法,且必须大于1时才能使用,要不在主控制器里也会调用,这样的话,导航就不能push了
四.上代码。
#import "BSJMainNavViewController.h" @interface BSJMainNavViewController ()<UIGestureRecognizerDelegate> @end @implementation BSJMainNavViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. UINavigationBar *navBar = [UINavigationBar appearance]; NSMutableDictionary *navInfo = [NSMutableDictionary dictionary]; navInfo[NSFontAttributeName] = [UIFont systemFontOfSize:16]; [navBar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault]; [navBar setTitleTextAttributes:navInfo]; //当重写导航的push方法时,导航的手势就失效了,可以这样写 self.interactivePopGestureRecognizer.delegate = self; } - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{ if (self.childViewControllers.count >=1) { UIButton *leftButton = [[UIButton alloc] init]; [leftButton setImage:[UIImage imageNamed:@"navigationButtonReturn"] forState:UIControlStateNormal]; [leftButton setImage:[UIImage imageNamed:@"navigationButtonReturnClick"] forState:UIControlStateHighlighted]; [leftButton setTitle:@"返回" forState:UIControlStateNormal]; [leftButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [leftButton setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted]; [leftButton sizeToFit]; [leftButton addTarget:self action:@selector(backItemClick:) forControlEvents:UIControlEventTouchUpInside]; //按钮中有个imageView 和Label //设置这个是设置imageView的内边距,如果设置它会imageView向内缩进 // leftButton setImageEdgeInsets:<#(UIEdgeInsets)#> //设置这个是设置title的内边距,如果设置它会title向内缩进 // leftButton setTitleEdgeInsets:<#(UIEdgeInsets)#> //设置整个按钮的内边距 [leftButton setContentEdgeInsets:UIEdgeInsetsMake(0,-20, 0, 0)]; viewController.hidesBottomBarWhenPushed = YES; viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton]; } //最后再调这段代码,上面的代码是先改导航的左边按钮 [super pushViewController:viewController animated:animated]; } #pragma mark --- delegate - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{ //因为如果不设置>1,在主页面这个手势也会起作用,这样的话就不能push了就有bug return self.childViewControllers.count > 1; } #pragma mark --- response methods - (void)backItemClick:(UIButton *)sender{ [self popViewControllerAnimated:YES]; } @end
UINavigationController的创建和相关设置---学习笔记四
标签:
原文地址:http://www.cnblogs.com/TheYouth/p/5851149.html