代码之家  ›  专栏  ›  技术社区  ›  Michael Waterfall

子类UITabBarController以调整其框架

  •  3
  • Michael Waterfall  · 技术社区  · 16 年前

    我在调整UITabBarController的大小时遇到一些问题,因为我只希望它占据屏幕的下半部分。它似乎强迫自己显示屏幕的全部高度(如果显示,则减去状态栏)。

    viewWillAppear;

    我尝试将UIWindow(选项卡栏控制器添加到其中)设置为 autoresizesSubviews:NO 选项卡栏是 autoresizingMask:UIViewAutoresizingNone 但那不行!

    1 回复  |  直到 11 年前
        1
  •  4
  •   Adam    16 年前

    • 所选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];
    

    这应该确保每次有什么东西触发选项卡栏几何图形的更改时,您都会抬起头来。一旦接收到这些回调,就可以从每个回调调用自己的自定义方法,并根据需要调整选项卡栏的大小,而无需对其进行子类化。