导航条更改

1,创建并使用一个UINavigationController UINavigationController *aNav = [[UINavigationController alloc] init];然后添加一个视图进去,否则导航栏也没有意义的 UIViewController *aViewCtrl = [[UIView alloc] initWithNibName: (*xib文件名*)];[aNav pushViewController:aViewCtrl animated:NO];//导航栏的第一个视图不要动画化aViewCtrl.title = @"标题"; //设置其标题:2,设置导航栏的左右按钮:设置导航栏的按钮并不是去设置导航栏本身,而是当时被导航的视图控制器,比如我们对aView作设置。//配置一个按钮,我这里是《我的佛典》上的代码UIBarButtonItem *callModalViewButton = [[UIBarButtonItem alloc] initWithTitle:@"经文"                                  style:UIBarButtonItemStyleBordered target:self action:@selector(callModalList)];self.navigationItem.leftBarButtonItem = callModalViewButton;[callModalViewButton release]; //由于本地视图会retain它,所以我们可以release了 可以看到,还是很简单的嘛。3,其他常用方法和属性:本地视图.navigationItem.leftBarButtonItem //左边栏项目本地视图.navigationItem.rightBarButtonItem //右边栏项目本地视图.navigationItem.backBarButtonItem //后退栏项目本地视图.navigationItem.hidesBackButton //隐藏后退按钮(YES or NO)在视图的viewWillAppear:方法中添加:[self.tableView reloadData]; 不起作用,viewWillAppear:这个方法根本没有调用,后来发现原来用了UINavigationController后,viewWillAppear方法是没有效果的,要用UINavigationControllerDelegate的– navigationController:willShowViewController:animated:方法才可以达到这个目的。所以要做到这个,你必须做以下几步:1. 设置代理类nav.delegate = self; 2. 代理类实现UINavigationControllerDelegate Protocol3. 在代理类中添加– navigationController:willShowViewController:animated:方法如:- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{ [self.myTableView reloadData];}pushViewController:viewController animated:BOOL(加载视图控制器)– 添加指定的视图控制器并予以显示,后接:是否动画显示popViewControllerAnimated:BOOL(弹出当前视图控制器)– 弹出并向左显示前一个视图popToViewController:viewController animated:BOOL(弹出到指定视图控制器)– 回到指定视图控制器, 也就是不只弹出一个popToRootViewControllerAnimated:BOOL(弹出到根视图控制器)– 比如说你有一个“Home”键,也许就会实施这个方法了。setNavigationBarHidden:BOOL animated:BOOL(设置导航栏是否显示)– 如果你想隐藏导航栏,这就是地方了。参照Picasa的WebApp样式,现pushViewController:animated:的不同页面转换特效1. 首先要明确的是,不使用pushViewController的默认动画,所以在调用这个函数时,要将animated设置为NO.2. 使用普通的来CATransition实现转换效果,代码如下:CATransition *animation = [CATransition animation];[animation setDuration:0.3];[animation setType: kCATransitionMoveIn];[animation setSubtype: kCATransitionFromTop];[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];[self.navigationController pushViewController:m_poseAddIssueViewController animated:NO];[self.navigationController.view.layer addAnimation:animation forKey:nil];经常要在导航栏中添加各种样式的按钮,添加一个按钮很简单,代码如下图:UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc]initWithTitle:@"Setting"style:UITabBarSystemItemContacts                                                                target:selfaction:@selector(clickSettings:)];          self.navigationItem.rightBarButtonItem =anotherButton; [anotherButton release];其中按钮的样式可以有多种,具体的可以参考:https://developer.apple.com/library/ios/prerelease/#documentation/UIKit/Reference/UIBarButtonItem_Class/在有些项目中要在右面添加两个按钮,实现的样式如下图:image 实现的代码如下图:UIToolbar* tools = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 150, 45)];//使用一个容器去装载多个UIBarButtonItem是实现多个按钮的最核心的部分[tools setTintColor:[self.navigationController.navigationBartintColor]]; [tools setAlpha:[self.navigationController.navigationBaralpha]]; NSMutableArray* buttons = [[NSMutableArray alloc]initWithCapacity:2];UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd                        target:self action:@selector(clickSettings:)];UIBarButtonItem *anotherButton1 = [[UIBarButtonItem alloc]initWithTitle:@"Edit"style:UITabBarSystemItemContacts                                                        target:self action:@selector(clickEdit:)]; [buttons addObject:anotherButton]; [anotherButton release]; [buttons addObject:anotherButton1]; [anotherButton1 release]; [tools setItems:buttons animated:NO]; [buttons release]; UIBarButtonItem *myBtn = [[UIBarButtonItem alloc]initWithCustomView:tools]; self.navigationItem.rightBarButtonItem = myBtn;[myBtn release]; [tools release];隐藏当前页的navigationBar:    [self.navigationController setNavigationBarHidden:YES animated:YES]; 给 UINavigationBar 设置背景图片方法1:+ (UINavigationBar *)createNavigationBarWithBackgroundImage:(UIImage *)backgroundImage title:(NSString *)title {    UINavigationBar *customNavigationBar = [[[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)] autorelease];    UIImageView *navigationBarBackgroundImageView = [[UIImageView alloc] initWithImage:backgroundImage];    [customNavigationBar addSubview:navigationBarBackgroundImageView];    UINavigationItem *navigationTitle = [[UINavigationItem alloc] initWithTitle:title];    [customNavigationBar pushNavigationItem:navigationTitle animated:NO];    [navigationTitle release];    [navigationBarBackgroundImageView release];    return customNavigationBar;}调用的时候:self.navigationController.navigationBar.hidden = YES;    UIImage *navigationBarBackgroundImage =[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"topbar-bg" ofType:@"png"]];    UINavigationBar *customNavigationBar = [YOUR_Util_Class createNavigationBarWithBackgroundImage:navigationBarBackgroundImage title:nil];    [self.view addSubview:customNavigationBar];方法2:利用objective-c的Category语法扩展UINavigationBar 类具体代码为@implementation UINavigationBar(UINavigationBarCategory)- (void)drawRect:(CGRect)rect {    // Drawingcode UIImage *img = [UIImage imageNamed:@"navbar_background.png"];CGPoint point = {0,0};[img drawAtPoint:point];}@end 方法3:@implementation UINavigationBar(UINavigationBarCategory) - (void)drawRect:(CGRect)rect {//加入旋转坐标系代码  // Drawing codeUIImage *navBarImage =[UIImage imageNamed:@"LOGO_320×44.png"];CGContextRef context= UIGraphicsGetCurrentContext();CGContextTranslateCTM(context, 0.0, self.frame.size.height);CGContextScaleCTM(context, 1.0,-1.0);CGPoint center=self.center; CGImageRef cgImage= CGImageCreateWithImageInRect(navBarImage.CGImage,CGRectMake(0, 0, 1, 44));CGContextDrawImage(context, CGRectMake(center.x-160-80, 0, 80,self.frame.size.height),cgImage);CGContextDrawImage(context, CGRectMake(center.x-160, 0, 320,self.frame.size.height),navBarImage.CGImage);CGContextDrawImage(context, CGRectMake(center.x+160, 0, 80,self.frame.size.height),cgImage);}@endold codeCGContextDrawImage(context, CGRectMake(0, 0, self.frame.size.width,self.frame.size.height),navBarImage.CGImage);方法4:有了这个,你还会扩展drawRect自定义导航栏背景吗?查了很多资料,网上的自定义导航栏的方法,清一色的是扩展navigationBar的drawRect方法.然而这样的扩展会影响到工程里所有的navigationBar.或许你并不想这么做,而且很多不合常规的UI用这种方法根本没法实现.做了无数实验,尝试了各种方法,今天跟大家分享一个新方法:#import@interface DDNavigationViewController: UINavigationController{

CALayer *_barBackLayer;

}

@end

@implementation DDNavigationViewController

- (id)initWithRootViewController:(UIViewController *)rootViewController{

self =[super initWithRootViewController:rootViewController];

self.delegate = self;

return self;

}

- (void)loadView{

[super loadView];

UINavigationBar *bar= self.navigationBar;

CALayer*layer= [CALayer layer];

UIImage *navBarImage= [UIImage imageNamed:@"navigationBarBackground.png"];

layer.contents =(id)navBarImage.CGImage;

layer.frame= CGRectMake(0,0, 320,navBarImage.size.height);

[bar.layer insertSublayer:layer atIndex:0];

_barBackLayer =layer;

}

#pragma mark -

#pragma mark UINavigationControllerDelegate

- (void)navigationController:(UINavigationController *)navigationControllerdidShowViewController:(UIViewController *)viewControlleranimated:(BOOL)animated{

[_barBackLayer removeFromSuperlayer];

[navigationController.navigationBar.layer insertSublayer:_barBackLayeratIndex:0];

}

@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 195,980评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,422评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 143,130评论 0 325
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,553评论 1 267
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,408评论 5 358
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,326评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,720评论 3 386
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,373评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,678评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,722评论 2 312
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,486评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,335评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,738评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,009评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,283评论 1 251
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,692评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,893评论 2 335

推荐阅读更多精彩内容

  • //设置尺寸为屏幕尺寸的时候self.window = [[UIWindow alloc] initWithFra...
    LuckTime阅读 787评论 0 0
  • 1,Search Bar 怎样去掉背景的颜色(storyboard里只能设置background颜色,可是发现cl...
    以德扶人阅读 2,293评论 2 50
  • 1.自定义控件 a.继承某个控件 b.重写initWithFrame方法可以设置一些它的属性 c.在layouts...
    圍繞的城阅读 3,326评论 2 4
  • 蓟县农家院,我们又来了,我们喜欢这里种的花草,喜欢这里种的蔬果,更喜欢这里的人实在热情。所以每年夏天都想来这看看您...
    森林羊羊阅读 469评论 0 0
  • 这一周基本上比较放松,没怎么学习。因为好友结婚的关系,所以很多时间都安排在旅程上。 计划安排 这周很自觉准备了手账...
    源源哒阅读 236评论 0 1