自定义导航栏按钮与系统图标和图像

时间:2011-02-19 06:22:07

标签: iphone button navigationbar

我正在尝试弄清楚如何将自定义背景图像添加到使用系统图标或图像图标的NavigationBar按钮。

我已经想出如何使用文本向NavigationBar按钮添加背景图像,但我很难弄清楚如何使用图标按钮完成此操作。

对于我遵循本教程的文本按钮: http://idevrecipes.com/2010/12/13/wooduinavigation/

如何创建带图标的自定义按钮?

谢谢!

1 个答案:

答案 0 :(得分:0)

你可能会得到这方面的帮助

 @implementation UINavigationBar (UINavigationBarCategory)
  -(void)setBackgroundImage:(UIImage*)image{
if(!image) return;
UIImageView *aTabBarBackground = [[UIImageView alloc]initWithImage:image];
//  aTabBarBackground.frame = CGRectMake(0,0,self.frame.size.width,self.frame.size.height);
aTabBarBackground.frame = CGRectMake(0,0,self.frame.size.width * .6,self.frame.size.height);

// [self addSubview:aTabBarBackground];
// [self sendSubviewToBack:aTabBarBackground];
[self insertSubview: aTabBarBackground atIndex: 0];
[aTabBarBackground release];
   }
   @end

它适用于我

我们还有另一个想法。

  @implementation UINavigationBar (UINavigationBarCategory)

 - (void)drawRect:(CGRect)rect {
// Drawing code 
UIImage *img = [UIImage imageNamed: @"navbar_background.png"];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawImage(context, CGRectMake(0, 0, 320, self.frame.size.height), img.CGImage);

}    @end