tomDev
tomDev

Reputation: 5760

Creating specific storyboard to iPhone X

I'm having a hard time to scale up everything to iPhone X using auto layout and constraints on a single storyboard.

iPhone 4s small screen, 6, 6+, all works well, but iPhone X tall screen with safe areas is a nightmare to custom design apps. It's just easier to create a specific storyboard only to the iPhone X, and keep the other storyboard to all other screen sizes.

As long as it's possible to do this. Can I load an specific storyboard only to iPhone X? How?

I already have an storyboard to iPad and another one to iPhone, but this can be configured on the plist.

Thanks!

Upvotes: 2

Views: 1294

Answers (2)

tomDev
tomDev

Reputation: 5760

I know this is not the best approach but it works flawlessly until I have time to make a universal storyboard.

Add this to AppDelegate to split the storyboards between iPad, iPhone X, and other iPhones.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    let storyboard = grabStoryboard()

}


func grabStoryboard() -> UIStoryboard {
    // determine screen size
    let screenHeight = UIScreen.main.bounds.size.height
    let screenWidht = UIScreen.main.bounds.size.width
    var storyboard: UIStoryboard! = nil

    if (UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.phone) {

        if ( screenHeight == 812 && screenWidht == 375) {

            print ("iphone x") 
            storyboard = UIStoryboard(name: "Main_iPhoneX", bundle: nil)

        } else {

            print ("all other phones")
            storyboard = UIStoryboard(name: "Main", bundle: nil)

        }

    } else {

        storyboard = UIStoryboard(name: "Main_iPad", bundle: nil)

    }

    return storyboard
}

Upvotes: 1

Er.Gopal Vasani
Er.Gopal Vasani

Reputation: 448

Actually there is no need to do this if you well aware for Constraint concept , but you want to load different xib files than you can do it.

you have to put the condition for checking the device is iphoneX or not. you can compare the device based on its screen size. so i am not writing any hint for that.

if (iphoneX) {

  // initialized the Instance of ViewController with identifier(OR with NIB name) which is defined for iphoneX.

} else {

  // initialized the Instance of ViewController with identifier(OR with NIB name) which is Not defined for iphoneX.
}

Happy Coding.!

Upvotes: 0

Related Questions