BookController в кулинарной книге Developer iOS 5 (Эрика Садун) не начинается правильно в режиме ландшафта
-
27-10-2019 - |
Вопрос
Проблема, которую я пытаюсь решить:
Я использовал главу 05 Пример 06 BookController в главе 5 (Страницы C05/06 в https://github.com/erica/ios-5-cookbook.git ) в проекте, который имеет UinaVigatioController в качестве rootViewController, но он не запускается правильно в режиме ландшафта. Если вы поворачиваете портрет устройства и возвращаетесь в ландшафт, он начинает работать.
Чтобы показать ошибку изменить пример -тестовый дилегат в main.c со следующим:
#pragma mark Application Setup
@interface TestBedAppDelegate : NSObject <UIApplicationDelegate>
{
UIWindow *window;
}
@end
@implementation TestBedAppDelegate
TestBedViewController *tbvc;
UINavigationController *navigationController;
- (void) pushBookController: (UIGestureRecognizer *) recognizer
{
[navigationController pushViewController:tbvc animated:YES];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
tbvc = [[TestBedViewController alloc] init];
UIViewController *start = [tbvc controllerWithColor:[UIColor whiteColor] withName:@"white"];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushBookController:)];
[start.view addGestureRecognizer:tap];
navigationController = [[UINavigationController alloc] initWithRootViewController:start];
window.rootViewController = navigationController;
[window makeKeyAndVisible];
return YES;
}
@end
Мое приложение запускает ландшафт, поэтому проблема в том, что контроллер книги не распознает режим ландшафта при запуске (если вы вращаетесь после запуска, он будет работать). Попытайся.
Чтобы распознать ландшафт в начале, замените в BookController следующее метод
// Entry point for external move request
- (void) moveToPage: (uint) requestedPage
{
//[self fetchControllersForPage:requestedPage orientation:(UIInterfaceOrientation)[UIDevice currentDevice].orientation];
[self fetchControllersForPage:requestedPage orientation:self.interfaceOrientation];
}
и добавьте следующее:
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {return YES;}
Теперь проблема в том, что BookController
Не появляется в начале, но опять же, если вы вращаетесь, это начинает работать.
Решение
Наверное, вы уже решили это, но попробуйте изменить экземпляр книги так:
+ (id) bookWithDelegate: (id) theDelegate
{
NSDictionary *options = nil;
//Check the orientation
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
//Setting Spine Location to middle for Landscape orientation
options = [NSDictionary dictionaryWithObject:
[NSNumber numberWithInteger:UIPageViewControllerSpineLocationMid]
forKey: UIPageViewControllerOptionSpineLocationKey];
}
BookController *bc = [[BookController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:options];
bc.dataSource = bc;
bc.delegate = bc;
bc.bookDelegate = theDelegate;
return bc;
}
Обратите внимание, что я добавляю проверку по статус -барориентации, чтобы установить позвоночник.
По сути, вам нужно проверить ориентацию, когда вы создаете UipageViewController, и я использовал статус -барориентацию.
О, и не забудьте убедиться, что вы получаете правильную ориентацию
- (BOOL) useSideBySide: (UIInterfaceOrientation) orientation
{
BOOL isLandscape = UIInterfaceOrientationIsLandscape(orientation);
// in case the UIInterfaceOrientation is 0.
if (!orientation) {
isLandscape = UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]);
}
return isLandscape;
}
PS: метод BookWithDelegate соответствует классу BookController в примере, упомянутом в вопросе.