定制导航栏
来源:互联网 发布:淘宝客单纯做3c产品 编辑:程序博客网 时间:2024/05/22 03:17
想定制导航栏吗?从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];
完。
- iOS5:定制导航栏
- 定制导航栏
- 定制导航栏NavigationBar
- 定制导航栏
- iOS 导航栏定制
- 定制导航栏和状态栏
- 定制boostrap的导航栏
- 定制导航栏和状态栏
- 定制导航栏的返回按钮
- 定制导航栏的返回按钮
- Android定制属于你自己的导航栏
- Android定制属于你自己的导航栏
- Android定制属于你自己的导航栏
- 定制导航栏的返回按钮
- Android定制属于你自己的导航栏
- iOS中定制导航栏背景
- 定制导航栏的返回按钮
- iOS中定制导航栏背景
- HDU 2371 用矩阵置换字符
- 制作简易计算器
- centos下oracle11g精简版客户端32位安装(简单5步)(64位下换成相应的64位)
- Window/Dialog style could not be loaded.
- curl获取响应时间
- 定制导航栏
- Mac OS X中配置Apache
- I、B、P帧
- BPlog通过ttySMD透传到PC遇到的问题及分析
- ActvSearchAdapter 自定义AutoCompleteTextView匹配(汉字和拼音首字母)
- Fragment和Activity的生命周期
- 模式识别 - 多视角学习 (Multi-View Learning) 简介
- 如何在maven工程中加载oracle驱动
- Oracle 11g RAC+DG项目实战-视频分享