ios - different storyboard based on phone model -
i'm working on app in swift needs run on iphone models 4s onwards want to laid out differently on 4s, want direct app load different storyboard on 4s. know can done ipad in info tab setting main storyboard file base name (ipad) cannot find way different iphone models. appreciated
following answer of @has here, can know model of device. let's make swift file entitled devicemodel
keep parts of extension separated.
devicemodel.swift
import uikit private let devicelist = [ /* ipod 5 */ "ipod5,1": "ipod touch 5", /* iphone 4 */ "iphone3,1": "iphone 4", "iphone3,2": "iphone 4", "iphone3,3": "iphone 4", /* iphone 4s */ "iphone4,1": "iphone 4s", /* iphone 5 */ "iphone5,1": "iphone 5", "iphone5,2": "iphone 5", /* iphone 5c */ "iphone5,3": "iphone 5c", "iphone5,4": "iphone 5c", /* iphone 5s */ "iphone6,1": "iphone 5s", "iphone6,2": "iphone 5s", /* iphone 6 */ "iphone7,2": "iphone 6", /* iphone 6 plus */ "iphone7,1": "iphone 6 plus", /* ipad 2 */ "ipad2,1": "ipad 2", "ipad2,2": "ipad 2", "ipad2,3": "ipad 2", "ipad2,4": "ipad 2", /* ipad 3 */ "ipad3,1": "ipad 3", "ipad3,2": "ipad 3", "ipad3,3": "ipad 3", /* ipad 4 */ "ipad3,4": "ipad 4", "ipad3,5": "ipad 4", "ipad3,6": "ipad 4", /* ipad air */ "ipad4,1": "ipad air", "ipad4,2": "ipad air", "ipad4,3": "ipad air", /* ipad air 2 */ "ipad5,1": "ipad air 2", "ipad5,3": "ipad air 2", "ipad5,4": "ipad air 2", /* ipad mini */ "ipad2,5": "ipad mini", "ipad2,6": "ipad mini", "ipad2,7": "ipad mini", /* ipad mini 2 */ "ipad4,4": "ipad mini", "ipad4,5": "ipad mini", "ipad4,6": "ipad mini", /* ipad mini 3 */ "ipad4,7": "ipad mini", "ipad4,8": "ipad mini", "ipad4,9": "ipad mini", /* simulator */ "x86_64": "simulator", "i386": "simulator" ] public extension uidevice { var modelname: string { var systeminfo = utsname() uname(&systeminfo) let machine = systeminfo.machine let mirror = reflect(machine) // swift 1.2 // let mirror = mirror(reflecting: machine) // swift 2.0 var identifier = "" // swift 1.2 - if use swift 2.0 comment loop out. in 0..<mirror.count { if let value = mirror[i].1.value as? int8 value != 0 { identifier.append(unicodescalar(uint8(value))) } } // swift 2.0 , later - if use swift 2.0 uncomment loop // child in mirror.children child.value as? int8 != 0 { // identifier.append(unicodescalar(uint8(child.value as! int8))) // } return devicelist[identifier] ?? identifier } }
then in appdelegate.swift
inside didfinishlaunchingwithoptions
can instantiate storyboard regarding device model, in following way:
func application(application: uiapplication, didfinishlaunchingwithoptions launchoptions: [nsobject: anyobject]?) -> bool { var storyboard: uistoryboard // instantiate `uiwindow` self.window = uiwindow(frame: uiscreen.mainscreen().bounds) // model name based in extension. let modelname = uidevice.currentdevice().modelname // if modelname variable contains string "iphone 6" inside. if (modelname.rangeofstring("iphone 6") != nil) { storyboard = uistoryboard(name: "iphone6storyboard", bundle: nil) self.window!.rootviewcontroller = storyboard.instantiateinitialviewcontroller() as! iphone6viewcontroller } else if (modelname.rangeofstring("ipad") != nil) { storyboard = uistoryboard(name: "ipadstoryboard", bundle: nil) self.window!.rootviewcontroller = storyboard.instantiateinitialviewcontroller() as! ipadviewcontroller } self.window!.makekeyandvisible() return true }
to above code works if have added uistoryboard
's need, set each uistoryboard
initial uiviewcontroller
using interface builder(in case don't want set initial uiviewcontroller
using interface builder, can find easy how set initial uiviewcontroller
programmatically, purpose of brevety i've used interface builder).
of course need in app's setting, go target , info
tab. there clear value of main storyboard file base name
. on general
tab, clear value main interface. can read more in answer programmatically set initial view controller using storyboards.
you need take care 2 things:
- in simulator can't test device models code(it give 'simulator'), need real devices.
- you need play devices name according code in
devicemodel.swift
match different names.
for sake of brevety put 2 devices above, in case need match device model want according names.
i hope you.
Comments
Post a Comment