When subclassing UIToolBar to have a custom background the bottom half of toolbar is black?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-12 03:56:42

问题


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)];
}

The app uses a UINavigationController paradigm initialized with initWithNavigationBarClass method.

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.


回答1:


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];

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)];
}


来源:https://stackoverflow.com/questions/16215981/when-subclassing-uitoolbar-to-have-a-custom-background-the-bottom-half-of-toolba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!