带有纯色 iOS 5 的 UINavigationBar

发布于 2024-12-17 17:34:42 字数 438 浏览 0 评论 0原文

我正在使用类别来自定义导航栏。我的代码是:

- (void) drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColor(context, CGColorGetComponents([self.tintColor CGColor]));
    CGContextFillRect(context, rect);
}

它运行良好,但不适用于 iOS 5。我需要导航栏颜色为纯色,没有任何渐变。我该怎么做?

据我所知,对于iOS 5,替换drawRect方法的唯一方法是创建一个子类,但是有没有办法让所有导航控制器都使用UINavigationBar子类而不是原来的班级?

I'm using the category to customize nav bar. My code is:

- (void) drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColor(context, CGColorGetComponents([self.tintColor CGColor]));
    CGContextFillRect(context, rect);
}

It works well, but not in iOS 5. I need nav bar color to be solid without any gradient. How should I do this?

As I know, for iOS 5 the only way to replace drawRect method is to make a subclass, but is there any way to make all navigation controllers to use UINavigationBar subclass instead of original class?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

因为看清所以看轻 2024-12-24 17:34:42

在 iOS 5 中,您可以使用 UIAppearance 协议来设置应用程序中所有 UINavigationBar 的外观。参考:链接。

我获得纯色的方法是为导航栏创建自定义图像,并将其设置为 UINavigationBars 背景。您可能必须创建与 UINavigationBar 大小相同的图像,至少我是这样做的。

在您的应用程序委托中,执行如下操作:

UIImage *image = [UIImage imageNamed:@"navbarbg.png"];
[[UINavigationBar appearance] setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];

您还必须将 UIAppearanceContainer 协议添加到头文件中:

@interface AppDelegate : UIResponder <UIApplicationDelegate, UIAppearanceContainer>

@end

您可能可以通过设置某种颜色或其他内容(而不是图像)来实现相同的操作。但我发现这是一个简单的解决方案。

In iOS 5 you can use the UIAppearance protocol to set the appearance of all UINavigationBars in your app. Reference: link.

The way I got a solid color was to create a custom image for the navigation bar, and set it as UINavigationBars background. You might have to create the image with the same size as the UINavigationBar, at least that's what I did.

In your app delegate, do something like this:

UIImage *image = [UIImage imageNamed:@"navbarbg.png"];
[[UINavigationBar appearance] setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];

You also have to add the UIAppearanceContainer protocol to the header file:

@interface AppDelegate : UIResponder <UIApplicationDelegate, UIAppearanceContainer>

@end

You can probably achieve the same thing by setting some color or something, instead of an image. But I found this to be an easy solution.

红颜悴 2024-12-24 17:34:42

这涵盖了 iOS 5 和 iOS 4.3 及更早版本

https://gist.github.com/1774444

This covers both iOS 5 and iOS 4.3 and earlier

https://gist.github.com/1774444

双手揣兜 2024-12-24 17:34:42

这更像是一种黑客行为,并且一直对我有用。
这需要位于 AppDelegate 文件中。

//Create  a size struct.
CGSize size = CGSizeMake(768, 50);

//CReate a imagecontext from a non-existing image. You can use a existing image as well, in that case the color will be the color of the image. 
UIImage *backgroundImage = [UIImage imageNamed:@"nonexist.png"];

UIGraphicsBeginImageContext(size);

[backgroundImage drawInRect:CGRectMake(0,0,size.width,size.height)];

UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

self.navigationController = [[UINavigationController alloc]initWithRootViewController:self.viewController];

将背景颜色设置为您选择的颜色。

[self.navigationController.view setBackgroundColor:[UIColor blackColor]];

self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];

//Most important line.
[navigationController.navigationBar setBackgroundImage: newImage forBarMetrics:UIBarMetricsDefault];

This is more of a hack and has consistently worked for me.
This needs to be in the AppDelegate file.

//Create  a size struct.
CGSize size = CGSizeMake(768, 50);

//CReate a imagecontext from a non-existing image. You can use a existing image as well, in that case the color will be the color of the image. 
UIImage *backgroundImage = [UIImage imageNamed:@"nonexist.png"];

UIGraphicsBeginImageContext(size);

[backgroundImage drawInRect:CGRectMake(0,0,size.width,size.height)];

UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

self.navigationController = [[UINavigationController alloc]initWithRootViewController:self.viewController];

Set the background color to be the color of your choice.

[self.navigationController.view setBackgroundColor:[UIColor blackColor]];

self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];

//Most important line.
[navigationController.navigationBar setBackgroundImage: newImage forBarMetrics:UIBarMetricsDefault];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文