UIToolBar 的 alpha 值减小,希望 UIBarButtonItem 的 alpha 值为 1

发布于 2024-10-17 06:52:15 字数 141 浏览 5 评论 0原文

我有一个 UIToolbar,其 alpha 设置为 0.6。看起来很棒 - 但它也给按钮留下了 0.6 的 alpha,这还可以,但并不是真正理想的。有没有办法独立设置 UIBarButtonItem 的 alpha?我怎样才能让它们看起来是白色的而不是稍微变灰的?

I have a UIToolbar with the alpha set to .6. Looks great - but it leaves the buttons with an alpha of .6 as well, which is okay but not really desirable. Is there some way to independently set the alpha of a UIBarButtonItem? How would I get these to look white instead of slightly grayed out?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

机场等船 2024-10-24 06:52:15

当您在视图上设置 Alpha 时,所有子视图都会使用相同的 Alpha 值进行合成。您可以做的不是在视图本身上设置 alpha,而是使用带有 alpha 值的色调颜色或背景颜色。这可以产生类似的效果,而不会导致所有子视图继承透明度。

toolbar.tintColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];

如果这不起作用并且您实际上需要使视图本身透明,则必须重新设计视图层次结构,以便不透明视图不是子视图。由于栏按钮项目不可用作标准视图,因此它可能无法在工具栏情况下使用。

When you set the alpha on a view, all subviews are composited with that same alpha value. What you can do instead of setting the alpha on the view itself is use a tint color or background color with an alpha value. This can produce similar effects, without causing all subviews to inherit the transparency.

toolbar.tintColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];

If this doesn't work and you actually need to make the view itself transparent, you will have to rework your view hierarchy so that the opaque views are not subviews. Since bar button items aren't available as standard views, its probably not going to be workable in the toolbar case.

§普罗旺斯的薰衣草 2024-10-24 06:52:15
self.bottomToolbar.barStyle = UIBarStyleBlack;
self.bottomToolbar.translucent = YES;
self.bottomToolbar.backgroundColor = [UIColor clearColor];

查看不透明度设置为 1,这些设置为我完成了它。

self.bottomToolbar.barStyle = UIBarStyleBlack;
self.bottomToolbar.translucent = YES;
self.bottomToolbar.backgroundColor = [UIColor clearColor];

View opacity set to 1 and these settings did it for me.

秋心╮凉 2024-10-24 06:52:15

上面的方法在 iOS 4.3 中对我不起作用,但这确实有效:

子类 UIToolbar,提供一种方法:

- (void)drawRect:(CGRect)rect 
{
[[UIColor colorWithWhite:0 alpha:0.6f] set]; // or clearColor etc
CGContextFillRect(UIGraphicsGetCurrentContext(), rect);
}

Above did not work for me in iOS 4.3, but this did:

Subclass UIToolbar, provide one method:

- (void)drawRect:(CGRect)rect 
{
[[UIColor colorWithWhite:0 alpha:0.6f] set]; // or clearColor etc
CGContextFillRect(UIGraphicsGetCurrentContext(), rect);
}
糖粟与秋泊 2024-10-24 06:52:15

我发现 barStyle 实际上并不重要。也没有将背景颜色设置为清除。

self.toolbar.barStyle = UIBarStyleDefault;
self.toolbar.tintColor = [UIColor colorWithWhite:0.0 alpha:0.5];
self.toolbar.translucent = YES;

这些行为我完成了工作(ios4.3)。
据我所知,将工具栏的半透明属性设置为“是”,您就将其设置为具有清晰(透明)的背景。当然,这取决于您设置的色调颜色。如果它不包含 alpha 分量,则该条将是不透明的。

I've found that barStyle does'n actually matter. Neither is setting background color to clear.

self.toolbar.barStyle = UIBarStyleDefault;
self.toolbar.tintColor = [UIColor colorWithWhite:0.0 alpha:0.5];
self.toolbar.translucent = YES;

Those lines done the job for me(ios4.3).
As I can understand, setting toolbars transfluent property to YES you've setting it to be with clear(transparent) background. Of course it depends on tint color you've set. If it doesn't contain alpha component the bar will be opaque.

寂寞美少年 2024-10-24 06:52:15

控制工具栏 alpha 值的主要问题是它适用于整个工具栏及其按钮。如果您设置一个带有某些 alpha 值的 backgroundColor backgroundColor 1,生成的颜色与 barStyle(UIBarStyleDefault 或 UIBarStyleBlack)相结合,产生比原始 backgroundColor 更不透明的颜色。如果您设置了 barTintColor,它似乎会覆盖所有其他设置,包括您设置的任何 alpha 值,从而产生完全不透明的颜色。

我能够在不影响其按钮的情况下减少工具栏的 alpha 的唯一方法是通过 通过 setBackgroundImage:forToolbarPosition:barMetrics: 设置其背景图像。您可以使用所需的颜色和 Alpha 创建 1 像素 x 1 像素的图像:

+ (UIImage *)onePixelImageWithColor:(UIColor *)color {
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(NULL, 1, 1, 8, 0, colorSpace, (CGBitmapInfo)kCGImageAlphaPremultipliedFirst);
    CGContextSetFillColorWithColor(context, color.CGColor);
    CGContextFillRect(context, CGRectMake(0, 0, 1, 1));
    CGImageRef imgRef = CGBitmapContextCreateImage(context);
    UIImage *image = [UIImage imageWithCGImage:imgRef];
    CGImageRelease(imgRef);
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
    return image;
}

[Swift 版本]

然后设置背景图片:

[toolbar setBackgroundImage:[self onePixelImageWithColor:[[UIColor blackColor] colorWithAlphaComponent:0.7]]
         forToolbarPosition:UIBarPositionBottom
                 barMetrics:UIBarMetricsDefault];

The main issue with controlling the alpha value of the toolbar is that it applies to the entire toolbar and its buttons. If you set a backgroundColor with some alpha value < 1, the resulting colour is combined with the barStyle (UIBarStyleDefault or UIBarStyleBlack) which results in a more opaque colour than the original backgroundColor. If you set the barTintColor, it seems to override all other settings, including any alpha value you set, resulting in a completely opaque colour.

The only way I've been able to do reduce the alpha of the toolbar without affecting its buttons is by setting its background image via setBackgroundImage:forToolbarPosition:barMetrics:. You can create a 1px by 1px image with the desired colour and alpha:

+ (UIImage *)onePixelImageWithColor:(UIColor *)color {
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(NULL, 1, 1, 8, 0, colorSpace, (CGBitmapInfo)kCGImageAlphaPremultipliedFirst);
    CGContextSetFillColorWithColor(context, color.CGColor);
    CGContextFillRect(context, CGRectMake(0, 0, 1, 1));
    CGImageRef imgRef = CGBitmapContextCreateImage(context);
    UIImage *image = [UIImage imageWithCGImage:imgRef];
    CGImageRelease(imgRef);
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
    return image;
}

[Swift version]

Then set the background Image:

[toolbar setBackgroundImage:[self onePixelImageWithColor:[[UIColor blackColor] colorWithAlphaComponent:0.7]]
         forToolbarPosition:UIBarPositionBottom
                 barMetrics:UIBarMetricsDefault];
夏见 2024-10-24 06:52:15

经过几个小时评估上述每种方法后,我发现只有 Ryan H. 提供的方法是可行的。这是因为这是您可以操纵 alpha 值的方法,而其他方法则不能。

这是 Swift 3 的版本:

override func viewDidLoad() {
    super.viewDidLoad()

    let bgImageColor = UIColor.black.withAlphaComponent(0.6) //you can adjust the alpha value here
    self.upperToolBar.setBackgroundImage(onePixelImageWithColor(color: bgImageColor),
          forToolbarPosition: .any,
          barMetrics: UIBarMetrics.default)
}

func onePixelImageWithColor(color : UIColor) -> UIImage {
    let colorSpace = CGColorSpaceCreateDeviceRGB()
    let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)
    var context = CGContext(data: nil, width: 1, height: 1, bitsPerComponent: 8, bytesPerRow: 0, space: colorSpace, bitmapInfo: bitmapInfo.rawValue)
    context!.setFillColor(color.cgColor)
    context!.fill(CGRect(x:0,y: 0, width: 1, height:1))
    let image = UIImage(cgImage: context!.makeImage()!)
    return image
}

After many hours evaluating each methods above, I found only the method provided by Ryan H. is feasible. It is because this is the method you can manipulate the alpha value while others cannot.

Here is the version for Swift 3:

override func viewDidLoad() {
    super.viewDidLoad()

    let bgImageColor = UIColor.black.withAlphaComponent(0.6) //you can adjust the alpha value here
    self.upperToolBar.setBackgroundImage(onePixelImageWithColor(color: bgImageColor),
          forToolbarPosition: .any,
          barMetrics: UIBarMetrics.default)
}

func onePixelImageWithColor(color : UIColor) -> UIImage {
    let colorSpace = CGColorSpaceCreateDeviceRGB()
    let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)
    var context = CGContext(data: nil, width: 1, height: 1, bitsPerComponent: 8, bytesPerRow: 0, space: colorSpace, bitmapInfo: bitmapInfo.rawValue)
    context!.setFillColor(color.cgColor)
    context!.fill(CGRect(x:0,y: 0, width: 1, height:1))
    let image = UIImage(cgImage: context!.makeImage()!)
    return image
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文