以编程方式呈现 ViewController 时导航栏未加载

NavigationBar not loading when ViewController is programatically presented

我正在尝试从一个导航控制器 (BasicSearchController) 加载另一个 (ViewController)。在 Storyboard 中,我创建了 2 个 Navigation Controller,它们不是由 segues 链接的,而是独立的 Nagivation 控制器。每个都分别链接到 ViewController 和 BasicSearchController,每个视图控制器在导航栏中都有自己的标题文本。

ViewController.m 我的应用程序中,我有一个使用此代码显示的导航栏项目:

searchButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"search-icon.png"] landscapeImagePhone:[UIImage imageNamed:@"search-icon.png"] style:UIBarButtonItemStylePlain target:self action:@selector(presentSearch)];

当按钮被按下时,这个方法被调用:

- (void) presentSearch {
    NSLog(@"Search presented!");

    [self performSelector:@selector(pushBasicSearchController) withObject:self afterDelay:0.0];
    }

最后,选择器运行这段代码:

- (IBAction)pushBasicSearchController {
BasicSearchController *basicController = [[BasicSearchController alloc] init];

[self presentViewController:basicController animated:YES completion:NULL];
}

在随后介绍的 BasicSearchController 中,我使用以下代码来生成用户界面:

- (void) createInterface {

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

UIImage *navBackgroundImage = [UIImage imageNamed:@"bar.png"];
[[UINavigationBar appearance] setBackgroundImage:navBackgroundImage forBarMetrics:UIBarMetricsDefault];

backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background1.png"]];
[backgroundView setFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
[backgroundView setContentMode:UIViewContentModeScaleToFill];
[[self view] addSubview:backgroundView];
[[self view] sendSubviewToBack:backgroundView];

underBarGradient = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"gradient-under-bar.png"]];
[underBarGradient setFrame:CGRectMake(0, 64, screenWidth, 3)];
[underBarGradient setContentMode:UIViewContentModeScaleToFill];
[underBarGradient setAlpha:0.5f];
[[self view] addSubview:underBarGradient];
[[self view] bringSubviewToFront:underBarGradient];

menuButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"menu.png"] landscapeImagePhone:[UIImage imageNamed:@"menu.png"] style:UIBarButtonItemStyleDone target:self action:@selector(presentMenu)];

navigationItemMain.leftBarButtonItem = menuButton;

searchButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"search-icon.png"] landscapeImagePhone:[UIImage imageNamed:@"search-icon.png"] style:UIBarButtonItemStylePlain target:self action:@selector(presentSearch)];

navigationItemMain.rightBarButtonItem = searchButton;

NSLog(@"UI created!");

}

underBarGradientbackGroundView 已正确显示。但是,NagivationBar 及其标题文本不可见,并且导航栏项也丢失。

回到ViewController,我在AppDelegate.m中使用了以下代码来自定义导航栏:

I have modified `ViewController`'s navigation bar's appearance using the following code, which is found in `AppDelegate.m`:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"bar.png"] forBarMetrics:UIBarMetricsDefault];
NSShadow* shadow = [NSShadow new];
shadow.shadowOffset = CGSizeMake(1.0f, 1.0f);
shadow.shadowColor = [UIColor clearColor];
[[UINavigationBar appearance] setTitleTextAttributes: @{
                                                        NSForegroundColorAttributeName: [UIColor whiteColor],
                                                        NSFontAttributeName: [UIFont fontWithName:@"Aliquam" size:20.0f],
                                                        NSShadowAttributeName: shadow
                                                        }];
return YES;
}

然而,虽然这适用于 ViewController 的导航栏,但 BasicSearchController 的栏未被此代码修改。

我的查询是:

  1. 如何使用 AppDelegate 中使用的代码自定义 BasicSearchController 的导航栏?
  2. 如何使导航栏项目显示在 BasicSearchController 中?

问题所在:

- (IBAction)pushBasicSearchController {
    BasicSearchController *basicController = [[BasicSearchController alloc] init];

    [self presentViewController:basicController animated:YES completion:NULL];
}

你不是在推动你在展示。您需要使用以下方式推送视图控制器:

[self.navigationController pushViewController: basicController animated:YES];

这将解决缺少的 NavigationBar。

用于自定义 NavigationBar

您最好在视图控制器 .m 文件中自定义单个 NavigationBar,最好是在 viewWillAppear 方法中。您还没有指定您想要的可定制性,所以我会把它留给您想象。

我解决了 :) 我改了

navigationItemMain.rightBarButtonItem = searchButton;

self.navigationItem.rightBarButtonItem = searchButton;

在目标视图控制器 (basicSearchController) 中。我也用代码

[basicController.navigationItem setHidesBackButton:YES];
[basicController.navigationItem setTitle:@"Find Duo Partners"];

在 SASmith 提供的代码中按下控制器后立即隐藏后退按钮,并向导航栏添加标题。您可以在目标视图控制器中使用此代码,但它会使按钮弹出,而不是立即出现。

感谢您的帮助!