1

I would like to use the same nib file for both iPad and iPhone.

I found there are two black bars in the top and bottom when I run the app on iPhone 6 plus. So I found there is a fixed size defined in my xib file, and they are not editable.

How should I make it adapt to different screen size?

Note: * The project doesn't use autolayout and storyboard

enter image description here

user469652
  • 48,855
  • 59
  • 128
  • 165

3 Answers3

5

On viewWillAppear set frame of your xib file.

swift

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.view.frame = UIScreen.main.bounds
    }

Objective-C

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[self view] setFrame:[[UIScreen mainScreen] bounds]];
}
Tapas Pal
  • 7,073
  • 8
  • 39
  • 86
2

There is no way around it, you need to use autolayout and size classes: https://developer.apple.com/library/ios/recipes/xcode_help-IB_adaptive_sizes/chapters/AboutAdaptiveSizeDesign.html

You can use nib files, that's fine, but autolayout and size classes are unavoidable if you're going to make an app for all devices.

bsarrazin
  • 3,990
  • 2
  • 18
  • 31
  • Incorrect. If your nib is for iPhone, you will always have black lines around it and it will never be universal. If you want your app to be universal, you have to use autolayout and size classes. Otherwise, you need multiple nibs for multiple devices. And yes a very bad UX and costly code base to maintain. – bsarrazin Mar 13 '15 at 04:40
  • No, Without AutoLayout and size classes Universal application will be possible..U have to use Autoresizing..and active all the resizable arrow... this was working for me.. – Dipen Chudasama Mar 13 '15 at 06:00
-2

AutoResizing Image

You have to unable all the Auto Resizing masks at XIB

In ur picture, u also need to set Width & Height Auto Resizing masks..So that the xib/view will take the width & Height as per super view or fill the screen.

For more info about Auto Resizing Masks refer below link..

Autoresizing masks programmatically vs Interface Builder / xib / nib

Hope it helps you..

Community
  • 1
  • 1
Vidhyanand
  • 5,369
  • 4
  • 26
  • 59
  • See my comment answering your incorrect comment (deleted comment?) – bsarrazin Mar 13 '15 at 04:41
  • I will also suggest AutoLayouts to support for all devices as Apple Recommended..but If u don't want to use it then go for AutoResizing masks..I would prefer AutoLayouts only... – Vidhyanand Mar 13 '15 at 04:45
  • To be clear, autosizing masks requires you to have a nib for iPhone and a nib for iPad. OP specifically said "I would like to use the same nib file for both iPad and iPhone". The only way to achieve this is to use autolayout and size classes. – bsarrazin Mar 13 '15 at 04:46