I only want iPhone 4s to have a different layout.

Asked 2 years ago, Updated 2 years ago, 36 views

I only want one page to be a completely different screen when I start using iPhone 4s with the app I'm creating.
What is the best way to do this? I'm thinking of creating two types of xib files and determining the device by screen size. Is that okay?

ios swift

2022-09-30 20:51

1 Answers

Consider switching between storyboards depending on the device.If you really want to switch between Xib files, replace the UIStoryboard class in the code below with the UINib class and read it.

I read the article on this blog.

Change your storyboard depending on your iOS device

First, create multiple storyboards.In the sample, we created two and named them Main and iPhoneMain, respectively.If you create a new Storyboard, you may want to replicate it to some extent because it starts with no objects at all.
Open the AppDelegate.swift file and add the application(application:UIApplication, didFinishLaunchWithOptions launchOptions:) method.

AppDelegate.swift

import UIKit

@UIAapplicationMain
classAppDelegate:UIResponder,UIApplicationDelegate{

    var window —UIWindow?


    func application(application:UIApplication, didFinishLaunchingWithOptions launchOptions:[NSObject:AnyObject]?) ->Bool{
        // Retrieving Devices
        let device = UIDevice.currentDevice()
        // Branch the name of the Storyboard by the model name of the device
        varsbName="Main"
        if device.model.hasPrefix("iPhone"){
            // Determine if 4s or earlier by screen size
            let screenSize = UISscreen.mainScreen().bounds.size
            if screenSize.height<=480.0 {// 4s for height 480 pixels or less
                sbName="iPhoneMain"
            }
        }
        // Instance ViewController by calling Storyboard by name
        let storyboard = UIStoryboard (name:sbName, bundle:nil)
        iflet viewController=storyboard.instantiateInitialViewController(){
            // Specify as rootViewController in the main window
            self.window?.rootViewController=viewController
        }

        return true
    }

hereinafter abbreviated

The UIDevice class properties model cannot distinguish between iPhone models, so we use screen size determination and matching techniques to determine if it is an iPhone 4s (previously).
Well, I think it's okay to judge only by the screen size.


2022-09-30 20:51

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.