【问题标题】:Overriding both UITabBarController & UITabBar omits UITabBar覆盖 UITabBarController 和 UITabBar 省略 UITabBar
【发布时间】:2011-08-24 06:49:23
【问题描述】:

在我的项目中,我必须在 UITabBar 中设置背景图像,因为我使用以下代码覆盖 UITabBar:

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

效果很好。我还需要 UITabBarController 来旋转,所以我使用了以下代码:

@interface UITabBarController (Autorotate)
@end
@implementation UITabBarController (Autorotate)

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
   UIViewController *controller = self.selectedViewController;
   if ([controller isKindOfClass:[UINavigationController class]])
       controller = [(UINavigationController *)controller visibleViewController];
   return [controller shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
@end

添加此代码旋转部分后工作正常,但 UITabBar 部分不再工作。我该怎么办?任何帮助将不胜感激。

【问题讨论】:

    标签: iphone objective-c uitabbarcontroller overriding uitabbar


    【解决方案1】:

    您不必在 UITabBarController 中实现 -shouldAutorotateToInterfaceOrientation:。只需在您的每个视图控制器中实现它,应用程序就会按照您的预期运行。 (另外,文档说你不应该动态实现-shouldAutorotateToInterfaceOrientation。)

    【讨论】:

    • @iEamin:真的吗?嗯...也许尝试使用没有任何额外代码的空视图控制器创建一个新项目,只是为了测试一下?
    • 是的,你是对的,它的工作。但是为什么它在我的项目中不起作用。是不是因为在我的项目中每个选项卡都有导航控制器而不是直接查看控制器(导航控制器是从 IB 添加的,并且视图控制器设置为导航控制器的根)?
    • 不,应该也可以。也许您的某些控制器不支持自动旋转,或者您更改了其他内容...
    • 谢谢!它现在工作。抱歉,我错过了一个未将 shouldAutorotateToInterfaceOrientation 设置为 YES 的控制器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-01
    相关资源
    最近更新 更多