【问题标题】:setting gradient in a UITabBar background在 UITabBar 背景中设置渐变
【发布时间】:2011-09-05 06:39:45
【问题描述】:

什么是我可以实现这样的最简单的方法:

是通过继承 UITabBarController 吗?如果是,需要更改什么属性?

【问题讨论】:

  • 这取决于你想在哪里展示这个......请发布更多详细信息。
  • 来自 UITabBarController 类参考:“此类不用于子类化。”如果你这样做,你可能会遇到应用程序获得批准的问题。

标签: iphone objective-c ios ipad uitabbar


【解决方案1】:
@interface UITabBarController (private)
- (UITabBar *)tabBar;
@end

@implementation CustomizeUITabBarController


- (void)viewDidLoad {
    [super viewDidLoad];

    CGRect frame = CGRectMake(0.0, 0.0, self.view.bounds.size.width, 48);
    UIView *v = [[UIView alloc] initWithFrame:frame];
    [v setBackgroundColor:kMainColor];
    [v setAlpha:0.5];
    [[self tabBar] addSubview:v];
    [v release];

}
@end

希望对你有所帮助....

【讨论】:

    【解决方案2】:

    是的,继承 UITabbarController 是正确的方法。

    覆盖drawRect方法:

    - (void)drawRect:(CGRect)rect {
        [[UIImage imageNamed:@"yourNavigationBar.png"] drawInRect: CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) ];
    }
    

    并将带有渐变的图像添加到您的项目中。

    【讨论】:

      【解决方案3】:

      这是一个老问题,但在搜索 TabBar 渐变时首先出现。一个更简单的方法是使用外观。只需将其放入您的 applicationDidFinishLaunching 方法中即可。

      [[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:@"navBarImage_1x49"]];
      

      另外,如果您更喜欢在代码中创建渐变图像,您可以这样做:

      CAGradientLayer *gradient = [CAGradientLayer layer];
      gradient.frame = CGRectMake(0, 0, 1, 49);
      gradient.colors = [NSArray arrayWithObjects:(id)[UIColor.darkGrayColor] CGColor], (id)[UIColor.blackColor] CGColor], nil];
      UIGraphicsBeginImageContext(gradient.frame.size);
      CGContextRef context = UIGraphicsGetCurrentContext();
      [gradient renderInContext:context];
      UIImage *gradientImage = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();
      

      【讨论】:

        【解决方案4】:

        我的应用遇到了同样的问题,我想出了以下问题: 在 applicationDidFinishLaunching 方法中,我创建了一个函数来绘制渐变,并使用我的 UITabBarController 的一个实例来根据设备的宽度设置正确的渐变框架。

        - (UIImage *)drawGradientInView:(UITabBarController *) tabBarVC {
            CAGradientLayer *gradient = [CAGradientLayer layer];
        gradient.frame = CGRectMake(CGRectGetMinX(tabBarVC.tabBar.frame), CGRectGetMinY(tabBarVC.tabBar.frame), CGRectGetWidth(tabBarVC.view.frame), CGRectGetHeight(tabBarVC.tabBar.frame));
        
            //set up your gradient
            //......
        
            UIImage *gradientImage = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
        
            return gradientImage;
        }
        

        获取UITabBarController的实例

        UITabBarController *tabVC = (UITabBarController *)[UIApplication sharedApplication].windows.firstObject.rootViewController;
        

        设置渐变

        [UITabBar appearance].backgroundImage = [self drawGradientInView:tabVC];
        

        我不确定这是否是正确的方法,但它确实对我有用。

        【讨论】:

          猜你喜欢
          • 2015-09-01
          • 1970-01-01
          • 2023-03-19
          • 1970-01-01
          • 2019-09-04
          • 1970-01-01
          • 1970-01-01
          • 2016-10-20
          • 2012-03-07
          相关资源
          最近更新 更多