How to make xib compatible with both iphone 5 and

2020-01-25 13:35发布

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 to use springs and Struts.

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.

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

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

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

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

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

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

14条回答
你好瞎i
2楼-- · 2020-01-25 13:39

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!

I found that adding the Default-568h@2x.png 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.

查看更多
姐就是有狂的资本
3楼-- · 2020-01-25 13:42

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);
}
查看更多
我只想做你的唯一
4楼-- · 2020-01-25 13:43

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
}
查看更多
冷血范
5楼-- · 2020-01-25 13:44

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.

查看更多
Lonely孤独者°
6楼-- · 2020-01-25 13:45

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.

If you are trying to access self.view.frame it will not be correctly reported until viewDidAppear or some similar event. If you try to access self.view.frame from within viewDidLoad then you can be reported the dimensions of the frame before autosizing takes place.

查看更多
姐就是有狂的资本
7楼-- · 2020-01-25 13:46

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

- (void) loadView
{
    [super loadView];
    self.view.frame = [UIScreen mainScreen].bounds;
}
查看更多
登录 后发表回答