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

iOS5:定制导航栏

2012-12-18 15:49 78 查看

原文:http://www.altinkonline.nl/tutorials/xcode/uinavigationbar/customizing-background-tintcolor-and-selected-image/

想定制导航栏吗?从iOS5开始你就可以改变导航栏的背景图片、tintcolor或者标题文本。
这里我们将介绍如何在Xcode中定制导航栏。



从这里你可以下载到本文用到的按钮: Buttons.zip
首先需要获取UINavigationController引用。你可以在创建UINavigationController时定制导航栏,也可以在ViewController加载到UINavigationController时定制导航栏。
打开要引用UINavigationController的类,在viewDidLoad方法中定制导航栏的背景图片,如下列代码所示:

UIImage *navbarPortrait= [[UIImageimageNamed:@"navbar_44.png"]

resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

UIImage *navbarLandscape =[[UIImage imageNamed:@"navbar_32.png"]

resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
// Setbackgroundimage for all navigationcontrollers

[[UINavigationBar appearance] setBackgroundImage:navbarPortrait

forBarMetrics:UIBarMetricsDefault];
[[UINavigationBarappearance] setBackgroundImage:navbarLandscape

forBarMetrics:UIBarMetricsLandscapePhone];


导航栏的背景图片必须是下列高度:
竖屏:44px(retina 屏则为 88px)
横屏:32px (retina 屏则为 64px)

BarButton(包括backButton)的背景图片用backgroundimage属性设置。BarButton的背景图片必须是下列高度:
竖屏:30px(retina 屏则为 60px)
横屏:24px(retina 屏则为 48px)

如果标题文本过长(超过按钮图片的宽度)图片将被缩放。

// Setbackgroundimage for all buttons
UIImage *buttonPortait= [[UIImageimageNamed:@"button_30.png"]

resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)];

UIImage *buttonLandscape =[[UIImage imageNamed:@"button_24.png"]

resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)];

[[UIBarButtonItem appearance] setBackgroundImage:buttonPortait

forState:UIControlStateNormal

barMetrics:UIBarMetricsDefault];
[[UIBarButtonItemappearance] setBackgroundImage:buttonLandscape

forState:UIControlStateNormal

barMetrics:UIBarMetricsLandscapePhone];
// Setbackgroundimage for all backbuttons
UIImage *backButtonPortait= [[UIImageimageNamed:@"back_button_30.png"]

resizableImageWithCapInsets:UIEdgeInsetsMake(0, 13, 0, 5)];

UIImage *backButtonLandscape =[[UIImage imageNamed:@"back_button_24.png"]

resizableImageWithCapInsets:UIEdgeInsetsMake(0, 10, 0, 4)];

[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonPortait

forState:UIControlStateNormal

barMetrics:UIBarMetricsDefault];
[[UIBarButtonItemappearance] setBackButtonBackgroundImage:backButtonLandscape

forState:UIControlStateNormal

barMetrics:UIBarMetricsLandscapePhone];

还可以通过titleTextAttriutes属性定制UIBarButton。setTitleTextAttributes:方法用一个NSDictionary参数指定所需选项:
UITextAttributeTextColor,定制文字颜色。
UITextAttributeTextShadowColor,定制文字的阴影色。
UITextAttributeTextShadowOffset,定制阴影的偏移位置。
UITextAttributeFont,定制文本字体。

// Settitletext attributes
NSMutableDictionary *attributes= [[NSMutableDictionary alloc] init];

[attributessetValue:[UIColor blackColor] forKey:UITextAttributeTextColor];
[attributessetValue:[UIColor whiteColor] forKey:UITextAttributeTextShadowColor];
[attributessetValue:[NSValue valueWithUIOffset:UIOffsetMake(0, 1)] forKey:UITextAttributeTextShadowOffset];
[attributessetValue:[UIFont fontWithName:@"Verdana"size:0.0] forKey:UITextAttributeFont];
[[UIBarButtonItemappearance] setTitleTextAttributes:attributes forState:UIControlStateNormal];
[attributesrelease];

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