重写 UINavigationBar drawRect 函数不起作用
我有一个带有导航栏控制器的简单应用程序,我想在覆盖 drawRect 函数时更改其导航栏。我在这里的很多地方都读到,我所需要做的就是将下面的代码粘贴到我的 appDelegate 上方。可悲的是,它似乎对我没有任何作用。我首先尝试更改导航栏的颜色,然后再尝试向其添加图像,但同样,什么也没有发生。我在这里缺少什么
我厌倦了在 AppDelegate 上方插入此代码:
@implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect {
UIColor *color = [UIColor redColor];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColor(context, CGColorGetComponents( [color CGColor]));
CGContextFillRect(context, rect);
}
@end
并带有背景图像:
@implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect {
// Drawing code
UIImage *img = [UIImage imageNamed: @"13.png"];
[img drawInRect:CGRectMake(0, 0, 320, self.frame.size.height)];
}
@end
感谢您的帮助!
I have a simple app with a nav bar controller and I would like to change its navigation bar while overriding the drawRect function. I read in many places here that all I need to do is just to paste the code below above my appDelegate. Sadly, it doesnt seem to do anything for me. I tried as a start to just have the color of the nav bar changed, before i try to add an image to it, but again, nothing happens. What am i missing here
I tired to insert this code above the AppDelegate:
@implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect {
UIColor *color = [UIColor redColor];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColor(context, CGColorGetComponents( [color CGColor]));
CGContextFillRect(context, rect);
}
@end
And with the background image:
@implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect {
// Drawing code
UIImage *img = [UIImage imageNamed: @"13.png"];
[img drawInRect:CGRectMake(0, 0, 320, self.frame.size.height)];
}
@end
Thanks for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此代码在
UINavigationBar
上声明一个类别,并重新定义其drawRect:
方法的行为。尝试将代码片段(第二个)放在应用程序委托的.m
文件的末尾。尽管第一个应该可行,但另一个更干净的选择是将类别写入将包含在应用程序委托中的单独文件中。
还要确保您正在加载的图片存在于应用程序包中。
This code declares a category on
UINavigationBar
and redefines the behaviour of itsdrawRect:
method. Try to put the code snippet (second one) at the end of the.m
file of the app delegate.Although the first should work, another more clean option is to write the category in a separate file you will include in the app delegate.
Also make sure the picture you are loading exists in the app bundle.
好的 - 找到解决方案!这似乎在 iPhone 5.0 模拟器上不起作用!
Ok - solution found! This doesnt seem to work on the iPhone 5.0 simulator!