【问题标题】:Navigation bar is removed when pushing a viewcontroller from another storyboard从另一个故事板推送视图控制器时,导航栏被删除
【发布时间】:2015-09-24 15:31:36
【问题描述】:

我的项目中有许多故事板,其中包含具有特定功能的模块。在我的一个故事板中,我有一个导航控制器,它推动另一个故事板的初始视图控制器。当我按下它时,导航栏会被移除,并且无法恢复这个特定的栏。当我在新的故事板中放置另一个导航控制器时,导航栏会重置为一个丑陋的过渡(它来自右侧并替换旧的)。

这就是我推动新故事板的视图控制器的方式:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:aStoryboardName bundle:nil];
UIViewController *SideBarInitialViewController = [storyboard instantiateInitialViewController];

UIStoryboardSegue *segue = [[GDFromRightCustomSegue alloc] initWithIdentifier:aSegueIdentifier source:aSource destination:SideBarInitialViewController];
[segue perform];

我还执行了自定义转场以摆脱基本动画(来自下方的视图):

UIView *sourceView = [self.sourceViewController view];
UIView *destinationView = [self.destinationViewController view];

CGFloat screenHeight = [[UIScreen mainScreen] bounds].size.height;
CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width;

destinationView.frame = CGRectMake(screenWidth, 0.0, screenWidth, screenHeight);

UIWindow *window = [[UIApplication sharedApplication] keyWindow];
[window insertSubview:destinationView aboveSubview:sourceView];

[UIView animateWithDuration:0.4 animations:^{
    destinationView.frame = CGRectOffset(destinationView.frame, -screenWidth, 0.0);
    sourceView.frame = CGRectOffset(sourceView.frame, -screenWidth, 0.0);

} completion:^(BOOL finished){
    [self.sourceViewController presentViewController:self.destinationViewController animated:false completion:nil];
}];

有没有办法在不为导航栏设置动画的情况下推送新的故事板,或者更好的是,在我的新故事板中使用旧的导航控制器?

【问题讨论】:

    标签: ios objective-c iphone cocoa-touch uinavigationcontroller


    【解决方案1】:

    在您的源故事板中拖动一个新的 UIViewController,然后将一个 ** Modal segue** 从您的 source ViewController 拖动到这个新的 UIViewController .单击此 segue,然后在属性检查器中添加 segue 的标识符(比如说“newSegue”)标识符必须是唯一的。还将 segue 类型设置为“Custom”并在 Segue Class

    中写入“newSegue

    现在在 UIStoryBoardSegue 上添加一个名为 newSegue 的类别,并在 .m 文件中编写这两个方法。

    - (id) initWithIdentifier:(NSString *)identifier
                       source:(UIViewController *)source
                  destination:(UIViewController *)destination
    {
        return [super initWithIdentifier:identifier
                                  source:source
                             destination:[[UIStoryboard storyboardWithName:@"YourDestinationStoryBoardName" bundle:nil] instantiateInitialViewController ]];
    }
    
    - (void)perform
    {
    
        UIViewController *source = (UIViewController *)self.sourceViewController;
    
       [source presentViewController:self.destinationViewController
                                              animated:YES
                                            completion:Nil];
    
    }
    

    现在在你的源视图控制器中,当你想展示新的视图控制器时,写下这一行

    [self performSegueWithIdentifier:@"newSegue" sender:nil];
    

    必须记住在上述函数中将“YourDestinationStoryBoardName”替换为您的实际目的地故事板名称。

    试试这个希望这会正常工作。

    【讨论】:

    • 感谢您的回答!这项工作非常好,但是有没有办法不将其作为堆栈(使用“后退”按钮)?我的意思是,我想要确切的古代导航控制器
    • 是的,为此您可以使用 Modal Segue 我正在编辑答案,以便您可以再次查看。使用这一行 [source presentViewController:self.destinationViewController animated:YES completion:Nil];跨度>
    • 使用这一行 [source presentViewController:self.destinationViewController animated:YES completion:Nil];在执行方法中
    • 当呈现视图控制器时,导航控制器被擦除!真的是模态转场吗?
    • 是的,当您以模态方式呈现 ViewController 时,导航控制器将被删除,因此如果您想保留导航控制器,那么您应该使用 Push Segue。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-01
    • 2015-11-26
    • 1970-01-01
    • 1970-01-01
    • 2018-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多