Sunday 29 September 2013

iOS 7 UINavigationController layout issue and workaround

Before iOS 7, UINavigationController layouts navigation bar above the view to be displayed. In iOS 7, the navigation bar is overlapping with the view to be displayed. This causes display issue for certain views. One workaround is through UINavigationControllerDelegate.

// Workaround for iOS7 UINavigationController layout issue.
// In iOS7, navigation bar is overlapping with the displayed view.
// Needs to shift it down by navigation bar height and shrink its height.
// But do not do this for table view.
- (void)navigationController:(UINavigationController *)navigationController
  didShowViewController:(UIViewController *)vc
animated:(BOOL)animated {
    if(!IOS7_OR_UP)
        return;
    if ([vc.view isKindOfClass:NSClassFromString(@"UITableView")])
        return;
    
    static int barH = 0;
    if (barH == 0) {
        UIView* bar = findViewOfClass(vc.view.superview.superview.superview, NSClassFromString(@"UINavigationBar"));
        barH = bar.frame.size.height;
    }
    
    CGRect frame = vc.view.frame;
    frame.size.height -= barH;
    frame.origin.y = barH;
    vc.view.frame = frame;

}

iOS 7 UITabBarController layout issue and workaround

Before iOS 7, UITabBarController layout the tab bar below the view to be displayed. However, in iOS 7, the tab bar is overlapping with the view to be displayed. This causes some display issues for some views drawn by user.

One workaround is to shrink the size of the view to be displayed by the UITabBarController by subclass UITabBarController.

@interface MyTabBarController : UITabBarController

@end

@implementation MyTabBarController

// Workaround for iOS UITabBarController layout issue.
// In iOS7, the tab bar overlaps with the view to be displayed.
// The height of the view to be displayed with a UITabBarController needs
// to be shrinked by the height of the tab bar.
-(void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    if (!IOS7_OR_UP)
        return;
    
    static int barHeight = 0;
    if (barHeight == 0) {
        UIView* bar = findViewOfClass(self.view, NSClassFromString(@"UITabBar"));
        barHeight = bar.frame.size.height;
    }
    UIView* CV = (UIView*)self.view.subviews.firstObject;
    CV.autoresizesSubviews = YES;
    UIView* GCV = (UIView*) CV.subviews.firstObject;
    UIView* GGCV = (UIView*) GCV.subviews.firstObject;
    CGRect frame = CV.frame;
    frame.size.height = self.view.frame.size.height - barHeight;
    CV.frame = frame;
    GCV.frame = frame;
    GGCV.frame = frame;
    
}

@end