带有自定义背景图像的 UINavigationbar
我有一个自定义类别,可以根据我所在的视图为我的导航栏定义特殊的背景图像。
在类别中,我使用设置图像。
- (void)drawRect:(CGRect)rect
事情是,当在导航控制器中浏览我的视图时,自定义图像会消失,留下320x44(条的大小)的空白区域,直到下一个控制器重新出现 - 即大约 1 秒后。
这种看起来很难看,我想知道是否有更好的方法来做到这一点,所以我有一个干净的实现,看起来也不错并且符合我的要求。
提前致谢。
类别代码:
- (void)initImageDictionary
{
if(navigationBarImages==NULL){
navigationBarImages=[[NSMutableDictionary alloc] init];
}
}
- (void)drawRect:(CGRect)rect
{
UIImage *imageName = [navigationBarImages objectForKey:[NSValue valueWithNonretainedObject: self]];
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];
}
然后我调用:
[self.navigationBar performSelectorInBackground:@selector(setMyImage:) withObject:[UIImage imageNamed:imageTopbar]];
I have a custom category to define a special background image for my navigationbar depending on the view I'm in.
Within the category I set the image using
- (void)drawRect:(CGRect)rect
The thing is, when navigating through my views in the Navigation Controller, the custom image disappears leaving a blank white space 320x44(size of the bar) until the next controller reappers - i.e. after about 1 second.
This sort of looks ugly and I was wondering whether there was a better way to do this so I have a clean implementation which looks nice aswell and fits my demands.
Thanks in advance.
Category Code:
- (void)initImageDictionary
{
if(navigationBarImages==NULL){
navigationBarImages=[[NSMutableDictionary alloc] init];
}
}
- (void)drawRect:(CGRect)rect
{
UIImage *imageName = [navigationBarImages objectForKey:[NSValue valueWithNonretainedObject: self]];
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];
}
Then I call:
[self.navigationBar performSelectorInBackground:@selector(setMyImage:) withObject:[UIImage imageNamed:imageTopbar]];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该仅在主线程上使用 UIKit。
You should use UIKit only on the main thread.
在您的 ViewController 中,尝试以下操作,其中 @"BGimage.png" 是您的图像文件:
//puts an image as bg pattern(tiles) for a View
//self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"BGimage.png"]];
In your ViewController, try this where @"BGimage.png" is your image file:
//puts an image as bg pattern(tiles) for a View
//self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"BGimage.png"]];