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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13275144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 04:38:12  来源:igfitidea点击:

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

iphonexcodeinterface-builder

提问by user739711

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

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

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

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

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.

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

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

有人可以提示如何实际使 xib 与两种设备兼容吗?

For more clarity I am adding screenshots:

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

When I set size 3.5 in attribute inspector: When I set size in attribute inspector to 3.5

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

it looks in iphone-5. There is a space below the buttons: And this is how it looks in simulator for 3.5 inch device

它看起来在 iphone-5。按钮下方有一个空格: 这就是它在 3.5 英寸设备模拟器中的样子

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

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

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

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

enter image description hereenter image description hereenter image description here

在此处输入图片说明在此处输入图片说明在此处输入图片说明

采纳答案by Waseem Shah

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

  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]];
    

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

\

\

回答by ivanzoid

Try adding this to all your controllers which need to support iPhone 5:

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

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

回答by Navnath Godse

Just set view size to None using Interface-builder
It will take view size at runtime, just you need to take care of origin and autosizing for each element(UILabel, UIImage, etc.) in the view.

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

  • Interface-builder(XIB) -> Attribute Inspector -> Simulated Metrics - Size: None
  • Interface-builder(XIB) -> Attribute Inspector -> Simulated Metrics - 大小:无

enter image description here

在此处输入图片说明

回答by Diarrhio

I was struggling with this today, and no matter what I did, my views were always showing up as 320x480, even when running on the retina 4" simulator. Even [UIScreen mainScreen].bounds was returning 320x480!

我今天为此苦苦挣扎,无论我做什么,我的视图总是显示为 320x480,即使在视网膜 4" 模拟器上运行时也是如此。甚至 [UIScreen mainScreen].bounds 也返回 320x480!

I found that adding the [email protected] launch image was the key to getting iOS to recognize my app as 'retina 4" ready'. Once I did that, I found had to do nothing else to get nibs to automatically size without the black bars. No need to have two separate xibs, change settings in Interface Builder, overriding loadView, etc.

我发现添加 [email protected] 启动图像是让 iOS 将我的应用程序识别为“视网膜 4”就绪的关键。一旦我这样做了,我发现无需做任何其他事情即可让笔尖自动调整大小没有黑条。不需要有两个单独的 xib,在界面生成器中更改设置,覆盖 loadView 等。

回答by MaheshShanbhag

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.

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

In your case just set the AutoResizingMask to

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

[view setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth];

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

自动调整大小掩码将视图正确放置在两个设备中的位置。

回答by Ed Trujillo

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.

如果您采用使用 2 个 xib 文件的解决方案;在您的 initWithNibName() 方法中,只需使用不同的笔尖名称调用 super 即可。

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.

我将在原始 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;
}

回答by Chris Alan

Define below line and check condition based on device.

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

#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);
}

回答by BigCheesy

I was having an issue with 3.5" vs. 4" as well, and I misunderstood what was wrong so I wanted to document it here incase it helps anyone.

我也遇到了 3.5" 与 4" 的问题,我误解了哪里出了问题,所以我想在这里记录一下,以防它对任何人都有帮助。

If you are trying to access self.view.frameit will not be correctly reported until viewDidAppearor some similar event. If you try to access self.view.framefrom within viewDidLoadthen you can be reported the dimensions of the frame beforeautosizing takes place.

如果您尝试访问self.view.frame它,直到viewDidAppear或发生类似事件时才会正确报告。如果您尝试self.view.frame从内部访问,viewDidLoad则可以在自动调整大小之前报告框架的尺寸。

回答by Shinigamae

I have an idea. Let separate your UI into header, body and footer (like website). Then in your code console, locate to Size Inspector and use the Autosizing.

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

Notice the outside lines of the square, it is your control location against main view. Set the controls (navigation bar, UIImageView, UIButton etc.) in header part and body part attached to Top and the controls (Bookmark, Close etc.) in footer to Bottom.

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

Everytime you run, the controls will attach to their autosizing settings. You will have a space between header/body and footer on iPhone 5 but I think it's fine.

每次运行时,控件都会附加到它们的自动调整大小设置。iPhone 5 上的页眉/正文和页脚之间会有一个空格,但我认为这很好。

回答by rooster117

Without using autolayout you may need to handle a lot of things in code. I assume most of your layout can work well with springs and struts but some UI elements can't so just manually set the frames of certain objects according to the size of your view is necessary.

如果不使用自动布局,您可能需要在代码中处理很多事情。我假设您的大部分布局可以很好地与弹簧和支柱配合使用,但某些 UI 元素不能,因此需要根据视图的大小手动设置某些对象的框架。