iOS自定义导航栏形状

来源:互联网 发布:淘宝如何免费引流 编辑:程序博客网 时间:2024/04/29 00:26

很多时候,系统自带的导航不能满足我们的项目需求,我们需要自定,来满足需求。
主要代码:
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

效果图
这里写图片描述

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 婴儿多大可以竖着抱 宝宝多大可以竖着抱 婴儿多久可以竖着抱 宝宝多久可以竖着抱 婴儿多久可以竖抱 宝宝多久可以竖抱 3个月宝宝可以竖抱吗 婴儿三个月能竖着抱吗 三个月宝宝能竖抱吗 宝宝几个月能竖着抱 两个月宝宝可以竖着抱吗 两个月宝宝可以竖抱吗 几个月宝宝可以竖抱 几个月宝宝可以竖着抱 三个月宝宝可以竖着抱吗 三个月宝宝可以竖抱吗 新生儿几个月可以竖着抱 婴儿几个月可以竖着抱 婴儿什么时候可以竖着抱 婴儿什么时候可以竖抱 新生儿几个月可以竖抱 宝宝三个月可以竖着抱吗 小孩几个月可以竖着抱 宝宝什么时候可以竖着抱 宝宝什么时候可以竖抱 孩子几个月可以竖着抱 小孩几个月可以竖抱 孩子几个月可以竖抱 宝宝几个月才能竖着抱 三个月的宝宝可以竖着抱吗 两个多月的宝宝可以竖着抱吗 宝宝什么时候能竖着抱 两个月的宝宝可以竖着抱吗 几个月的宝宝可以竖着抱 几个月婴儿可以竖着抱 婴儿三个月可以竖抱吗 2个月的宝宝可以竖着抱吗 小宝宝几个月可以竖着抱 婴儿两个月可以竖抱吗 婴幼儿几个月可以竖着抱 一个月的宝宝可以竖着抱吗