我正在尝试将我们的应用程序转换为使用 SWRevealViewController 在应用程序的每一侧为我们提供侧边栏。但是,尽管遵循了其中一个示例应用程序中的代码,但我还是收到一个错误,其中前 View 的 ViewController 无法正常工作。 viewDidLoad 被调用,但一切仍然是黑色。
有趣的是,如果在我的 viewDidLoad 中,我将 View 的背景色设置为红色,就会反射(reflect)出这一点。但是来自原始 Storyboard的 Interface Builder 中的内容不是。
我在 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:
应用加载时, View 是黑色的。但我可以从左右边缘拖动并按预期查看侧边栏。所以只有 FrontView 变黑了。我将一个 NSLog() 插入到 ViewDidLoad 中,它出现在控制台中,-(void)loadView{} 中也是如此,它显示 View 正在加载。
如果我在 viewDidLoad 中放入一个 [self.view setBackgroundColor:[UIColor redColor]] 这会生效,这意味着它链接到 View ,但它只是 Storyboard 内的 View 不是出现。这很奇怪,因为 Storyboard 还包含一个 NavigationController,它确实出现了(我认为 - 除非该导航 Controller 来自其他地方 - 我很确定它不是)。
对可能导致此问题的原因有任何想法吗?
最佳答案
我遇到了同样的事情。我在查看示例代码时修复了它。
现在您有了基本设置,在运行您的应用程序时,您应该会看到您的自定义 ViewController。现在必须添加菜单按钮。
在您的 ViewControllers .m 中添加以下内容:
@interface MyViewController ()
@property (nonatomic) IBOutlet UIBarButtonItem* revealButtonItem;
@end
- (void)viewDidLoad {
[super viewDidLoad];
[self customSetup];
}
- (void)customSetup
{
SWRevealViewController *revealViewController = self.revealViewController;
if ( revealViewController )
{
[self.revealButtonItem setTarget: self.revealViewController];
[self.revealButtonItem setAction: @selector( revealToggle: )];
[self.navigationController.navigationBar addGestureRecognizer: self.revealViewController.panGestureRecognizer];
}
}
现在再次切换到 Storyboard。将 UIBarButtonItem 添加到 MyViewController。再次右键单击并从 MyViewController 画一条线到 NavigationBar 中的这个新项目。选择“revealButtonItem”。
就是这样!对要添加的每个 ViewController 重复最后的步骤。您只需通过从 TableView 右键单击绘图将它们连接到您添加的每个 ViewController 的新添加的 NavigationController。要推送 ViewController,只需选择“显示 View Controller 推送 Controller ”。
希望对您有所帮助!
关于ios - SWRevealViewController FrontViewController 未加载,卡在黑色,其他 View 正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27306974/