Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIViewController does not load its xib correctly

Tags:

ios

swift

ipad

I am reproducing the following case and am hoping to find out whats the theoretical cause of the problem. The problem is as follows:

When I declare a new Swift view controller and nib pair, sometimes the view controller's outlets don't load at all ( the nib doesn't load at all ). This happens only on specific devices ( in my case on iPad mini 1, non retina, iOS 8.4.1. On all other devices that I have on my disposal everything works as expected.

The solution that I found is to override the init with nib method :

override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
    super.init(nibName: Constants.viewControllerNibName, bundle: nil)
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

When I enter the nib name manually everything works as expected on all devices.

Can someone enlighten me a little, why this is needed and whats the cause of the problem.

The most interesting part of all is that the issue happens only on a very small subset of devices (in my case one, mentioned above).

UPDATE: It seems that the problem happens only on iOS 8 and not on iOS 9+, found that while testing and from the ticket linked below from Zonily Jame.

like image 290
Georgi Boyadzhiev Avatar asked Apr 15 '26 06:04

Georgi Boyadzhiev


1 Answers

This is what I use to mannually setup a viewController

class ViewController: UIViewController {
    init() {
        super.init(style: .plain)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}

Then I just setup the frame and add it as a subview.

let vc = ViewController()!
vc.view.frame = CGRect(x: xValue, y: yValue, width: desiredWidth, height: desiredHeight)
view.addSubview(vc.view)
addChildViewController(vc)
vc.didMove(toParentViewController: self)

and remove it by saying

vc.view.removeFromSuperview()
vc.removeFromParentViewController()
vc = nil
like image 92
Sethmr Avatar answered Apr 16 '26 21:04

Sethmr