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

I'm trying to place my xib so that the layout fits in both iphone 5 (4 inches of the retina) and 3.5 devices.

Since I need to support IOS-5, I cannot use autorun. I have to use springs and struts.

I tried everything in the builder interface. But either my view goes beyond the iphone-3.5 inch or does not completely fill the iphone-4-inch-retina.

Can someone tell me how to actually make xib compatible with both devices?

For clarity, I am adding screenshots:

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

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

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

So you ask what settings I use. Here they are:

enter image description hereenter image description hereenter image description here

+45
iphone xcode interface-builder
Nov 07 '12 at 17:39
source share
14 answers
  • You add a new category for UIviewController and add this code to the .h file

    - (id)initWithNibNameforIphone4:(NSString *)nibNameOrNil4 NibNameforIphone5:(NSString *)nibNameOrNil5 NibNameforIpad:(NSString *)nibNameOrNilpad bundle:(NSBundle *)nibBundleOrNil; 
  • Add this code to 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; } 
  • Open YouProject-Prefix.pch and import your category here

  • now you just use it in the whole project like this

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

    thanks and any question, then comment and do not forget to raise :-)

\

+38
Nov 08
source share

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

 - (void) loadView { [super loadView]; self.view.frame = [UIScreen mainScreen].bounds; } 
+20
Dec 21
source share

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

  • Interface-builder (XIB) โ†’ Attribute Inspector โ†’ Simulated Indicators - Size: No

enter image description here

+11
Apr 17 '13 at 10:47 on
source share

I struggled with this today, and no matter what I did, my looks always showed as 320x480, even when working on the retina simulator 4. Even [UIScreen mainScreen] .bounds returned 320x480!

I found that adding the Default-568h@2x.png launcher image was the key for iOS to recognize my application as a โ€œready Retina 4". As soon as I did this, I found that I did not have to do anything to get nibs for automatic size without black bars. There is no need to have two separate xibs, change the settings in Interface Builder, override loadView, etc.

+6
Feb 14 '14 at 22:55
source share

You do not need to use a different tip for 3.5-inch and 4-inch devices. Build the application for the 4-inch device and configure AutoResizingMask correctly and it should work correctly.

In your case, just set the AutoResizingMask parameter to

 [view setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth]; 

The autoresist mask correctly places the view in its position on both devices.

+3
Sep 05 '13 at 11:53 on
source share

If you decide to use 2 xib files; in your initWithNibName () method, just make a super call with a different nib name.

I would test the original measurement of 480 height rather than 568 to increase the xib file size when Apple releases a larger screen. In the future, at least a larger xib will not be placed in a booklet the size of a 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; } 
+3
Oct 02 '13 at
source share

I have an idea. Separate your user interface into a header, body, and footer (e.g. on a website). Then, in the console of your code, find in the "Size Inspector" and use auto-creation.

Pay attention to the outer lines of the square, this is your control location against the main view. Set the controls (navigation bar, UIImageView, UIButton, etc.) in the header and body parts attached to the top and controls (bookmark, close, etc.) in the footer below.

Each time you run, the controls will attach to their autosave settings. You will have a space between the header / body and footer on the iPhone 5, but I think this is normal.

+1
Nov 08
source share

Define the condition below the line and device-based checks.

 #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);
 }
+1
Nov 20 '13 at 9:36
source share

I had a problem with 3.5 "versus 4" and I misunderstood what was wrong, so I wanted to document it here if it helps anyone.

If you try to access self.view.frame , this will be reported incorrectly before viewDidAppear or some similar event. If you try to access self.view.frame from viewDidLoad , then you can report frame sizes before the automation happens.

+1
Feb 07 '14 at 2:06
source share

Without using autorun, you may need to process many things in the code. I assume that most of your layout may work well with springs and struts, but some user interface elements cannot simply manually set the borders of certain objects to fit the size of your view.

0
Nov 07 '12 at 17:56
source share
  • In your storyboard, click Show File Inspector.
  • In the document "Interface Builder" โ†’ "Document Versioning", select "Deployment = iOS 5" (or your choice).

If this does not work, you need to work with each of your views. Select them. In the "Attributes inspector" section for each of them in the "View" section, see "Mode Attribute". Set this to Redraw.

Even if this does not give a satisfactory result, set the view size to the smallest of all versions that you intend to use. And set the attribute Mode = 'Scale to Fill'.

Programmatically, the mode attribute is the property view.contentmode .

0
Nov 07 '12 at 18:00
source share

The ivanzoid snippet over these queries for the entire screen size does the trick, as long as you remember, to subtract offsets for navigation and toolbars (a total of 64 in most conditions).

This is the height of the view that needs to be adjusted; springs and struts otherwise take care of this.

Retesting my application on iPhone 5 I only needed to do this on one screen with some settings for the runtime position control. Every other case is handled by default by default XIB.

0
May 01 '13 at 15:13
source share

If you do not want to use two xib and are interested in using Autosizing here

0
Jun 21 '13 at 6:12
source share

If your user interface is too complex and contains too much UIControl, then I suggest doing 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 } 
-one
Jun 21 '13 at 6:19 06:19
source share



All Articles