现在的位置: 首页 > 综合 > 正文

兼容ios4的自定义NavigationBar的背景

2019年10月01日 ⁄ 综合 ⁄ 共 480字 ⁄ 字号 评论关闭

在AppDelegate.m的文件中加入如下定义,覆盖NavigationBar的drawRect函数:

@interface UINavigationBar (CustomBackgroud)

- (void)drawRect:(CGRect)rect;

@end

@implementation UINavigationBar (CustomBackgroud)

// iOS 4.* 下替换导航条皮肤
- (void)drawRect:(CGRect)rect
{
    UIImage *navBarImg = [[UIImage imageNamed:@"nav_bar"] stretchableImageWithLeftCapWidth:1 topCapHeight:0.0];
    [navBarImg drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}

@end

其中 nav_bar 是资源文件,这里使用objective-c的拉伸图片的函数来确定其拉伸状况。这样整个软件的导航栏都是以该nav_bar为背景。

抱歉!评论已关闭.