-
所选ViewController的值发生更改;
-
界面取向变化;
-
您可以使用KVO观察selectedViewController的更改,如下所示:
[tabBarController addObserver:self forKeyPath:@"selectedViewController" options:NSKeyValueObservingOptionNew context:NULL]
观察界面方向的变化可能有点棘手。如果您是从应用程序委托或其他专用于此目的的对象执行此操作,则很可能无法获得用于软件间方向更改的标准UIViewController回调。有两种方法可以解决这个问题:(1)UIDevice和notifications,(2)让一个视图控制器生成回调。就我个人而言,我建议使用后者,因为UIDevice通知往往会与接口不同步几微秒,结果可能看起来不协调。简化实施:
// In a header somewhere (Prefix.pch works).
extern NSString *const kOrinetationNotification;
extern NSString *const kOrinetationNotificationOrientationKey;
// in an instance of UIViewController
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[[NSNotificationCenter defaultCenter] postNotificationName:kOrientationNotification object:self userInfo:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:toInterfaceOrientation] forKey:kOrientationNotificationOrientationKey]];
// do other stuff
}
// in your application delegate's (or other dedicated object) init method
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationDidChange:) name:kOrientationNotification object:nil];
这应该确保每次有什么东西触发选项卡栏几何图形的更改时,您都会抬起头来。一旦接收到这些回调,就可以从每个回调调用自己的自定义方法,并根据需要调整选项卡栏的大小,而无需对其进行子类化。