透明UI工具栏

发布于 2024-11-14 04:32:13 字数 140 浏览 3 评论 0原文

我编写了以下代码以使工具栏透明。

[mtoolbar setBackgroundColor:[UIColor clearColor]];

如何使 UIToolbar 透明?

I wrote the following code to make my toolbar transparent.

[mtoolbar setBackgroundColor:[UIColor clearColor]];

How do I make UIToolbar transparent?

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

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

发布评论

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

评论(7

冰火雁神 2024-11-21 04:32:13

您可以将属性 translucent 设置为 YES 并查看这是否有帮助。

You can set the property translucent to YES and see if this helps.

甜嗑 2024-11-21 04:32:13
[self.toolbar setBackgroundImage:[UIImage new]
              forToolbarPosition:UIToolbarPositionAny
                      barMetrics:UIBarMetricsDefault];

[self.toolbar setBackgroundColor:[UIColor clearColor]];
[self.toolbar setBackgroundImage:[UIImage new]
              forToolbarPosition:UIToolbarPositionAny
                      barMetrics:UIBarMetricsDefault];

[self.toolbar setBackgroundColor:[UIColor clearColor]];
凉墨 2024-11-21 04:32:13

将属性 translucent 设置为 YES 在 iOS 5 及更低版本中不起作用。以下是无需子类化工具栏即可完成的方法:

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

Setting the property translucent to YES will not work in iOS 5 and 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];
爱人如己 2024-11-21 04:32:13

检查以下代码

[myToolbar setBarStyle:UIBarStyleBlack];
[myToolbar setTranslucent:YES];

取自

@Brandon Bodnár 已在下面的帖子中回答。

UIToolBar 不能透明吗?

您还可以使用不同的方法

透明 UIToolBar

Check the below code

[myToolbar setBarStyle:UIBarStyleBlack];
[myToolbar setTranslucent:YES];

Taken from

@Brandon Bodnár has answered in the below SO post.

Couldn't UIToolBar be transparent?

you could also use the different approach

Transparent UIToolBar

探春 2024-11-21 04:32:13
for (UIView * sv in [toolBar subviews])
{
     [sv removeFromSuperview];
}

;) 任何 iO

for (UIView * sv in [toolBar subviews])
{
     [sv removeFromSuperview];
}

;) any iOs

音栖息无 2024-11-21 04:32:13

以下适用于 iOS 5(和 iOS 6 beta 4,尽管仍然可见轻微的顶部阴影)。

请注意:
让 UIToolbar 或 UINavigationBar 透明并不是一个好主意,以这种方式修改 Apple 的 UIKit 元素迟早会被破坏。

TransparentToolbar.h

#import <UIKit/UIKit.h>

@interface TransparentToolbar : UIToolbar

@end

TransparentToolbar.m

#import "TransparentToolbar.h"

@implementation TransparentToolbar

-(void)insertSubview:(UIView *)view atIndex:(NSInteger)index
{
    //  This method is called with a view of class "UINavigationBarBackground" or "_UIToolbarBackground", respectively. It would be possible to check for this with NSStringFromClass([view class]) to be completely sure that we're skipping the right view.

    if (index != 0)
    {
        [super insertSubview:view atIndex:index];
    }
    else
    {
        // insert your custom background view, if you want to
    }
}


@end

编辑:在iOS 5+中,也可以简单地设置backgroundImage(可以是透明的)。这当然是“更干净”的解决方案,但不如自定义 UIView 灵活。

[someToolbar setBackgroundImage:[UIImage imageNamed:@"clear"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];

The following works in iOS 5 (and iOS 6 beta 4, although a slight top shadow is still visible there).

Please note:
Making a UIToolbar or UINavigationBar transparent is rarely a good idea, and modifying Apple's UIKit elements in such a way is bound to break sooner or later.

TransparentToolbar.h

#import <UIKit/UIKit.h>

@interface TransparentToolbar : UIToolbar

@end

TransparentToolbar.m

#import "TransparentToolbar.h"

@implementation TransparentToolbar

-(void)insertSubview:(UIView *)view atIndex:(NSInteger)index
{
    //  This method is called with a view of class "UINavigationBarBackground" or "_UIToolbarBackground", respectively. It would be possible to check for this with NSStringFromClass([view class]) to be completely sure that we're skipping the right view.

    if (index != 0)
    {
        [super insertSubview:view atIndex:index];
    }
    else
    {
        // insert your custom background view, if you want to
    }
}


@end

EDIT: In iOS 5+, it's also possible to simply set the backgroundImage (which could be transparent). This is certainly the "cleaner" solution, but is less flexible than a custom UIView.

[someToolbar setBackgroundImage:[UIImage imageNamed:@"clear"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
泪是无色的血 2024-11-21 04:32:13

这对我的 iOS 6 和 7 有用:

UIGraphicsBeginImageContextWithOptions(CGSizeMake(1, 1), NO, 0.0);
UIImage *blank = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

[self.toolBar setBackgroundImage:blank forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];

This worked for me for iOS 6 and 7:

UIGraphicsBeginImageContextWithOptions(CGSizeMake(1, 1), NO, 0.0);
UIImage *blank = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

[self.toolBar setBackgroundImage:blank forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文