透明UI工具栏

问题描述 投票:0回答:7

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

[mtoolbar setBackgroundColor:[UIColor clearColor]];

如何使

UIToolbar
透明?

ios objective-c uikit uitoolbar
7个回答
12
投票

您可以将属性

translucent
设置为
YES
,看看这是否有帮助。


11
投票
[self.toolbar setBackgroundImage:[UIImage new]
              forToolbarPosition:UIToolbarPositionAny
                      barMetrics:UIBarMetricsDefault];

[self.toolbar setBackgroundColor:[UIColor clearColor]];

5
投票

将属性

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

4
投票

检查以下代码

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

摘自

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

UIToolBar 不能透明吗?

您也可以使用不同的方法

透明UIToolBar


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

;) 任何 iO


1
投票

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

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

透明工具栏.h

#import <UIKit/UIKit.h>

@interface TransparentToolbar : UIToolbar

@end

透明工具栏.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];

1
投票

这对我的 iOS 6 和 7 有用:

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

[self.toolBar setBackgroundImage:blank forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
© www.soinside.com 2019 - 2024. All rights reserved.