将 UIToolBar 子类化为具有自定义背景时,工具栏的下半部分是黑色的? [英] When subclassing UIToolBar to have a custom background the bottom half of toolbar is black?

查看:29
本文介绍了将 UIToolBar 子类化为具有自定义背景时,工具栏的下半部分是黑色的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在继承 UIToolBar,这里是我如何覆盖 UIToolBar 的 drawRect 方法:

I'm subclassing UIToolBar, here is how I override the drawRect method of UIToolBar:

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

该应用程序使用 UINavigationController 范式,该范式使用 initWithNavigationBarClass 方法初始化.

The app uses a UINavigationController paradigm initialized with initWithNavigationBarClass method.

问题是工具栏的下半部分是黑色的?UIToolBar_Background.png 的高度为 44 像素(或视网膜为 88).它不应该有它的下半部分黑色.

The issue is that the bottom half of toolbar is black? The UIToolBar_Background.png is 44 pixels height (or 88 for retina). It should not have it's bottom half black.

推荐答案

通过继承 UIToolBar 并覆盖 drawRect,您可以消除一些 UIToolBar 自己的绘图.为什么不使用外观api来设置背景图片:

By subclassing UIToolBar and overriding drawRect, you eliminate some of UIToolBar's own drawing. Why not use the appearance api to set a background image:

[[UIToolbar appearance] setBackgroundImage:[UIImage imageNamed:@"UIToolBar_Background.png"]
                        forToolbarPosition:UIToolbarPositionBottom
                                barMetrics:UIBarMetricsDefault];

或者,您可以使用子类化路线,只需确保在进行自己的绘图之前调用 [super drawrect:rect]:

alternatively, you could use the subclassing route, just make sure you call [super drawrect:rect] before doing your own drawing:

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

这篇关于将 UIToolBar 子类化为具有自定义背景时,工具栏的下半部分是黑色的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆