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

时间:2012-11-07 17:39:40

标签: iphone xcode interface-builder

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

因为我必须支持IOS-5,所以我不能使用autolayout。我必须使用spring和Struts。

我在interface-builder中尝试了一切。但要么我的观点超出了iphone-3.5英寸的底部,要么完全没有填充iphone-4英寸视网膜。

有人可以提示如何让xib与这两种设备兼容吗?

为了更清晰,我添加了截图:

在属性检查器中设置大小为3.5时: When I set size in attribute inspector to 3.5

它看起来像iphone-5。按钮下方有一个空格: And this is how it looks in simulator for 3.5 inch device

如果我在界面构建器中设置了4英寸的大小。您可以看到底部按钮在iphone-4中不可见。 If I set size 3.5 inch in interface builder

所以你会问我正在使用的设置是什么。他们是:

enter image description here enter image description here enter image description here

14 个答案:

答案 0 :(得分:38)

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

     - (id)initWithNibNameforIphone4:(NSString *)nibNameOrNil4 NibNameforIphone5:(NSString *)nibNameOrNil5 NibNameforIpad:(NSString *)nibNameOrNilpad bundle:(NSBundle *)nibBundleOrNil;
    
  2. 在.m文件中添加此代码

     - (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. 打开YouProject-Prefix.pch文件并在此处导入您的类别

  4. 现在你只需在这个项目中使用它

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

    谢谢,然后任何问题评论,不要忘记upvote: - )

  5. \

答案 1 :(得分:20)

尝试将此添加到需要支持iPhone 5的所有控制器:

- (void) loadView
{
    [super loadView];
    self.view.frame = [UIScreen mainScreen].bounds;
}

答案 2 :(得分:11)

使用Interface-builder
将视图大小设置为None 它将在运行时获取视图大小,只需要在视图中处理每个元素(UILabel,UIImage等)的原点和自动调整。

  • 界面构建器(XIB) - >属性检查器 - >模拟指标 - 大小:无

enter image description here

答案 3 :(得分:6)

今天我正在努力解决这个问题,无论我做了什么,我的观点总是显示为320x480,即使在视网膜4“模拟器上运行。甚至[UIScreen mainScreen] .bounds返回320x480!

我发现添加Default-568h@2x.png启动图像是让iOS识别我的应用程序为“视网膜4”准备就绪的关键。一旦我这样做,我发现不得不做任何其他事情来获取笔尖在没有黑条的情况下自动调整大小。无需使用两个单独的xib,更改Interface Builder中的设置,覆盖loadView等。

答案 4 :(得分:3)

3.5英寸和4英寸设备不需要使用不同的笔尖。为4英寸设备设计应用程序并正确设置AutoResizingMask,它应该可以正常工作。

在您的情况下,只需将AutoResizingMask设置为

即可
[view setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth];

自动调整遮罩将视图正确放置在两个设备中的位置。

答案 5 :(得分:3)

如果你选择使用2个xib文件的解决方案;在你的initWithNibName()方法中,只需使用不同的nib名称调用super。

我将测试原始480高度尺寸而不是568高度,以便在Apple发布更大屏幕时选择更大的xib文件。将来,至少较大的xib不会像较小的xib那样使用信箱。

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

答案 6 :(得分:1)

我有个主意。让您的UI分为标题,正文和页脚(如网站)。然后在代码控制台中,找到Size Inspector并使用Autosizing。

注意方形的外线,它是您对主视图的控制位置。将标题部分和正文部分中的控件(导航栏,UIImageView,UIButton等)设置为顶部,将控件(书签,关闭等)设置为页脚到底部。

每次运行时,控件都会附加到自动调整设置中。你会在iPhone 5的标题/正文和页脚之间留出一个空格,但我认为没关系。

答案 7 :(得分:1)

定义以下行并根据设备检查条件。

#define IS_IPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) 
if (IS_IPHONE_5) {
    btnBookmark.frame=CGRectMake(0, 460, 100, 70);
    btnBookmark.frame=CGRectMake(150, 460, 100, 70);
}
else {
    btnBookmark.frame=CGRectMake(0, 360, 100, 70);
    btnBookmark.frame=CGRectMake(150, 360, 100, 70);
}

答案 8 :(得分:1)

我遇到了3.5"与4"同样,我误解了什么是错的,所以我想在这里记录它,这对任何人都有帮助。

如果您尝试访问self.view.frame,则在viewDidAppear或某些类似事件之前无法正确报告。如果您尝试从self.view.frame中访问viewDidLoad,则可以在自动调整大小之前报告框架的尺寸。

答案 9 :(得分:0)

不使用自动布局,您可能需要在代码中处理很多事情。我假设您的大多数布局都可以与弹簧和支柱一起使用,但是有些UI元素不能只根据视图的大小手动设置某些对象的帧。

答案 10 :(得分:0)

  • 在故事板中,点击“显示文件检查器”。
  • 在“界面”构建器文档 - >“文档版本控制”下,选择“部署= iOS 5(或您选择)”。

如果这不起作用,您需要处理每个视图。 选择它们。在每个属性的“属性”检查器下,在“视图”部分下,请参阅“模式”属性。将其设置为“重绘”。

如果即使这样也不能给出满意的结果,请将视图大小设置为您要使用的所有版本中的最小值。并设置Mode属性='Scale to fill'。

以编程方式,Mode属性为view.contentmode属性。

答案 11 :(得分:0)

只要您记得减去导航和工具栏的偏移(在大多数条件下总共64个),上面的ivanzoid的片段就可以查询整个屏幕尺寸。

这是需要调整的视图高度;弹簧和支柱否则会照顾它。

在iPhone 5上重新测试我的应用程序我只需要在一个屏幕上执行此操作,并调整一些运行时控制位置。其他每种情况都由XIB默认值处理。

答案 12 :(得分:0)

如果您不想使用两个xib并且有兴趣使用自动调整大小here

答案 13 :(得分:-1)

如果您的UI太复杂并且包含太多UIControl,那么我建议在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
}