jjzjj

ios - SWRevealViewController FrontViewController 未加载,卡在黑色,其他 View 正常

coder 2024-01-14 原文

我正在尝试将我们的应用程序转换为使用 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 来自其他地方 - 我很确定它不是)。

对可能导致此问题的原因有任何想法吗?

最佳答案

我遇到了同样的事情。我在查看示例代码时修复了它。

  1. 在你的 Storyboard 中添加一个 UIViewController
  2. 选择它并在 Identity Inspector 中使用“SWRevealViewController”作为类
  3. 将 UITableViewController 添加到您的 Storyboard
  4. 现在选择您之前添加的 ViewControler 并右键单击 - 在您的 TableViewController 上画一条线
  5. 选择“显示 View Controller 设置 Controller ”
  6. 点击新添加的 Segue 并在 Attribute Inspector 中将标识符更改为“sw_rear”
  7. 将任何自定义 ViewController(例如“MyViewController”)添加到 Storyboard
  8. 选择它,然后转到菜单->编辑器->嵌入->导航 Controller
  9. 现在应该会出现一个新的导航 Controller
  10. 再次右键单击 - 从第一个 ViewController 到新的 NavigationController 画一条线
  11. 再次选择'reveal view controller set controller'
  12. 现在将这个新 Segue 的标识符设置为“sw_front”

现在您有了基本设置,在运行您的应用程序时,您应该会看到您的自定义 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/

有关ios - SWRevealViewController FrontViewController 未加载,卡在黑色,其他 View 正常的更多相关文章

随机推荐