为了账号安全,请及时绑定邮箱和手机立即绑定

自定义UINavigationBar背景

自定义UINavigationBar背景

iOS
鸿蒙传说 2019-10-05 14:02:34
我一直在尝试为整个NavigationBar(不仅是titleView)设置自定义背景,但一直在努力。我找到了这个线程http://discussions.apple.com/thread.jspa?threadID=1649012&tstart=0但是不确定如何实现给出的代码段。该代码是否作为新类实现?另外,UINavigationController由于我有一个使用NavigationView模板构建的应用程序,因此该实例化在哪里,所以按照示例,它不会在我的根控制器中完成
查看完整描述

3 回答

?
LEATH

TA贡献1936条经验 获得超6个赞

您只需要像这样重载drawRect:


@implementation UINavigationBar (CustomImage)

- (void)drawRect:(CGRect)rect {

    UIImage *image = [UIImage imageNamed: @"NavigationBar.png"];

    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];

}

@end


查看完整回答
反对 回复 2019-10-05
?
白衣非少年

TA贡献1155条经验 获得超0个赞

乌达夫和列夫劳是对的。这段代码很好地工作:


@interface CustomNavigationBar : UINavigationBar

@end


@implementation CustomNavigationBar

-(void) drawRect:(CGRect)rect 

{

    UIImage *image = [UIImage imageNamed: @"myNavBarImage"];

    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];

}

@end


// this can go anywhere

+(UINavigationController*) myCustomNavigationController

{

  MyViewController *vc = [[[MyViewController alloc] init] autorelease];

  UINavigationController *nav = [[[NSBundle mainBundle] loadNibNamed:@"CustomNavigationController" owner:self options:nil] objectAtIndex:0];

  nav.viewControllers = [NSArray arrayWithObject:vc];

  return nav;

}

您必须创建CustomNavigationController.xib并将UINavigationController放入其中,然后将navigationBar类更改为“ CustomNavigationBar”。


查看完整回答
反对 回复 2019-10-05
  • 3 回答
  • 0 关注
  • 552 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信