我正在尝试将我们的应用程序转换为使用 SWRevealViewController 在应用程序的每一侧为我们提供一个侧边栏。但是,尽管遵循其中一个示例应用程序中的代码,但我收到了一个错误,即前视图的 ViewController 无法正常工作。viewDidLoad 被调用,但一切仍然是黑色的。
有趣的是,如果在我的 viewDidLoad 中,我将视图的背景颜色设置为红色,就会反映出来。但是原始故事板中的界面生成器中的东西不是。
我在 AppDelegate 中使用的代码是:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window = window;
MainViewController *frontViewController = [[MainViewController alloc] init];
RearViewController *rearViewController = [[RearViewController alloc] init];
UINavigationController *frontNavigationController = [[UINavigationController alloc] initWithRootViewController:frontViewController];
UINavigationController *rearNavigationController = [[UINavigationController alloc] initWithRootViewController:rearViewController];
SWRevealViewController *revealController = [[SWRevealViewController alloc] initWithRearViewController:rearNavigationController frontViewController:frontNavigationController];
revealController.delegate = self;
RightViewController *rightViewController = rightViewController = [[RightViewController alloc] init];
rightViewController.view.backgroundColor = [UIColor greenColor];
revealController.rightViewController = rightViewController;
//revealController.bounceBackOnOverdraw=NO;
//revealController.stableDragOnOverdraw=YES;
self.viewController = revealController;
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
#pragma mark - SWRevealViewDelegate
- (id <UIViewControllerAnimatedTransitioning>)revealController:(SWRevealViewController *)revealController animationControllerForOperation:(SWRevealControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC
{
if ( operation != SWRevealControllerOperationReplaceRightController )
return nil;
if ( [toVC isKindOfClass:[RightViewController class]] )
{
if ( [(RightViewController*)toVC wantsCustomAnimation] )
{
id<UIViewControllerAnimatedTransitioning> animationController = [[CustomAnimationController alloc] init];
return animationController;
}
}
return nil;
}
这是仅用于 MainViewController 的 Main.Storyboard:
当应用程序加载时,视图只是黑色的。但我可以从左右边缘拖动并按预期查看侧边栏。所以只有 FrontView 会变黑。我将一个 NSLog() 插入到 ViewDidLoad 中,它出现在控制台中,就像 -(void)loadView{} 中的一个一样,它显示视图正在加载。
如果我在 viewDidLoad 中放入一个[self.view setBackgroundColor:[UIColor redColor]]
this 生效,这意味着它链接到视图,但它只是情节提要内的视图没有出现。这很奇怪,因为 Storyboard 还包含一个 NavigationController,它确实出现了(我认为 - 除非该导航控制器来自其他地方 - 我很确定它不是)。
关于可能导致这种情况的任何想法?