Single NIB for iPhone and iPad

I see similar questions already here and Google, but no one answers my real question. I would like to have one NIB for iPhone and iPad. It is assumed that the NIB itself has only an empty view. Everything that should go to the point of view is carried out programmatically. I check if the device is iPhone / iPad, and then calculate and set the layout. A little more work, but for the required task this is the best solution for me.

But when adding a new view through the "New File ... → User Interface → View" they ask me about the device family. The choice, of course, is the iPhone or iPad. But I want the presentation to work on both devices, as if generic.

I tried viewing the iPhone and it works on both devices, iPhone and iPad. Everything seems to be in order. My question is more general, asking whether it is normal to use the iPhone also for the iPad. Should I do it differently? Any better solution?

Please understand that I really want to work with one view and programmatically, therefore solutions for using separate views for each device and using IB should be omitted, please.

thanks

+4
source share
2 answers

If you create everything programmatically, there is no need to create a nib to create an empty view.

Your subclass of UIViewController should look like this:

@implementation MyViewController
-(void)loadView
{
    //Do not call [super loadView] in your implementation. 
    //The super implementation loads the nib based on the nibName and nibBundle properties. 
    UIView * view = [[UIView alloc] init];
    //Add subviews, etc

    //You must assign a UIView object to the view property before loadView completes
    self.view = view;
}
@end

:

MyViewController * controller = [[MyViewController alloc] initWithNibName:nil bundle:nil];
MyViewController * controller = [[MyViewController alloc] init];
MyViewController * controller = [MyViewController new];
+5

, AutoResize > LINK

, .

-(int)setScreenOf
{

    int size=0;
    if((UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad))
    {
        //ipad 
         size=3;

    }
    else
    {

        CGRect screenBounds = [[UIScreen mainScreen] bounds];
        if (screenBounds.size.height ==568)
        {
             size=2;
            // code for 4-inch screen           
        }
        else
        {
            size=1;
            // code for 3.5-inch screen           
        }  
    }
    return  size;    
}

, .

-(NSString *)deviceVersion
{
    size_t size;
    sysctlbyname("hw.machine", NULL, &size, NULL, 0);
    char *answer = (char*)malloc(size);
    sysctlbyname("hw.machine", answer, &size, NULL, 0);
    NSString *platform = [NSString stringWithCString:answer encoding: NSUTF8StringEncoding];
    free(answer);
    NSLog(@"Platform: %@", platform);
    return platform;
}
0

All Articles