【问题标题】:Autorotation disabled after setting selectedIndex property of UITabBarControllers (SDK bug?)设置 UITabBarControllers 的 selectedIndex 属性后禁用自动旋转(SDK 错误?)
【发布时间】:2011-01-19 18:09:06
【问题描述】:

问题。我的应用程序出现了一个非常奇怪的错误。我有一个 UITabBarController 和几个用于选项卡的视图控制器。在视图控制器中,我通过shouldAutorotateToInterfaceOrientation: 实现了自动旋转,并且在我进行以下更改之前它工作正常。

我在视图控制器中实现了滑动手势以在选项卡之间切换。这是通过以下代码完成的。

- (void)onSwipeLeft {
  int _count = [[self.tabBarController.tabBar items] count];
  int i = self.view.tag - 1;
  if (i < _count - 1) {
    self.tabBarController.selectedIndex = (i + 1) % _count;
  }
}

onSwipeRight 也是如此。

现在,自动旋转仅在您向左或向右滑动之前有效。之后,shouldAutorotateToInterfaceOrientation: 就再也不会被调用了。

另请参阅。

  • 在this thread 中描述了相同的问题。我有时也看到 类似以下的日志消息:-[UIWindow beginDisablingInterfaceAutorotation] overflow on &lt;UIWindow: 0x1410e0; frame = (0 0; 320 480); opaque = NO; autoresize = RM+BM; layer = &lt;CALayer: 0x141190&gt;&gt;. Ignoring.我找不到任何其他信息。

  • This question 似乎在描述同样的问题。

  • This question 似乎描述了一个类似的问题,但使用了popViewController:。请注意,该错误自 SDK 3.2 以来一直存在。

怎么办?这似乎是 SDK 中的一个错误,在 4.1 中仍然存在。有没有人找到解决方法?这似乎是一种常见的情况。

【问题讨论】:

  • 您的 UITabViewController 是否符合自动旋转功能?因为它是其他视图的父视图,所以它也必须允许旋转。
  • 我尝试继承 UITabViewController 并在 shouldAutorotateToInterfaceOrientation: 中返回 YES,但这没有帮助。

标签: iphone ipad ios uitabbarcontroller autorotate


【解决方案1】:

我早该想到的。

创建UIWindow+ensureAutorotation.h:

#import <UIKit/UIKit.h>

@interface UIWindow (ensureAutorotation)

- (void)beginDisablingInterfaceAutorotation;
- (void)endDisablingInterfaceAutorotation;

@end

还有UIWindow+ensureAutorotation.m:

#import "UIWindow+ensureAutorotation.h"

@implementation UIWindow (ensureAutorotation)

- (void)beginDisablingInterfaceAutorotation {}
- (void)endDisablingInterfaceAutorotation{}

@end

// of course this can be added as a simple category, rather than .h .m files

【讨论】:

  • 你能解释一下为什么这是一个解决方案……一个空方法吗?
  • 这可能会导致次要影响,因为您正在覆盖扩展内的 UIKit 方法,而忽略了原始方法的作用。不建议这样做,此外,在扩展中这样做是非常模糊的,因为您实际上并不知道会发生哪种实现。想象一下,如果您提供多个覆盖相同方法的扩展......会发生什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-03
  • 2016-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多