gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 12:20:36 25 4
gpt4 key购买 nike

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

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

您不是在插入,而是在展示。您需要使用以下方式推送 View Controller :

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

这将解决丢失的 NavigationBar。

用于自定义 NavigationBar

您最好在 View Controller .m 文件中自定义一个单独的 NavigationBar,最好是在 viewWillAppear 方法中。您还没有指定您想要什么样的可定制性,所以我将把它留给您想象。

关于ios - 以编程方式呈现 ViewController 时导航栏未加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30763949/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com