改變導航條樣式

轉載 http://hua397.iteye.com/blog/1181299

1.背景色:

(http://stackoverflow.com/questions/2259929/iphone-navigationbar-custom-background)網上說用category給UINavigationBar重寫drawRect:

@implementation UINavigationBar (CustomImage)

- (void)drawRect:(CGRect)rect {

    UIImage *image = [UIImage imageNamed: @"NavigationBar.png"];

    [image drawInRect:CGRectMake(00self.frame.size.width, self.frame.size.height)];

}

@end

可以用,他自己也說其實不是很好,我覺得不是很靈活


忘了在哪看到了,說是tintColor,經過測試確實如此,setBackgroundColor不行,tintColor就可以:

UINavigationBar *bar = self.navigationController.navigationBar;

bar.tintColor=[UIColor redColor];


而且連left/right button一起改了(前提是item用tittle初始化):

UIBarButtonItem *item=[[UIBarButtonItem allocinitWithTitle:@"ad1.png" style:UIBarButtonItemStyleBordered target:nilaction:nil];

self.navigationItem.rightBarButtonItem=item;

效果:

注意:用Image初始化的item加上去有點問題,會覆蓋大部分bar,不解:

UIBarButtonItem *item=[[UIBarButtonItem allocinitWithImage:[UIImage imageNamed:@"ad1.png"]style:UIBarButtonItemStyleBordered target:nil action:nil];

item.width=50;

self.navigationItem.rightBarButtonItem=item;

效果:

 


另,item用customview初始化的話,如用button,需要設置frame,不然不顯示:

UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];

btn.frame=CGRectMake(005030);

[btn setBackgroundImage:[UIImage imageNamed:@"ad1.png"forState:UIControlStateNormal];

UIBarButtonItem *item=[[UIBarButtonItem allocinitWithCustomView:btn];

self.navigationItem.rightBarButtonItem=item;

效果:

 


2.不止改背景顏色的話,就得用imageView,貌似以前看過,反正就是insert到bar的subviews的0,要不add之後exchange一下,bar原先只有一個subview:

UIImageView *imageView=[[UIImageView allocinitWithFrame:bar.bounds];

imageView.image=[UIImage imageNamed:@"adBg.png"];

[bar insertSubview:imageView atIndex:0];

[imageView release];

效果:

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章