【问题标题】:Couldn't UIToolBar be transparent?UIToolBar 不能是透明的吗?
【发布时间】:2011-01-28 22:57:26
【问题描述】:

我尝试了以下代码,但它不起作用。

[helloToolbar setBackgroundColor:[UIColor clearColor]];

【问题讨论】:

标签: cocoa-touch


【解决方案1】:

你能做的最好的就是使用

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

这将为您提供一个黑色但半透明的工具栏。

【讨论】:

  • 查看下面的解决方案以获得完全透明(非半透明)的工具栏。
  • 我添加了一个适用于 iOS 5.1 的答案 - 您将 UIToolbar 子类化并在 layoutSubviews 期间询问子视图。
【解决方案2】:

要制作一个完全透明的工具栏,请使用方法described here。简而言之,创建一个继承自 UIToolbar 的新 TransparentToolbar 类,并使用它来代替 UIToolbar。

TransarentToolbar.h

@interface TransparentToolbar : UIToolbar
@end

TransarentToolbar.m

@implementation TransparentToolbar

// Override draw rect to avoid
// background coloring
- (void)drawRect:(CGRect)rect {
    // do nothing in here
}

// Set properties to make background
// translucent.
- (void) applyTranslucentBackground
{
    self.backgroundColor = [UIColor clearColor];
    self.opaque = NO;
    self.translucent = YES;
}

// Override init.
- (id) init
{
    self = [super init];
    [self applyTranslucentBackground];
    return self;
}

// Override initWithFrame.
- (id) initWithFrame:(CGRect) frame
{
    self = [super initWithFrame:frame];
    [self applyTranslucentBackground];
    return self;
}

@end

(上面链接的博客文章中的代码)

【讨论】:

  • 谢谢!我做到了,但现在由于某种原因工具栏是黑色的不透明...
  • 这很好用,我用它作为一种“简单”的方式将多个按钮放在导航栏的左侧和/或右侧(另一个按钮有工具栏作为自定义视图)。然而,在这种情况下还需要做的另一件事是在 applyTranslucentBackground 方法中将 tintColor 设置为 clearColor。
  • 我认为它在 iOS 5 上停止工作。但在 iOS 5 上,你有“正确”的方法让它变得透明。
  • iOS5.1 的正确方法对我不起作用,所以我按原样实现了它,它起到了一种魅力。也许稍后当我有时间时,我会发现哪里出了问题,并以新的 iOS5 方式完成。
  • 我的 UIBarButton 应该是白色的项目在这个工具栏代码中非常暗淡
【解决方案3】:

另一种解决方案是为 UIToolbar 定义一个类别:

@implementation UIToolbar(Transparent) 
-(void)drawRect:(CGRect)rect {
    // do nothing in here
}
@end

在 IB 中将工具栏设置为黑色半透明且不透明。

【讨论】:

  • 请注意,这会使您应用中的所有工具栏变得透明。
【解决方案4】:

我们刚刚注意到覆盖 drawRect 不再适用于 iOS 4.3。它不再被调用(编辑:似乎只在模拟器中)。而是调用 drawLayer:inContext:。

here 发布了一个很好的解决方案

现在您可以将每个 UIToolbar 对象设置为透明,方法是将其 tintColor 设置为 [UIColor clearColor] :)

【讨论】:

    【解决方案5】:

    我刚刚在模拟器和手机上使用 iOS 4.3 测试了以下内容,似乎工作正常。子类 UIToolbar,提供一种方法:

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

    【讨论】:

      【解决方案6】:
      toolbar.barStyle = UIBarStyleBlackTranslucent;
      

      【讨论】:

        【解决方案7】:

        感谢@morais 的解决方案 - 这是转换为 MonoTouch 的代码:

          public class TransparentToolbar : UIToolbar
          {
            public TransparentToolbar()
            {
              init();
            }
        
            public TransparentToolbar(RectangleF frame) : base(frame)
            {
              init();
            }
        
            void init()
            {
              BackgroundColor=UIColor.Clear;
              Opaque=false;
              Translucent=true;
            }
        
            public override void Draw(RectangleF rect)
            {
            }
          }
        

        【讨论】:

          【解决方案8】:

          在 iOS 5 中,以下工作:

          UIToolbar *bar = [[UIToolbar alloc] initWithFrame:CGRectZero];
          if (bar.subviews.count > 0)
               [[[bar subviews] objectAtIndex:0] removeFromSuperview];
          

          这是因为背景现在是子视图。即使在 iOS 的新迭代中,此代码也是安全的,它可能会停止工作。这不是私有 API 使用,您的应用可以安全地提交到商店。

          确保在将任何UIBarButtonItems 添加到栏之前删除背景视图。否则我的代码将无法正常工作。

          【讨论】:

            【解决方案9】:

            在 iOS 5 中,只需调用 setBackgroundImage 并传递一个透明图像。

            我是这样做的(我动态生成透明图像):

            CGRect rect = CGRectMake(0, 0, 1, 1);
            UIGraphicsBeginImageContext(rect.size);
            CGContextRef context = UIGraphicsGetCurrentContext();
            CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
            CGContextFillRect(context, rect);
            UIImage *transparentImage = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            
            [toolbar setBackgroundImage:transparentImage forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
            

            【讨论】:

            • 在 iOS 4 中是否可以提供相同的功能?
            • iOS > 5.0 的不错解决方案。谢谢
            • 你可以只传递一个新分配的 UIImage:[toolbar setBackgroundImage:[UIImage alloc] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
            【解决方案10】:

            这可以在 iOS5.1 中以极少的努力工作。我正在匹配大小,因为只有背景与工具栏本身具有相同的框架大小。当然,您可以使用其他标准。

            享受吧。

            如下创建一个UIToolbar的子类:

            .h:

            #import <UIKit/UIKit.h>
            
            @interface UIClearToolbar : UIToolbar
            
            @end
            

            .m:

            #import "UIClearToolbar.h"
            
            @implementation UIClearToolbar
            
            - (void)layoutSubviews {
                // super has already laid out the subviews before this call is made.
                [self.subviews enumerateObjectsUsingBlock:^(UIView* obj, NSUInteger idx, BOOL *stop) {
                    if (CGSizeEqualToSize(self.frame.size, obj.frame.size) ||
                    self.frame.size.width <= obj.frame.size.width) {  // on device, the background is BIGGER than the toolbar.) {
                        [obj removeFromSuperview];
                        *stop = YES;
                    }
                }];
            }
            
            @end
            

            【讨论】:

              【解决方案11】:

              透明(iOS 5.0):

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

              半透明:

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

              【讨论】:

              • 这对我有用。斯威夫特:self.setBackgroundImage(UIImage(),forToolbarPosition:.any,barMetrics:.default)
              【解决方案12】:

              适用于所有设备的累积解决方案,从最旧的 iOS 3.0 (iPhone 1) 到最新的 iOS 6.1 (iPad mini)。

              @implementation UIToolbar (Extension)
              
              - (void)drawRect:(CGRect)rect
              {
                  if (CGColorGetAlpha(self.backgroundColor.CGColor) > 0.f)
                  {
                      [super drawRect:rect];
                  }
              }
              
              - (void)setTransparent
              {
                  //iOS3+
                  self.backgroundColor = [UIColor clearColor];
              
                  //iOS5+
                  if ([self respondsToSelector:@selector(setBackgroundImage:forToolbarPosition:barMetrics:)])
                  {
                      [self setBackgroundImage:[[UIImage new] autorelease] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
                  }
                  //iOS6+
                  if ([self respondsToSelector:@selector(setShadowImage:forToolbarPosition:)])
                  {
                      [self setShadowImage:[[UIImage new] autorelease] forToolbarPosition:UIToolbarPositionAny];
                  }
              }
              
              @end
              

              如果您想要一个透明的工具栏,请在上面调用setTransparent。 当您想要一个不透明的工具栏时,设置您选择的背景颜色或自己添加一个 imageView。

              【讨论】:

                猜你喜欢
                • 2014-05-27
                • 1970-01-01
                • 2011-09-09
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2012-06-02
                相关资源
                最近更新 更多