如何使 xib 与 iphone 5 和 iphone 4 设备兼容

如何使 xib 与 iphone 5 和 iphone 4 设备兼容

问题描述:

我正在尝试布局我的 xib,以便布局适合 iphone 5(4 英寸视网膜)和 3.5 设备.

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

因为我必须支持 IOS-5,所以我不能使用自动布局.我必须使用弹簧和 Struts.

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

我尝试了界面构建器中的所有内容.但是我的观点要么超出了 iphone-3.5 英寸的底部,要么没有完全填满 iphone-4 英寸的视网膜.

I tried everything in interface-builder. But either my view is going beyond the bottom of iphone-3.5-inch or not filling completely the iphone-4-inch-retina.

有人可以提示如何真正使 xib 兼容两种设备吗?

Can someone give a hint how to actually make an xib compatible to both the devices?

为了更清楚,我添加了屏幕截图:

For more clarity I am adding screenshots:

当我在属性检查器中设置大小为 3.5 时:

When I set size 3.5 in attribute inspector:

它看起来像 iphone-5.按钮下方有一个空格:

it looks in iphone-5. There is a space below the buttons:

如果我在界面生成器中设置大小为 4 英寸.您可以看到在 iphone-4 中底部按钮不可见.

If I set size 4 inch in interface builder. You can see that bottom buttons are not visible in iphone-4.

所以你会问我使用的是什么设置.它们是:

So you will ask what are the settings I am using. Here are they:

  1. 您为 UIviewController 添加新类别并在 .h 文件中添加此代码

  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;

  • 将此代码添加到您的 .m 文件中

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

  • 打开 YouProject-Prefix.pch 文件并在此处导入您的类别

  • Open YouProject-Prefix.pch file and import your category here

    现在你只需在像这样的整个项目中使用它

    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 :-)