UINAV,UITOOLBAR,UISEARCHBAR,UITABBAR修改背景图片

这些都是5.0以前版本使用,

5.0tabBar可以直接改了,其他的还没研究,这里先做记录

#ifndef IOS_VERSION  
#define IOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]  
#endif  

    if (IOS_VERSION >= 5.0f)
    {
        tabBarController.tabBar.backgroundImage = [UIImage imageNamed:@"bar_bottom"];
    }


这里运用了 object-c里面的一个类别,大体意思就是在不知道苹果封装起来的API内容的情况下,在外部程序中覆盖其原来的函数。大体这个意思吧。

程序代码: 在程序的任何一个 .m文件 后面加上下面代码即可 记住 要在 @end 的后面加上;

@implementation UINavigationBar (CustomImage2)   
- (void)drawRect:(CGRect)rect {   
    UIImage *image = [UIImage imageNamed: @"bar.png"];   
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];   
}   
@end

@implementation UIToolbar (CustomImage2)   
- (void)drawRect:(CGRect)rect {   
    UIImage *image = [UIImage imageNamed: @"bar.png"];   
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];   
}   
@end

@implementation UITabBar (CustomImage2)   
- (void)drawRect:(CGRect)rect {   
    UIImage *image = [UIImage imageNamed: @"bar.png"];   
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];   
}

我还是喜欢用这个

- (void)drawRect:(CGRect)rect
{   
    CGRect bounds=[self bounds];
    UIImage *image = [UIImage imageNamed:@"bar_bottom.png"];   
    //[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    [image drawInRect:bounds];
}   

@end


上面这三个的用法都是一样的,关键是SearchBar

SearchBar的背景图片控制不是由SearchBar 本身 而是由 UISearchBarBack 来控制的,因此无法直接用上面的代码,需要手动把SearchBar上面的两个View 删除了才行。

且看删除代码:

    在你的ViewDidLoad 或者任何一个程序可以执行到的地方 写下如下代码,不过首先你得nib 一个 UISearchbar 才行的

    [[_searchBar.subviews objectAtIndex:0] setHidden:YES];
    [[_searchBar.subviews objectAtIndex:0] removeFromSuperview];
    for (UIView *subview in _searchBar.subviews) {
        if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
            [subview removeFromSuperview];
            break;
        }
    }

这个代码 配合 上面的那个类别

    @implementation UISearchBar (CustomImage2)   
- (void)drawRect:(CGRect)rect {   
    UIImage *image = [UIImage imageNamed: @"bar.png"];   
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];   
}   
@end

OK了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值