Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load xib for iPhone 5 display

Tags:

tabs

ios

xib

I have an App created with IB with a tab bar and several tabs, one of the tabs views has a background image that cannot automatically be resized (stretched). The view has a dozen buttons hidden behind the background and the location of the buttons must changed as per the background.

I am thinking of copying the XIB file and editing the XIB to select a 568 pixel background image and repositioning the buttons down the page accordingly. Then at runtime I would like to add code to select the 568 pixel when on the iPhone 5 (iPhone x/5 selection is not an issue)".

One last thing, I would like to use the same view controller and connections (if possible), since all code is common. Is it possible to do that? How do I create the XIB and make it visible upon tab selection.

like image 450
PeterPurple Avatar asked Dec 14 '25 17:12

PeterPurple


1 Answers

You can check the height of the screen, and programmatically choose which xib you want to use:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    MyFirstViewController *vc1 = nil;
    MySecondViewController *vc2 = nil;

    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        CGSize screenSize = [[UIScreen mainScreen] bounds].size;
        if(screenSize.height == 568) {
            vc1 = [[MyFirstViewController alloc] initWithNibName:@"LargeFirstViewController" bundle:nil];
            vc2 = [[MySecondViewController alloc] initWithNibName:@"LargeSecondViewController" bundle:nil];
        }
        if(screenSize.height == 480) {
            vc1 = [[MyFirstViewController alloc] initWithNibName:@"SmallFirstViewController" bundle:nil];
            vc2 = [[MySecondViewController alloc] initWithNibName:@"SmallSecondViewController" bundle:nil];
        }
    }
    else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        // ... Add iPad code here if relevant.
    }

    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.viewControllers = @[vc1, vc2];
    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
    return YES;
}

To change the tab bar icon image programmatically edit the following in your view controller (replace "YOUR-IMAGE" with the actual name... do not put the extension (such as .png)):

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tabBarItem.image = [UIImage imageNamed:@"YOUR-IMAGE"];
    }
    return self;
}

When you create a new xib file, don't forget to select the "File Owner" (under "Placeholders") and set the "Custom Class" to the actual view controller class in the Attributes Inspector. Also, while the "File Owner" is selected, go to the "Connections Inspector" and drag the "view" outlet to the top level view of your xib.

like image 127
J Shapiro Avatar answered Dec 17 '25 13:12

J Shapiro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!