【发布时间】:2021-01-04 19:14:01
【问题描述】:
这是一个转发,因为另一个已关闭。
我在自动布局方面遇到了麻烦,所以我只想制作两个故事板,iPad 和 iPhone。我想要的只是检测它的设备类型或大小,然后显示他们尊重的故事板。我已经尝试了在 StackOverflow 上找到的几乎所有解决方案,但我无法让它发挥作用。
目前,我在 DidFinishLaunchingWithOptions 下的 AppDelegate.m 上检测到设备的大小,然后加载情节提要并设置 initialViewController。当我运行 iPad 模拟器时,它会正确运行并显示,并在控制台中返回正确的故事板和视图控制器。但是,当我运行 iPhone 模拟器时,它会显示 iPad 故事板,但在控制台中,它显示它返回了 iPhone 故事板和正确的初始视图控制器。
另外,我是 Objective-C 的新手,但已经掌握了窍门。谢谢,希望有人可以帮忙。 这是我的 App Delegate 代码。
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
UIStoryboard *storyboard = self.window.rootViewController.storyboard;
UIViewController *rootViewController;
// detect screen height
int height = [UIScreen mainScreen].fixedCoordinateSpace.bounds.size.height;
NSLog(@"The fixed height is %i", height);
// determine if this is an iPad
if (UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad) {
// it's an iPad
if (height >= 1024) {
storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"loginViewController"];
NSLog(@"LOADING MAIN/IPAD STORYBOARD");
}
} else {
// it's an iPhone
storyboard = [UIStoryboard storyboardWithName:@"iPhone" bundle:nil];
rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"iPhoneLoginViewController"];
NSLog(@"LOADING IPHONE STORYBOARD");
}
NSLog(@"Root View Controller: %@", rootViewController);
NSLog(@"Storyboard: %@", storyboard);
self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible];
return YES;
}
#pragma mark - UISceneSession lifecycle
- (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return [[UISceneConfiguration alloc] initWithName:@"Default Configuration" sessionRole:connectingSceneSession.role];
}
- (void)application:(UIApplication *)application didDiscardSceneSessions:(NSSet<UISceneSession *> *)sceneSessions {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}
@end
这是我的 Info.plist:
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
<string>Main</string>
<key>UIMainStoryboardFile~ipad</key>
<string>Main</string>
<key>UIMainStoryboardFile~iphone</key>
<string>iPhone</string>
<key>UIRequiredDeviceCapabilities</key>
以下是 iPad VS iPhone 结果的截图:
iPad Simulator with its correct storyboard
【问题讨论】:
-
能否提供源代码?
-
我建议您对 iPhone 和 iPad 使用相同的类 (h&m) 以实现可重复性。在不同的故事板中没有问题
-
您需要发布 SceneDelegate 和 AppDelegate 的完整代码,以便我们看到您在做什么。作为旁注 - 您展示的图像可以通过自动布局和单个故事板轻松管理。
-
整个项目的源码?好一点,我将为两者使用相同的课程。谢谢! @AnisMansuri
-
我添加了整个 App Delegate 代码。另外,对于 SceneDelegate 我并没有搞砸它。它是空的。我需要在那里配置一些东西吗?是的,我会再次尝试进行自动布局。谢谢! @DonMag
标签: ios objective-c xcode uistoryboard