4

I see similar questions already here and Google but none answer my actual question. I'd like to have a single NIB for both iPhone and iPad. The NIB itself is supposed to have an empty view only. Everything that should go into the view is done programmatically. I check if device is iPhone/iPad and then calculate and set the layout. A little more work but for the required task this the best solution for me.

But when adding a new view through "New File... -> User Interface -> View" I'm asked for the device Family. The choices are of course iPhone OR iPad. But I want the view to work on both devices, kind of generic.

I tried the iPhone view and it actually works on both devices, iPhone AND iPad. So it seems that everything is fine. My question is more generic asking if it is ok to re-use the iPhone view also for iPad. Shall I do it differently? Any better solution?

Please understand that I really want to work with a single view and programmatically, so solutions to use separate views for each device and use IB shall be left out please!!!

Thanks

Amir
  • 81
  • 4

2 Answers2

5

If you are creating everything programmatically, there is no need for a nib at all to create an empty view.

Your UIViewController subclass 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

You can instantiate the view controller using any format below:

MyViewController * controller = [[MyViewController alloc] initWithNibName:nil bundle:nil];
MyViewController * controller = [[MyViewController alloc] init];
MyViewController * controller = [MyViewController new];
Fruity Geek
  • 7,351
  • 1
  • 32
  • 41
0

First of all you can do this thing With AutoResize > LINK

and if it become complex for you then you can use below function and do it yourself programmatically.

-(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;    
}

OR if you want to do a Device version Specific code then try this.

-(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;
}
Community
  • 1
  • 1
utkal patel
  • 1,321
  • 1
  • 15
  • 24