0

I have a custom tab bar that was optimized for the iphone 5.

UINavigationBar *myBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
[self.view addSubview:myBar];

When it on the iphone 6, part of it is cut off of course because it only goes to 320 pixels.

How do i fix this? Is there a way to check which iphone its being ran on, and then run the pixel specified code? I plan on putting a background image on this navbar later so it must be centered.

ian
  • 1,002
  • 2
  • 12
  • 29

2 Answers2

1

In general it is a bad idea to hard code dimensions like this. This will break if you rotate, or if viewed on a screen with a different size than the original iPhone screen.

Many of these controls have a default size that you can use to your advantage. Instead of handing it a frame, consider just modifying the frame it gives you:

UINavigationBar *myBar = [[UINavigationBar alloc] init];
CGRect navBarFrame = myBar.frame;
navBarFrame.size.height // returns the right size for the current OS
navBarFrame.size.width = self.view.frame.size.width;
myBar.frame = navBarFrame;

This type of defensive coding is helpful in keeping your app laid out properly under many conditions, including when you embed this control into a parent view controller, or viewed on tomorrows larger-screened iOS devices.

All this said, are you sure you don't want a UIToolbar? Typically you don't ever create your own UINavigationBar, as that class is just used in UINavigationController for you.

Ben Scheirman
  • 40,531
  • 21
  • 102
  • 137
  • How would i handle the dynamic resizing of the height of the navbar as it changes for the 6 plus? – ian Dec 28 '14 at 19:28
  • You can handle the view controller method `willTransitionToTraitCollection:withTransitionCoordinator` and check, the traits. The navigation bar is normally smaller size when in a _compact_ height trait collection. – Ben Scheirman Dec 28 '14 at 22:00
0
UINavigationBar *myBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 50)];
[self.view addSubview:myBar];

may be this help you.

Daxesh Nagar
  • 1,405
  • 14
  • 22