通用App中自定义UINavigationbar
我有一个通用应用程序,我可以在其中自定义 UINavigationBar。 在我的 iPhone AppDelegate 中,我使用它来实现它:
@implementation UINavigationBar (CustomImage)
static NSMutableDictionary *navigationBarImages = NULL;
- (void)initImageDictionary
{
if(navigationBarImages==NULL){
navigationBarImages=[[NSMutableDictionary alloc] init];
}
}
- (void)drawRect:(CGRect)rect
{
NSLog(@"drawing navbar2");
UIImage *imageName=[navigationBarImages objectForKey:[NSValue valueWithNonretainedObject: self]];
if (imageName==nil) {
imageName=[UIImage imageNamed:@"bg_titleBar.png"];
UIImage *image = imageName;
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
}
- (void)setMyImage:(UIImage*)image
{
[navigationBarImages setObject:image forKey:[NSValue valueWithNonretainedObject: self]];
[self setNeedsDisplay];
}
@end
现在我的问题是:尽管我启动了 iPad 模拟器,为什么会调用此代码?
更重要的是,它似乎破坏了 UIPopoverController,因为它看起来像这样:
http://awesome-apps .com/pic/ok.png
虽然它应该看起来像这样:
http: //awesome-apps.com/pic/nok.png
此外,它会损坏我的应用程序中的更多内容,但这应该是初学者的:)
任何人都可以帮助我吗?你有过类似的经历吗?
I have a Universal App in which I customize my UINavigationBar.
In my iPhone AppDelegate I use this to achieve it:
@implementation UINavigationBar (CustomImage)
static NSMutableDictionary *navigationBarImages = NULL;
- (void)initImageDictionary
{
if(navigationBarImages==NULL){
navigationBarImages=[[NSMutableDictionary alloc] init];
}
}
- (void)drawRect:(CGRect)rect
{
NSLog(@"drawing navbar2");
UIImage *imageName=[navigationBarImages objectForKey:[NSValue valueWithNonretainedObject: self]];
if (imageName==nil) {
imageName=[UIImage imageNamed:@"bg_titleBar.png"];
UIImage *image = imageName;
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
}
- (void)setMyImage:(UIImage*)image
{
[navigationBarImages setObject:image forKey:[NSValue valueWithNonretainedObject: self]];
[self setNeedsDisplay];
}
@end
Now my questions: why does this code get called, although I start the iPad simulator?
And more important it seems to corrupt the UIPopoverController because it looks like this:
http://awesome-apps.com/pic/ok.png
While it should look somehow like this:
http://awesome-apps.com/pic/nok.png
Besides it corrupts more in my App, but this should be it for starters :)
Can anyone help me with this? Have you ever had a similar experience?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,忽略当您在 iPad 模拟器中运行时它会被调用的事实,因为正如您稍后会看到的,即使您使用两个不同的类别(一个用于 iPhone,一个用于 iPad),您'仍然有这个问题。
原因如下:
您正在使用类别来覆盖 UINavigationBar 行为。我假设您知道这意味着什么 - 至关重要的是应用程序中的所有导航栏都将使用您在该类别中提供的方法。
如果您使用使用 UINavigationBars 的标准苹果元素,这可能会导致问题 - 这正是您在弹出窗口控制器中看到的东西。发生的情况是 UIPopoverController 使用 UINavigationBar。但因为您已经定义了一个类别,所以应用程序假定您希望弹出导航栏也使用该类别。。
这就是为什么你会在弹出控制器中看到奇怪的行为。
只要您使用类别,您就会遇到这个问题,因为您无法有选择地告诉系统哪些酒吧应该使用您的类别。
我建议您准确地告诉我们您正在尝试在导航栏中进行自定义,因为还有其他方法可以在类别之外实现自定义。
So ignore the fact that it gets called when you run in the iPad simulator, because as you'll see in a minute even if you used two different categories (one for iPhone, one for iPad) you'd still have this problem.
Here's why:
You are using a category to override the UINavigationBar behaviour. I assume you know what that means - crucially any and all navigation bars in your app will use your supplied methods in the category.
This can cause problems if you're using standard apple elements that use UINavigationBars - the exact thing you're seeing in the popover controller. What's happening is the UIPopoverController uses a UINavigationBar. But because you've defined a category, the app assumes you want the popover navbar to use that category as well.
So that's why you're seeing your weird behaviour in your pop-over controller.
As long as you use categories you'll have this problem, because you can't selectively tell the system which bars should use your category.
I'd suggest you tell us exactly you're trying to customise in the navbar, because there are other ways to achieve customisation outside of categories.