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

前端 未结 14 1752
执念已碎
执念已碎 2020-11-28 20:39

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

相关标签:
14条回答
  • 2020-11-28 21:10

    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);
    }
    
    0 讨论(0)
  • 2020-11-28 21:13

    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

    0 讨论(0)
  • 2020-11-28 21:13

    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.

    0 讨论(0)
  • 2020-11-28 21:13

    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.

    0 讨论(0)
  • 2020-11-28 21:13

    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.

    0 讨论(0)
  • 2020-11-28 21:14

    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.

    0 讨论(0)
提交回复
热议问题