0

I'm updating app from iOS 4.3 to iOS 7 and I cannot fix one issue. I did lots of searches online but with no luck. There are similar problems but it doesn't work for me:

link 1 link 2

I don't use storyboard, I use xib files. I have view controller embedded in nav controller embedded in tab bar controller. I add my views in didFinishLaunchingWithOptions: method like that:

    HomeViewController *homeViewController = [[HomeViewController alloc] init];
    homeViewController.tabBarItem.image = [UIImage imageNamed:@"home.png"];
    homeViewController.tabBarItem.title = NSLocalizedString(@"Home", @"Home");
    UINavigationController *homeNavController = [[UINavigationController alloc] initWithRootViewController:homeViewController];
    homeNavController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
    //...other views
    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:homeNavController, ..., nil];

To add custom background image I use code (still in the same method):

    [[UINavigationBar appearance] setTintColor:UIColorFromRGB(0xFFA722)];
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"Logo.png"] forBarMetrics:UIBarMetricsDefault];

I try add UINavigationBarDelegate and:

- (UIBarPosition)positionForBar:(id <UIBarPositioning>)bar
{
    return UIBarPositionTopAttached;
}

But when I set the delegate, I try to do it in two ways:

//in didFinishLaunchingWithOptions

homeNavController.navigationBar.delegate = self;

of in view controller:

self.navigationController.navigationBar.delegate = self;

The error appears:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Cannot manually set the delegate on a UINavigationBar managed by a controller.'

Do you know how can I fix this issue to have nav bar layout right on the top without override status bar?

The other issue is that I have image Logo with size 320x44 and retina version 640x88 in my app the image is being repeated. Why is like that it has exact size as nav bar? Thanks in advance.

Community
  • 1
  • 1
Greg
  • 25,317
  • 6
  • 53
  • 62
  • @iDev in viewDidLoad I added log NSLog(@"FRAME: %@", NSStringFromCGRect(self.navigationController.navigationBar.frame)); and it show 44. I update the app from iOS4 to iOS7 but I still want to support iOS 6 (6 and above). – Greg Jan 30 '14 at 12:25
  • you set navbar size in 320*64. because the navbar merge with status bar 44+20=64 – codercat Jan 30 '14 at 12:29
  • I can resize image to 64 but it still will be override status bar. – Greg Jan 30 '14 at 12:40

1 Answers1

0

One way that I use:

    UIView *navBar = nil;
    [_customBar removeFromSuperview];
    _customBar = nil;
    //
    CGFloat width = self.view.frame.size.width; // 320px
    CGFloat height = 110.0f;
    // subview height
    navBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
    [navBar setBackgroundColor:[UIColor blueColor]];
    // redefine bar height
    UINavigationBar *currentNavBar = self.navigationController.navigationBar;
    CGRect frame = [currentNavBar frame];
    frame.size.height = height;
    frame.origin.x = 0.0f;
    [currentNavBar setFrame:frame];
    _normalBar = (UINavigationBar*) navBar;
    // ******************************
    // add your custom view to navBar
    // ******************************
    _normalBar = (UINavigationBar*) navBar;
    [self.navigationController.navigationBar addSubview:navBar];

And don't forget restore the original bar:

[_normalBar removeFromSuperview];
_normalBar = nil;
CGRect frame =  self.navigationController.navigationBar.frame;
frame.size.height = 44.0f;
[self.navigationController.navigationBar setFrame:frame];
Alexmelyon
  • 1,168
  • 11
  • 18