How to make xib compatible with both iphone 5 and iphone 4 devices

前端 未结 14 1751
执念已碎
执念已碎 2020-11-28 20:39

I am trying to layout my xib so that layout fits in both iphone 5 (4 inches retina) and 3.5 devices.

Because I have to support IOS-5 I cannot use autolayout. I have

相关标签:
14条回答
  • 2020-11-28 20:54

    If your UI is too complicated and contains too many UIControls , then I suggest to do the following in viewDidLoad():

    NSLog(@"Iphone %f ",[[UIScreen mainScreen] bounds].size.height);
    if ([[UIScreen mainScreen] bounds].size.height == 568) 
    {
         //design frames of your controls accordingly add add the controls as subview to
         self.view            
         //this is iphone 5 xib
    } else 
    {
         //design frames of your controls accordingly add add the controls as subview to
         self.view 
         // this is iphone 4 xib
    }
    
    0 讨论(0)
  • 2020-11-28 21:01

    If you dont want use two xib and interested to do using Autosizing here

    0 讨论(0)
  • 2020-11-28 21:02
    • In your storyboard, click 'Show file inspector'.
    • Under Interface builder document->Document versioning, select Deployment=iOS 5 (or your choice).

    If this doesn't work, you need to work with each of your views. Select them. Under Attributes inspector for each of them, under View section, see Mode attribute. Set this to 'Redraw'.

    If even that doesn't give satisfactory result, set the view size to the smallest of all the version you are going to use. And set the Mode attribute = 'Scale to fill'.

    Programmatically, Mode attribute is view.contentmode property.

    0 讨论(0)
  • 2020-11-28 21:06
    1. You add new category for UIviewController and add this code in .h file

       - (id)initWithNibNameforIphone4:(NSString *)nibNameOrNil4 NibNameforIphone5:(NSString *)nibNameOrNil5 NibNameforIpad:(NSString *)nibNameOrNilpad bundle:(NSBundle *)nibBundleOrNil;
      
    2. Add this code in your .m file

       - (id)initWithNibNameforIphone4:(NSString *)nibNameOrNil4 NibNameforIphone5:(NSString *)nibNameOrNil5 NibNameforIpad:(NSString *)nibNameOrNilpad bundle:(NSBundle *)nibBundleOrNil
       {
         if (self = [super init])
       {
        self = [self initWithNibName:[self CheckDeviceIphone4:nibNameOrNil4 Iphone5:nibNameOrNil5 Ipad:nibNameOrNilpad] bundle:nibBundleOrNil];
        }
        return self;
      
      }
      
        -(NSString *)CheckDeviceIphone4:(NSString *)iphone4 Iphone5:(NSString *)iphone5 Ipad:(NSString *)ipad {
      
          return ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) ? ipad :([[UIScreen mainScreen] bounds].size.height == 568) ?  iphone5 :iphone4;
        }
      
    3. Open YouProject-Prefix.pch file and import your category here

    4. now you just use this in all over project like this

       self.firstView=[[firstView alloc]initWithNibNameforIphone4:@"firstView4" NibNameforIphone5:@"firstView" NibNameforIpad:@"firstViewIpad" bundle:[NSBundle mainBundle]];
      

      thanks and any question then comment and dont forget to upvote :-)

    \

    0 讨论(0)
  • 2020-11-28 21:08

    You need not use a different nib for the 3.5 inch and 4 inch devices. Design the app for the 4 inch device and set the AutoResizingMask correctly and it should work correctly.

    In your case just set the AutoResizingMask to

    [view setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth];
    

    The autoresizing mask places the view correctly to its position in both the devices.

    0 讨论(0)
  • 2020-11-28 21:10

    If you go with the solution of using 2 xib files; in your initWithNibName() method simply make a call to super with the different nib name.

    I would test on the original 480 height dimension rather than on the 568 height so that the larger xib file is selected when Apple releases a larger screen. In the future, at least the larger xib won't be letter-boxed as much as the smaller one.

    // From MainView.m
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        CGSize result = [[UIScreen mainScreen] bounds].size;
        if(result.height == 480)
        {
            // iPhone Classic
            self = [super initWithNibName:@"MainView" bundle:nibBundleOrNil];
        }
        else
        {
            // iPhone 5 or maybe a larger iPhone ??
            self = [super initWithNibName:@"MainView5" bundle:nibBundleOrNil];
        }
    
        return self;
    }
    
    0 讨论(0)
提交回复
热议问题