How to make UIToolbar have a Clear Background?

前端 未结 8 1830
渐次进展
渐次进展 2021-02-05 12:54

I have a UIToolbar that has a white tint, with a bar button item, followed by some flexible space, followed by another bar button item. I would like to make the toolbar complete

相关标签:
8条回答
  • 2021-02-05 13:46

    There is a solution in @ievgen answer in Swift 5.1

    toolbar.setBackgroundImage(UIImage(), forToolbarPosition: .any, barMetrics: .default)
    toolbar.backgroundColor = .clear
    

    If you want to clear the separator gray line

    toolbar.setShadowImage(UIImage(), forToolbarPosition: .any)
    
    0 讨论(0)
  • 2021-02-05 13:49

    It can be done without subclassing in iOS 6+ with setting the property translucent to `YES.

    This will not work in iOS 5 in below. Here's how it can be done without subclassing toolbar:

    const float colorMask[6] = {222, 255, 222, 255, 222, 255};
    UIImage *img = [[UIImage alloc] init];
    UIImage *maskedImage = [UIImage imageWithCGImage: CGImageCreateWithMaskingColors(img.CGImage, colorMask)];
    
    [self.toolbar setBackgroundImage:maskedImage forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
    
    0 讨论(0)
提交回复
热议问题