您的位置:首页 > 移动开发 > IOS开发

iOS自定义导航栏形状

2016-03-16 10:02 183 查看
很多时候,系统自带的导航不能满足我们的项目需求,我们需要自定,来满足需求。

主要代码:

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


效果图

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: