http://mobile.51cto.com/hot-313145_2.htm
在iOS5推出之前,要实现标准界面的定制设计,对于开发者来说可没有那么简单。尽管开发者可以通过重写drawRect是一个不错的办法,但开发者也很头痛。
AD:
定制UIBarButtonItem
打开images,找到button_textured_24.png和button_textured_30.png两个文件,我们将用它们来设置导航栏中的按钮外观。
注意我们需要将按钮图像设置为可调整大小的,因为按钮的宽度取决于其中的文本。
对于这些按钮,我们不需要最左和最右的5个像素也伸缩,所以需要将left和right cap insets设置为5。
在customizeAppearance方法的最后添加以下代码:
- //customize the apperance for UIBarButtonItems
- UIImage *button30 = [[UIImageimageNamed:@"button_textured_30"] r
- esizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)];
- UIImage *button24 = [[UIImageimageNamed:@"button_textured_24"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)];
- [[UIBarButtonItemappearance] setBackgroundImage:button30forState:UIControlStateNormalbarMetrics:UIBarMetricsDefault];
- [[UIBarButtonItemappearance] setBackgroundImage:button24forState:UIControlStateNormalbarMetrics:UIBarMetricsLandscapePhone];
- [[UIBarButtonItemappearance]setTitleTextAttributes:
- [NSDictionarydictionaryWithObjectsAndKeys:
- [UIColorcolorWithRed:220.0/255.0green:104.0/255.0blue:1.0/255.0alpha:1.0],
- UITextAttributeTextColor,
- [UIColorcolorWithRed:1.0green:1.0blue:1.0alpha:1.0],
- UITextAttributeTextShadowColor,
- [NSValuevalueWithUIOffset:UIOffsetMake(0, 1)],
- UITextAttributeTextShadowOffset,
- [UIFontfontWithName:@"AmericanTypewriter"size:0.0],
- UITextAttributeFont,
- nil]
- forState:UIControlStateNormal];
以上代码其实和定制导航栏的差不多。首先我们还是为按钮创建了可伸缩的图像,并设置为背景图片。然后我们指定了文本的格式。
其中的”back”按钮需要特殊定制,因为它需要看起来与众不同。
让我们在customizeApperance方法的最后添加以下代码来特殊对待back按钮:
- //customize the appeance for "back" on UIBarButtonItems
- UIImage *buttonBack30 = [[UIImageimageNamed:@"button_back_textured_30"]
- resizableImageWithCapInsets:UIEdgeInsetsMake(0, 13, 0, 5)];
- UIImage *buttonBack24 = [[UIImageimageNamed:@"button_back_textured_24"]
- resizableImageWithCapInsets:UIEdgeInsetsMake(0, 12, 0, 5)];
- [[UIBarButtonItemappearance]setBackButtonBackgroundImage:buttonBack30forState:
- UIControlStateNormalbarMetrics:UIBarMetricsDefault];
- [[UIBarButtonItemappearance]setBackButtonBackgroundImage:buttonBack24forState:
- UIControlStateNormalbarMetrics:UIBarMetricsLandscapePhone];
需要注意的是,我们为back按钮设置了不同的cap inset值。同时,UIBarButtonItem还有一个专门的backButtonBackgroundImage属性可以使用。
编译运行,可以看到下图: