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;

}

No comments:

Post a Comment