【问题标题】:Can I give a UIToolBar a custom background in my iPhone app?我可以在我的 iPhone 应用程序中为 UIToolBar 提供自定义背景吗?
【发布时间】:2010-12-28 19:20:33
【问题描述】:

是否可以从图像中为 UIToolBar 提供自定义背景,而不是通常的蓝色/黑色淡出?

我尝试为视图提供背景并设置 UIToolBar 的不透明度,但这也会影响其上任何 UIBarButtons 的不透明度。

【问题讨论】:

    标签: iphone uitoolbar


    【解决方案1】:

    在这里回答我自己的问题!!!覆盖 drawRect 函数并创建 UIToolbar 的实现就可以了:)

        @implementation UIToolbar (CustomImage)
    - (void)drawRect:(CGRect)rect {
        UIImage *image = [UIImage imageNamed: @"nm010400.png"];
        [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    }
    @end
    

    【讨论】:

    • 您正在 UIToolbar 上创建一个“类别”。
    • 这行得通,谢谢,但我认为子类化 UIToolbar 会更好。
    • 你是否需要调用那里的[super drawRect:]?它是如何知道绘制按钮或其他元素的?
    • 我相信所有其他元素都是子视图,所以不需要用drawRect绘制。
    • 简单的drawInRect:self.bounds怎么样?
    【解决方案2】:

    UIToolbar 继承自 UIView。这对我有用:

    [topBar insertSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:BAR_BKG_IMG]] autorelease] atIndex:0];
    

    【讨论】:

      【解决方案3】:

      loreto 答案的略微修改版本,适用于我在 ios 4 和 5 上:

      // Set the background of a toolbar
      +(void)setToolbarBack:(NSString*)bgFilename toolbar:(UIToolbar*)toolbar {   
          // Add Custom Toolbar
          UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:bgFilename]];
          iv.frame = CGRectMake(0, 0, toolbar.frame.size.width, toolbar.frame.size.height);
          iv.autoresizingMask = UIViewAutoresizingFlexibleWidth;
          // Add the tab bar controller's view to the window and display.
          if([[[UIDevice currentDevice] systemVersion] intValue] >= 5)
              [toolbar insertSubview:iv atIndex:1]; // iOS5 atIndex:1
          else
              [toolbar insertSubview:iv atIndex:0]; // iOS4 atIndex:0
          toolbar.backgroundColor = [UIColor clearColor];
      }
      

      【讨论】:

      • 在 iOS 5 中有新的 API 允许自定义控件。无需插入子视图。此外,不推荐检查系统版本,respondsToSelector 是一种更强大的替代方法。
      • 检查系统版本没有任何问题。 respondsToSelector 如何更健壮?
      • @diablosnuevos - 如果您现在检查系统版本,当新版本发布时,您将始终需要不断修改您的代码。 respondsToSelector 独立于系统版本。也就是说,无论版本如何,此方法都会响应 insertSubview,因此 respondsToSelector 在这里不合适。
      【解决方案4】:

      这是我用于 iOS 4 和 5 兼容性的方法:

      if ([toolbar respondsToSelector:@selector(setBackgroundImage:forToolbarPosition:barMetrics:)]) {
          [toolbar setBackgroundImage:[UIImage imageNamed:@"toolbar-background"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
      } else {
          [toolbar insertSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"toolbar-background"]] autorelease] atIndex:0];
      }
      

      【讨论】:

      • 嗨克里斯托弗,我正在实施这个解决方案,但我仍然可以看到工具栏上的突出显示(Apple 标准突出显示在工具栏的上半部分)。你知道是否有办法关闭那个亮点吗?
      • @MadOxyn 很奇怪,我没有看到那个亮点。你在哪个 iOS 版本上看到这个?
      • 我在 iOS4 和 5 上都看到了它,但我现在遇到了问题。我很愚蠢,它上面有一个分页控件,上面有一个半透明的白色背景,导致了那个亮点。我没有意识到这一点。您的回复让我重新思考;)谢谢。
      【解决方案5】:

      只需将此作品添加到您的-(void)viewDidLoad{}

      [toolBarName setBackgroundImage:[UIImage imageNamed:@"imageName.png"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
      

      【讨论】:

        【解决方案6】:

        如果您使用 idimmu 的答案并希望您的 barbuttonitems 被着色而不是默认值,您也可以将这几行代码添加到您的类别中:

        UIColor *color = [UIColor redColor];
        self.tintColor = color;
        

        【讨论】:

          【解决方案7】:

          iOS5 以后可以使用 Appearance API:

          [[UIToolbar appearance] setBackgroundImage:[UIImage imageNamed:@"navbar_bg"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
          

          【讨论】:

            【解决方案8】:

            要兼容 iOS 5,您可以执行以下操作

            -(void) addCustomToolbar {
            
                // Add Custom Toolbar
                UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"customToolbar.png"]];
                img.frame = CGRectMake(-2, -20, img.frame.size.width+4, img.frame.size.height);
            
                // Add the tab bar controller's view to the window and display.
            
                if( SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO( @"5.0" ) )
                   [self.tabBarController.tabBar insertSubview:img atIndex:1]; // iOS5 atIndex:1
                else
                  [self.tabBarController.tabBar insertSubview:img atIndex:0]; // iOS4 atIndex:0
            
                self.tabBarController.tabBar.backgroundColor = [UIColor clearColor];
            
                // Override point for customization after application launch.
                [self.window addSubview:tabBarController.view];
            
            }
            

            【讨论】:

            • 效果很好!由于某种原因,类别方法对我不起作用
            • 只需将 if 语句更改为:if([[[UIDevice currentDevice] systemVersion] intValue] >= 5)
            【解决方案9】:

            这个对我来说很好用:

            ToolbarOptions *tbar = [[ToolbarOptions alloc] init];
            [tbar setToolbarBack:@"footer_bg.png" toolbar:self.toolbarForPicker];
            [tbar release];
            
            #import <Foundation/Foundation.h>
            @interface ToolbarOptions : NSObject {
            
            }
            -(void)setToolbarBack:(NSString*)bgFilename toolbar:(UIToolbar*)toolbar;
            @end
            
            #import "ToolbarOptions.h"
            
            
            @implementation ToolbarOptions
            
            -(void)setToolbarBack:(NSString*)bgFilename toolbar:(UIToolbar*)bottombar {   
            // Add Custom Toolbar
            UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:bgFilename]];
            iv.frame = CGRectMake(0, 0, bottombar.frame.size.width, bottombar.frame.size.height);
            iv.autoresizingMask = UIViewAutoresizingFlexibleWidth;
            // Add the tab bar controller's view to the window and display.
            if([[[UIDevice currentDevice] systemVersion] intValue] >= 5)
                [bottombar insertSubview:iv atIndex:1]; // iOS5 atIndex:1
            else
                [bottombar insertSubview:iv atIndex:0]; // iOS4 atIndex:0
            bottombar.backgroundColor = [UIColor clearColor];
            }
            
            @end
            

            【讨论】:

              【解决方案10】:

              您可以使用基本上向 UIToolBar 添加新属性的类别来执行此操作。覆盖drawRect 可以工作,但不一定是未来的证明。自定义 UINavigationBar 的相同策略在 iOS 6 中停止工作。

              这就是我的做法。

              .h 文件

              @interface UIToolbar (自定义工具栏) @property (nonatomic, strong) UIView *customBackgroundView; @结尾

              .m 文件

              #import "自定义工具栏.h" #进口 静态字符 TIToolbarCustomBackgroundImage; @implementation UIToolbar(自定义工具栏) - (void)setCustomBackgroundView:(UIView *)newView { UIView *oldBackgroundView = [self customBackgroundView]; [oldBackgroundView removeFromSuperview]; [self willChangeValueForKey:@"tfCustomBackgroundView"]; objc_setAssociatedObject(self, &TIToolbarCustomBackgroundImage, 新视图, OBJC_ASSOCIATION_RETAIN); [self didChangeValueForKey:@"tfCustomBackgroundView"]; if (newView != nil) { [自我添加子视图:新视图]; } } - (UIView *)customBackgroundView { UIView *customBackgroundView = objc_getAssociatedObject(self, &TIToolbarCustomBackgroundImage); 返回自定义背景视图; } @结尾

              在您的视图控制器代码中,例如viewDidLoad

              如果(self.navigationController.toolbar.customBackgroundView == nil){ self.navigationController.toolbar.customBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"navigation_bar_background.png"]]; self.navigationController.toolbar.customBackgroundView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; }

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2021-07-19
                • 2010-09-26
                • 2016-06-08
                • 1970-01-01
                • 1970-01-01
                • 2014-06-07
                • 1970-01-01
                • 2018-12-23
                相关资源
                最近更新 更多