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条回答
相关推荐>>
2楼-- · 2020-01-25 13:50

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.

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.

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.

查看更多
啃猪蹄的小仙女
3楼-- · 2020-01-25 13:51
  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 :-)

\

查看更多
Rolldiameter
4楼-- · 2020-01-25 13:51

ivanzoid's snippet above that queries for the whole screen size does the trick so long as you remember to subtract the offsets for navigation and toolbars (totalling 64 under most conditions).

It's the view height that needs to be adjusted; springs and struts otherwise take care of it.

Retesting my app on the iPhone 5 I only had to do this on one screen with some runtime control position adjustments. Every other case is handled by XIB defaults.

查看更多
唯我独甜
5楼-- · 2020-01-25 13:54

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.

查看更多
手持菜刀,她持情操
6楼-- · 2020-01-25 13:56

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(XIB) -> Attribute Inspector -> Simulated Metrics - Size: None

enter image description here

查看更多
走好不送
7楼-- · 2020-01-25 13:58

If you dont want use two xib and interested to do using Autosizing here

查看更多
登录 后发表回答