SWRevealViewController FrontViewController not loading, stuck as Black, other views fine

主宰稳场 提交于 2019-12-05 04:23:34

问题


I'm trying to transition our app to use SWRevealViewController to give us a side-bar on each side of the application. But despite following the code in one of the example apps, I'm getting an error where the ViewController for the front view doesn't work properly. viewDidLoad gets called, but everything remains black.

Interestingly, if in my viewDidLoad, I set the background colour to red of the view, this is reflected. But stuff in Interface builder from the original story board is not.

The code I use in the AppDelegate is:

- (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;
}

This is the Main.Storyboard which is just for the MainViewController:

When the app loads, the view is just black. But I can drag from both the left and right edge and view the side bars as would be expected. So it's only the FrontView that is coming up black. I stuck an NSLog() into the ViewDidLoad, which appears in the console, as does one in -(void)loadView{}, which shows the View is loading.

If I put into the viewDidLoad a [self.view setBackgroundColor:[UIColor redColor]] this takes effect, meaning it is linked to the view, but it's just the view inside the storyboard is not appearing. Which is weird since the Storyboard also contains a NavigationController, which does appear (I think - unless that navigation controller is coming from somewhere else - which I'm pretty sure it isn't).

Any thoughts on what might be causing this?


回答1:


I had the same thing going on. I fixed it while looking on the Example code.

  1. In your Storyboard add a UIViewController
  2. Select it and in Identity Inspector just use 'SWRevealViewController' as class
  3. Add a UITableViewController to your story board
  4. Now select your previously added ViewControler and right-click-draw a line to your TableViewController
  5. Select 'reveal view controller set controller'
  6. Click on the newly added Segue and in Attribute Inspector change the identifier to 'sw_rear'
  7. Add any custom ViewController (for example 'MyViewController') to the story board
  8. Select it, then go to the Menu->Editor->Embed In->Navigation Controller
  9. Now a new Navigation Controller should appear
  10. Again right-click-draw a line from the first ViewController to your new NavigationController
  11. Again choose 'reveal view controller set controller'
  12. Now set the identifier of this new Segue to 'sw_front'

Now you have a Basic Setup and when running your app, you should see your custom ViewController. Now the Button for the Menu has to be added.

In your ViewControllers .m add following:

@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];
    }
}

Now again switch to the story board. Add a UIBarButtonItem to MyViewController. Again right-click-draw a line from MyViewController to this new item in the NavigationBar. Choose 'revealButtonItem'.

Thats it! Repeat the last steps for every ViewController you want to add. You only have to connect them with right-click-drawing from the TableView to your newly added NavigationController of each of your added ViewControllers. To push the ViewControllers just select 'reveal view controller push controller'.

Hope that helps a bit!



来源:https://stackoverflow.com/questions/27306974/swrevealviewcontroller-frontviewcontroller-not-loading-stuck-as-black-other-vi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!