tes*_*ing 7 iphone xcode objective-c
有一种简单的方法可以在视图顶部更改导航项的背景颜色吗?我有一个基于导航的应用程序,我只希望一个视图获得另一种背景颜色.我主要用IB创建了视图.我找到了以下解决方案(未经测试):
float r=10;
float g=55;
float b=130;
float a=0;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 120, 30)];
[label setBackgroundColor:[UIColor colorWithRed:r/255.f
green:g/255.f
blue:b/255.f
alpha:a/255.f];];
[self.navigationController.navigationBar.topItem setTitleView:label];
[label release];
Run Code Online (Sandbox Code Playgroud)
是否有更简单的方式
[navigationController.navigationBar setBackgroundColor:blueColor]
Run Code Online (Sandbox Code Playgroud)
或者我可以使用IB(但在每个视图中没有相同的颜色)吗?
提前谢谢了!
干杯
Jos*_*hua 18
你可以使用:
[navigationController.navigationBar setTintColor:[UIColor redColor]; //Red as an example.
Run Code Online (Sandbox Code Playgroud)
这会将导航栏的颜色和所有它的按钮着色为特定颜色,在本例中为红色.也可以在Interface Builder中设置此属性.
如果您想进一步自定义它,可以通过UINavigationBar对图像进行子类化来设置图像的背景.像这样......
头文件.
#import <UIKit/UIKit.h>
@interface UINavigationBar (CustomImage)
@end
Run Code Online (Sandbox Code Playgroud)
实现文件.
#import "CustomNavigationBar.h"
@implementation UINavigationBar (CustomImage)
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
if([self isMemberOfClass: [UINavigationBar class]]){
UIImage *image = [UIImage imageNamed:@"bar.png"];
CGContextClip(ctx);
CGContextTranslateCTM(ctx, 0, image.size.height);
CGContextScaleCTM(ctx, 1.0, -1.0);
CGContextDrawImage(ctx, CGRectMake(0, 0, self.frame.size.width, self.frame.size.height), image.CGImage);
}else{
[super drawLayer:layer inContext:ctx];
}
}
@end
Run Code Online (Sandbox Code Playgroud)
然后在Interface Builder中将您的类设置UINavigationBar为(在本例中)CustomNavigationBarIdentity标签下.
| 归档时间: |
|
| 查看次数: |
33289 次 |
| 最近记录: |