【问题标题】:How to make UIToolbar's background transparent? [duplicate]如何让 UIToolbar 的背景透明? [复制]
【发布时间】:2013-04-26 18:40:24
【问题描述】:

我想让我的 UIToolBar 具有透明背景(类似于 iBooks),但我无法设置 translucent 属性。

这是我的代码:

UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    NSMutableArray *toolBarItems = [[NSMutableArray alloc] init];
    [toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:nil action:nil]];
    [toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:@"Source" style:UIBarButtonItemStyleBordered target:nil action:nil]];
    [toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:@"Aa" style:UIBarButtonItemStyleBordered target:nil action:nil]];
    [toolBarItems addObject:[[UIBarButtonItem alloc] initWithTitle:@"Rabbit" style:UIBarButtonItemStyleBordered target:nil action:nil]];
    toolBar.items = toolBarItems;
    toolBar.translucent = YES;
    [self.view addSubview:toolBar];

结果还是这样:

【问题讨论】:

  • 我相信你需要设置一个透明的背景图片才能做到这一点。只需创建一个 1x1 像素的透明 png。
  • @doug Smithh 因为 uitoolbar 是 uiview 的子类,您可以使用它的 layer 属性通过更改 alpha 的值使其透明

标签: ios objective-c uiview uitoolbar


【解决方案1】:

如果您希望工具栏为 透明:

[toolBar setBackgroundImage:[[UIImage alloc] init] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];

如果您希望工具栏为 半透明

[toolBar setBarStyle:UIBarStyleBlack];
toolBar.translucent = YES;

希望对你有所帮助。

【讨论】:

  • 你能解释一下你用于透明的方法吗?
  • Transparent 方法仍然在工具栏边框顶部显示一个细条,如何消除它?
  • iOS 7 的优雅解决方案
  • 似乎在 iOS 7 中您必须拥有所有三行代码才能获得透明度。如果我设置 translucent = NO,似乎根本无法在工具栏中获得透明度
  • @Malloc: [工具栏 setShadowImage:[UIImage new]forToolbarPosition:UIToolbarPositionAny];
【解决方案2】:

一种选择是继承 UIToolbar 并覆盖 draw 方法,按钮将继续正常绘制自己:

@interface TransparentToolbar : UIToolbar 
{
}

@implementation TransparentToolbar

// drawRect stub, toolbar items will still draw themselves
- (void)drawRect:(CGRect)rect
{
    return;
}

@end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-27
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 2013-09-06
    • 2021-05-04
    • 2023-03-23
    相关资源
    最近更新 更多