Ios 不同屏幕大小的故事板
如何在Swift中为每个可能的屏幕大小设置不同的故事板 我已经有了Objective-C代码 请不要自动布局,我不需要它 但如何将其转换为Swift?我不熟悉斯威夫特 以下是Objective-C的代码: AppDelgate.m文件Ios 不同屏幕大小的故事板,ios,objective-c,iphone,swift,uikit,Ios,Objective C,Iphone,Swift,Uikit,如何在Swift中为每个可能的屏幕大小设置不同的故事板 我已经有了Objective-C代码 请不要自动布局,我不需要它 但如何将其转换为Swift?我不熟悉斯威夫特 以下是Objective-C的代码: AppDelgate.m文件 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// int screenHeight = [UIScreen mainScreen].bounds.size.height;
// NSLog(@"Screen Height is %i", screenHeight);
// grab correct storyboard depending on screen height
UIStoryboard *storyboard = [self grabStoryboard];
// display storyboard
self.window.rootViewController = [storyboard instantiateInitialViewController];
[self.window makeKeyAndVisible];
return YES;
}
- (UIStoryboard *)grabStoryboard {
// determine screen size
int screenHeight = [UIScreen mainScreen].bounds.size.height;
UIStoryboard *storyboard;
switch (screenHeight) {
// iPhone 4s
case 480:
storyboard = [UIStoryboard storyboardWithName:@"Main-4s" bundle:nil];
break;
// iPhone 5s
case 568:
storyboard = [UIStoryboard storyboardWithName:@"Main-5s" bundle:nil];
break;
// iPhone 6
case 667:
storyboard = [UIStoryboard storyboardWithName:@"Main-6" bundle:nil];
break;
// iPhone 6 Plus
case 736:
storyboard = [UIStoryboard storyboardWithName:@"Main-6-Plus" bundle:nil];
break;
default:
// it's an iPad
storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
break;
}
return storyboard;
}
如果你只是想把代码转换成Swift,你可以参考苹果的Swift教程。你可以在iBook商店或网上查阅这本书。为了便于参考,我把代码转换成了Swift
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let splitViewController = self.window!.rootViewController as! UISplitViewController
let navigationController = splitViewController.viewControllers[splitViewController.viewControllers.count-1] as! UINavigationController
navigationController.topViewController!.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem()
splitViewController.delegate = self
return true
// int screenHeight = [UIScreen mainScreen].bounds.size.height;
// NSLog(@"Screen Height is %i", screenHeight);
// grab correct storyboard depending on screen height
let storyboard = grabStoryboard()
// display storyboard
self.window?.rootViewController = storyboard.instantiateInitialViewController()
self.window?.makeKeyAndVisible()
return true
}
func grabStoryboard() -> UIStoryboard
{
// determine screen size
let screenHeight = UIScreen.mainScreen().bounds.size.height
var storyboard: UIStoryboard! = nil
switch (screenHeight)
{
// iPhone 4s
case 480:
storyboard = UIStoryboard(name: "Main-4s", bundle: nil)
// iPhone 5s
case 568:
storyboard = UIStoryboard(name: "Main-5s", bundle: nil)
// iPhone 6
case 667:
storyboard = UIStoryboard(name: "Main-6", bundle: nil)
// iPhone 6 Plus
case 736:
storyboard = UIStoryboard(name: "Main-6-Plus", bundle: nil)
default:
// it's an iPad
storyboard = UIStoryboard(name: "Main", bundle: nil)
}
return storyboard
}
如果你只是想把代码转换成Swift,你可以参考苹果的Swift教程。你可以在iBook商店或网上查阅这本书。为了便于参考,我把代码转换成了Swift
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let splitViewController = self.window!.rootViewController as! UISplitViewController
let navigationController = splitViewController.viewControllers[splitViewController.viewControllers.count-1] as! UINavigationController
navigationController.topViewController!.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem()
splitViewController.delegate = self
return true
// int screenHeight = [UIScreen mainScreen].bounds.size.height;
// NSLog(@"Screen Height is %i", screenHeight);
// grab correct storyboard depending on screen height
let storyboard = grabStoryboard()
// display storyboard
self.window?.rootViewController = storyboard.instantiateInitialViewController()
self.window?.makeKeyAndVisible()
return true
}
func grabStoryboard() -> UIStoryboard
{
// determine screen size
let screenHeight = UIScreen.mainScreen().bounds.size.height
var storyboard: UIStoryboard! = nil
switch (screenHeight)
{
// iPhone 4s
case 480:
storyboard = UIStoryboard(name: "Main-4s", bundle: nil)
// iPhone 5s
case 568:
storyboard = UIStoryboard(name: "Main-5s", bundle: nil)
// iPhone 6
case 667:
storyboard = UIStoryboard(name: "Main-6", bundle: nil)
// iPhone 6 Plus
case 736:
storyboard = UIStoryboard(name: "Main-6-Plus", bundle: nil)
default:
// it's an iPad
storyboard = UIStoryboard(name: "Main", bundle: nil)
}
return storyboard
}
根据屏幕大小加载不同的故事板:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
// Override point for customization after application launch.
// int screenHeight = [UIScreen mainScreen].bounds.size.height;
// NSLog(@"Screen Height is %i", screenHeight);
// grab correct storyboard depending on screen height
var storyboard: UIStoryboard = self.grabStoryboard()
// display storyboard
self.window.rootViewController = storyboard.instantiateInitialViewController()
self.window.makeKeyAndVisible()
return true
}
func grabStoryboard() -> UIStoryboard {
// determine screen size
var screenHeight: Int = UIScreen.mainScreen().bounds.size.height
var storyboard: UIStoryboard
switch screenHeight {
// iPhone 4s
case 480:
storyboard = UIStoryboard.storyboardWithName("Main-4s", bundle: nil)
// iPhone 5s
case 568:
storyboard = UIStoryboard.storyboardWithName("Main-5s", bundle: nil)
// iPhone 6
case 667:
storyboard = UIStoryboard.storyboardWithName("Main-6", bundle: nil)
// iPhone 6 Plus
case 736:
storyboard = UIStoryboard.storyboardWithName("Main-6-Plus", bundle: nil)
default:
// it's an iPad
storyboard = UIStoryboard.storyboardWithName("Main", bundle: nil)
}
return storyboard
}
根据屏幕大小加载不同的故事板:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
// Override point for customization after application launch.
// int screenHeight = [UIScreen mainScreen].bounds.size.height;
// NSLog(@"Screen Height is %i", screenHeight);
// grab correct storyboard depending on screen height
var storyboard: UIStoryboard = self.grabStoryboard()
// display storyboard
self.window.rootViewController = storyboard.instantiateInitialViewController()
self.window.makeKeyAndVisible()
return true
}
func grabStoryboard() -> UIStoryboard {
// determine screen size
var screenHeight: Int = UIScreen.mainScreen().bounds.size.height
var storyboard: UIStoryboard
switch screenHeight {
// iPhone 4s
case 480:
storyboard = UIStoryboard.storyboardWithName("Main-4s", bundle: nil)
// iPhone 5s
case 568:
storyboard = UIStoryboard.storyboardWithName("Main-5s", bundle: nil)
// iPhone 6
case 667:
storyboard = UIStoryboard.storyboardWithName("Main-6", bundle: nil)
// iPhone 6 Plus
case 736:
storyboard = UIStoryboard.storyboardWithName("Main-6-Plus", bundle: nil)
default:
// it's an iPad
storyboard = UIStoryboard.storyboardWithName("Main", bundle: nil)
}
return storyboard
}
可能重复不重复我不想使用自动布局可能重复不重复我不想使用自动布局我的目标是为每个不同的屏幕大小使用不同的故事板,而不是自动布局,我已经这样做了这是目标c,但不知道如何做这是迅速的。所以你能不能帮我谢谢我的目标是使用不同的故事板为每个不同的屏幕大小,而不是自动布局,我已经做了这是目标c,但不知道如何做这是迅速的。所以你能帮我吗谢谢