Sam*_*her 2 objective-c storyboard ios swrevealviewcontroller
我正在尝试转换我们的应用程序以使用SWRevealViewController为应用程序的每一侧提供一个侧栏.但是,尽管在其中一个示例应用程序中遵循了代码,但我收到的错误是前视图的ViewController无法正常工作.viewDidLoad被调用,但一切都是黑色的.
有趣的是,如果在我的viewDidLoad中,我将背景颜色设置为视图的红色,则会反映出来.但是原始故事板中的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;
}
Run Code Online (Sandbox Code Playgroud)
这是Main.Storyboard,它只适用于MainViewController:

当应用加载时,视图只是黑色.但我可以从左右边缘拖动并按预期方式查看侧边栏.因此,只有FrontView才会出现黑色.我将一个NSLog()插入到ViewDidLoad中,该ViewDidLoad出现在控制台中, - (void)loadView {}中的一个显示View正在加载.
如果我放入viewDidLoad,[self.view setBackgroundColor:[UIColor redColor]]这将生效,这意味着它链接到视图,但它只是故事板内的视图没有出现.这是奇怪的,因为故事板还包含一个NavigationController,它确实出现(我认为 - 除非导航控制器来自其他地方 - 我很确定它不是).
关于可能导致这种情况的任何想法?
我有同样的事情发生.我在查看示例代码时修复了它.
现在您有了一个基本设置,在运行您的应用程序时,您应该看到自定义的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];
}
}
Run Code Online (Sandbox Code Playgroud)
现在再次切换到故事板.将一个UIBarButtonItem添加到MyViewController.再次右键单击 - 从MyViewController中绘制一条线到NavigationBar中的这个新项.选择'revealButtonItem'.
而已!对要添加的每个ViewController重复上一步.您只需将它们从TableView右键单击绘图连接到您添加的每个ViewControllers的新添加的NavigationController.要推动ViewControllers,只需选择"显示视图控制器推送控制器".
希望那有所帮助!