给UINavigationBar 和 UIToolbar的背景绘图的讨论

APP开发技术QQ群:347072638


iOS5.0以后 直接调用新的API

- (void)setBackgroundImage:(UIImage *)backgroundImage forBarMetrics:(UIBarMetrics)barMetrics __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0) UI_APPEARANCE_SELECTOR;
- (UIImage *)backgroundImageForBarMetrics:(UIBarMetrics)barMetrics __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0) UI_APPEARANCE_SELECTOR;

iOS5.0以前
@implementation UINavigationBar (CustomImage)

-(void) drawRect:(CGRect)rect {
    NSString *pngName;
    if (getDeviceType() == 3) {
        pngName = @"img/new_basic.png";
    }else{
        pngName = @"sw-header-top.png";
    }
    UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%@/%@", g_SkinPathStr,pngName]];
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}

@end

@implementation UIToolbar (CustomImage)

-(void) drawRect:(CGRect)rect {
    NSString *pngName;
    if (getDeviceType() == 3) {
        pngName = @"img/new_basic.png";
    }else{
        pngName = @"sw-header-bot.png";
    }
    self.translucent = YES;
    UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%@/%@", g_SkinPathStr,pngName]];
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end

这里是重写了UINavigationBar和UIToolbar 的drawRect方法,实际上drawRect方法之后需要手动setNeedsDisplay才开始重绘窗口,
一般情况下,setFrame系统也会调用setNeedsDisplay。

setNeedsDisplay
Marks the receiver’s entire bounds rectangle as needing to be redrawn.
- (void)setNeedsDisplay
Discussion
You can use this method or the setNeedsDisplayInRect: to notify the system that your view’s contents need to be redrawn. This method makes a note of the request and returns immediately. The view is not actually redrawn until the next drawing cycle, at which point all invalidated views are updated.
Note: If your view is backed by a CAEAGLLayer object, this method has no effect. It is intended for use only with views that use native drawing technologies (such as UIKit and Core Graphics) to render their content.

You should use this method to request that a view be redrawn only when the content or appearance of the view change. If you simply change the geometry of the view, the view is typically not redrawn. Instead, its existing content is adjusted based on the value in the view’s contentMode property. Redisplaying the existing content improves performance by avoiding the need to redraw content that has not changed.
Availability
        Available in iOS 2.0 and later.


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值