iphone - iPhone-尝试制作纯色UIToolbar

标签 iphone ios ipad quartz-2d

我想将我的UIToolbar设为纯灰色。这必须适用于iOS 4.3+

我已经将我的UIToolbar子类化并添加了它

- (void)drawRect:(CGRect)rect {

    UIGraphicsBeginImageContext(rect.size);

    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSaveGState(ctx);

        [[UIColor colorWithRed:64.0f/255.0f
                         green:64.0f/255.0f
                          blue:64.0f/255.0f
                         alpha:1.0f] set];

    CGRect myRect = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    CGContextFillRect(ctx, myRect);

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    CGContextRestoreGState(ctx);
    UIGraphicsEndImageContext();

    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}

没门。工具栏始终为黑色。

有什么线索吗?

谢谢。

最佳答案

您可以尝试使用纯灰色背景图像。您可以使用所需的颜色制作1x1像素的灰色png图像。然后使用以下代码将其设置为背景图片,它将在所有iOS版本(包括4.3及更低版本)上运行。

    UIToolbar *toolBar = //...your toolbar

    UIImage *toolbarBkg = [[UIImage imageNamed: @"toolbarBkg.png"] stretchableImageWithLeftCapWidth:0 topCapHeight:0];

    if ([toolBar respondsToSelector:@selector(setBackgroundImage:forToolbarPosition:barMetrics:)])
        [toolBar setBackgroundImage:toolbarBkg forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
    else {
        UIImageView *background = [[UIImageView alloc] initWithImage:toolbarBkg];
        background.frame = toolBar.frame;
        [toolBar insertSubview:background atIndex:0];
    }

关于iphone - iPhone-尝试制作纯色UIToolbar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13042267/

相关文章:

iOS 和蓝牙...我需要什么框架?

ios - 用户界面 TableView : Change cell height dynamically when clicked the custom button

ipad - 无法让 UISearchBar 范围栏出现在 iPad 上的工具栏(或任何地方)中

ios - 更新后无法打开 xcode 7.2

iphone - 游戏设计 : Checking for object intersection or getting values from accelerometer

ios - [NSDate date] 在多个设备中的不同结果

iphone - UIWebView 的属性 scalesPageToFit = YES 在较大的图像上无法正常工作

iPhone 应用程序升级和移动 xibs/nibs 导致资源过时

ios - 如何使用 pod 获取最新的 GoogleMaps SDK

javascript - 如何使用 jQuery 在 iPad 上避免 "double touch"?