iOS自定义导航栏形状
2016-03-16 10:02
183 查看
很多时候,系统自带的导航不能满足我们的项目需求,我们需要自定,来满足需求。
主要代码:
baseNavigationViewController.h
baseNavigationViewController.m
RootViewController.m
效果图
主要代码:
baseNavigationViewController.h
#import <UIKit/UIKit.h> @interface baseNavigationViewController : UINavigationController @end
baseNavigationViewController.m
#import "baseNavigationViewController.h" #import "SecondViewController.h" @interface baseNavigationViewController () @end @implementation baseNavigationViewController - (void)viewDidLoad { [super viewDidLoad]; [self loadThemeImage]; [self customNavigationBar]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)loadThemeImage { UIImage *image = [UIImage imageNamed:@"top_bar_bkg_6"]; [self.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault]; //// UIButton *buton = [[UIButton alloc]initWithFrame:CGRectMake(5, 0, 60, 44)]; //// buton.backgroundColor = [UIColor colorWithRed:0.2955 green:0.2599 blue:1.0 alpha:1.0]; //// [buton setTitle:@"点击" forState:UIControlStateNormal]; //// [buton setTitle:@"ok" forState:UIControlStateHighlighted]; // // buton.layer.cornerRadius = 5; // buton.layer.masksToBounds = YES; // [buton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside]; // [self.navigationBar addSubview:buton]; } -(void)click:(UIButton *)b{ SecondViewController *sec = [[SecondViewController alloc]init]; [self pushViewController:sec animated:YES]; } - (void)customNavigationBar { if ([self respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) { [self.navigationBar setBackgroundImage:[UIImage imageNamed:@"Title"] forBarMetrics:UIBarMetricsDefault]; } else { [self drawRect:self.navigationBar.bounds]; } [self drawRoundCornerAndShadow]; } - (void)drawRect:(CGRect)rect { [[UIImage imageNamed:@"Title"] drawInRect:rect]; } - (void)drawRoundCornerAndShadow { CGRect bounds = self.navigationBar.bounds; bounds.size.height +=10; UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight) cornerRadii:CGSizeMake(10.0, 10.0)]; CAShapeLayer *maskLayer = [CAShapeLayer layer]; maskLayer.frame = bounds; maskLayer.path = maskPath.CGPath; [self.navigationBar.layer addSublayer:maskLayer]; self.navigationBar.layer.mask = maskLayer; self.navigationBar.layer.shadowOffset = CGSizeMake(3, 3); self.navigationBar.layer.shadowOpacity = 0.7; self.navigationBar.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.navigationBar.bounds].CGPath; } @end
RootViewController.m
#import "RootViewController.h" @interface RootViewController () @end @implementation RootViewController - (void)viewDidLoad { [super viewDidLoad]; self.title = @"自定义导航栏"; UIBarButtonItem *menu = [[UIBarButtonItem alloc]initWithTitle:@"xsac" style:UIBarButtonItemStylePlain target:self action:@selector(click)]; self.navigationItem.rightBarButtonItem = menu; UISegmentedControl *SegmentedControl = [[UISegmentedControl alloc] initWithItems: [NSArray arrayWithObjects: @"笑话", @"论坛", @"段子",nil]]; [SegmentedControl addTarget:self action:@selector(clicks:) forControlEvents:UIControlEventValueChanged]; SegmentedControl.frame = CGRectMake(0, 0, 80, 30); SegmentedControl.momentary = YES; SegmentedControl.tintColor = [UIColor purpleColor]; self.navigationItem.titleView =SegmentedControl; } -(void)clicks:(UISegmentedControl *)seg{ switch (seg.selectedSegmentIndex) { case 0: NSLog(@"111111"); break; case 1: NSLog(@"222222"); break; case 2: NSLog(@"3333333"); break; default: break; } } -(void)click{ NSLog(@"cdcds"); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end
效果图
相关文章推荐
- 点击任意空白收回键盘
- 文本框随键盘动态改变位置(获取键盘弹起高度)
- iOS开发 蓝牙4.0的各种亲身经历踩过得坑
- iOS开发实战之真机调试(免证书)
- iOS项目目录结构
- IOS taBbar item角标设置
- 【转】在写一个iOS应用之前必须做的7件事(附相关资源)
- ios开发笔记之TextView
- iOS开发 - "Cast from pointer to smaller type 'int' loses information” 解决的方法
- IOS之开发者申请测试证书步骤
- iOS使用CollectionView实现瀑布流
- 在写一个iOS应用之前必须做的7件事(附相关资源)
- 详解在iOS App中自定义和隐藏状态栏的方法
- iOS报错笔记
- iOS开发笔记13:顶部标签式导航栏及下拉分类菜单
- IOS开发之 一个实例解析 录音
- iOS开发之 音频播放
- iOS---cell-自适应高度
- iOS 页面跳转传值,属性传值,代理传值,代码块传值,单例传值,通知传值
- IOS GCD 浅析