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
- iOS自定义导航栏形状
- iOS自定义导航栏
- iOS自定义导航栏
- iOS 自定义导航栏
- IOS 自定义导航栏背景
- ios开发---自定义导航栏
- ios学习(自定义导航栏)
- 【iOS】自定义导航栏(一)
- 【iOS】自定义导航栏(二)
- iOS自定义导航栏titleView
- iOS 自定义导航栏 NavigationBar
- iOS 自定义导航栏笔记
- [iOS 自定义导航栏/导航条]隐藏系统导航条
- iOS自定义形状的Button
- IOS 自定义导航栏和标签栏
- iOS自定义导航栏-导航栏联动(一)
- iOS自定义导航栏-导航栏联动(二)
- ios 自定义导航栏背景图片和颜色
- sensor_g_control & sensor_s_control
- struts2拦截器
- 用泛型实现参数化类型(泛型的深化与提高)
- Java并发编程:Thread类的使用
- Hadoop-创建Hadoop并添加用户(CentOS)
- iOS自定义导航栏形状
- 关于发布App时遇到的坑(一)
- 蓝桥杯-- 历届试题 剪格子(dfs)
- android的actionbar
- 使用Source insight时键入字符时会覆盖后面的字符,如何取消这个功能?
- HDU Fibonacci Again
- Android类汇总之---------起始篇
- 解决:ubuntu下eclipse的maven项目报错:Maven Dependencies
- 利用CXF框架开发WebService