【问题标题】:Transparent UITabBarController with changeable view in background透明的 UITabBarController 在后台具有可变视图
【发布时间】:2012-06-19 02:24:22
【问题描述】:

我想要一个UITabBarController,它的 alpha 为 0.5,它的透明度允许您在背景中看到一个视图。背景视图必须易于访问和更改。

我可以使用这种技术添加背景:https://gist.github.com/1157542

这是一个Category,它将一个子视图添加到UITabBarController,并将子视图发送到后面。但是,因为它是一个类别,所以我不能使子视图成为一个属性。所以我无法真正访问它。

有没有办法让这个背景视图更加灵活和易于访问?例如,我可以从任何标签栏控制器的视图控制器轻松地向其中添加其他子视图?

【问题讨论】:

  • categories 可以添加对象 ;) 在某种程度上——objc 关联存储

标签: iphone objective-c ios uiview uitabbarcontroller


【解决方案1】:

您应该subclass UITabBarController 而不是类别。这将允许您更好地控制对象。这是一个子类的示例。

// MPCustomTabBar.h
@interface MPCustomTabBar : UITabBarController
- (void) setBackgroundImage:(UIImage *)image;
@end

// MPCustomTabBar.m
@interface MPCustomTabBar

- (void) setBackgroundImage:(UIImage *)image  {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,480)];
imageView.backgroundColor = [UIColor colorWithPatternImage:i];  
[[self view] addSubview:imageView];
[[self view] sendSubviewToBack:imageView];
[[self view] setOpaque:NO];
[[self view] setBackgroundColor:[UIColor clearColor]];
[imageView release];
}

@end

现在您可以进行所有您想要的自定义、分配和初始化您的新子类,如下所示:

MPCustomTabBar *bar = [[MPCustomTabBar alloc] init];

【讨论】:

【解决方案2】:

我的问题的解决方案可能就是这样..

在我的 AppDelegate 中,就在 [self.window makeKeyAndVisible]; 之前

self.theImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image1.png"]];
[self.tabBarController.view insertSubview:self.theImage atIndex:0];

然后我可以在任何标签栏控制器的视图控制器中轻松更改此图像,例如:

AppDelegate *appDelegate= (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.theImage.image = [UIImage imageNamed:@"image2.png"];

.. 不需要类别。

【讨论】:

    【解决方案3】:

    这对我有用:

    1. 通过将 Tint- 和 Background 设置为 Clear Color 并将 Opaque 设置为 No,使 TabBar 透明(通过 Storyboard 或以编程方式)。

    2. 黑色背景颜色实际上是窗口的颜色。将要用作背景的图像分配给窗口本身:

      UIImage *i = [UIImage imageNamed:@"main_background.png"]; UIColor *c = [[UIColor alloc] initWithPatternImage:i]; [self.window setBackgroundColor:c];

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-15
      • 2016-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多