【问题标题】:NavigationBar not loading when ViewController is programatically presented以编程方式呈现 ViewController 时导航栏未加载
【发布时间】:2015-06-11 11:37:33
【问题描述】:

我正在尝试从另一个 (ViewController) 加载一个导航控制器 (BasicSearchController)。在 Storyboard 中,我创建了 2 个导航控制器,它们不是由 segue 链接的,而是独立的导航控制器。每个都分别链接到 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中?

【问题讨论】:

    标签: ios objective-c uinavigationcontroller uinavigationbar viewcontroller


    【解决方案1】:

    这是问题:

    - (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 方法中。你还没有指定你想要的可定制性,所以我会留下你的想象力。

    【讨论】:

    • 这解决了缺少的操作栏。我想要的 BasicSearchController 的自定义导航栏是左侧的菜单按钮,右侧的搜索按钮。我为此使用了 ViewController 中的相同代码(createInterface 方法),但是,出现的只是返回按钮,它自己创建,而不是我编写的。此外,导航栏的标题不存在。如何删除后退按钮,并让菜单和搜索按钮出现在其位置?另外,我怎样才能让导航栏的标题出现?这仍然是缺失的。谢谢。
    • 另外,viewWillLoad 方法是什么意思?这是 viewWillAppear 还是 viewDidLoad?我已经在viewDidLoad中调用了createInterface,但是正如我在之前的评论中所说,它并没有添加导航栏项。
    • 没关系,我已经解决了 :) 我更改了 'navigationItemMain.rightBarButtonItem = searchButton;'到'self.navigationItem.rightBarButtonItem = searchButton;'。我还使用了代码'[basicController.navigationItem setHidesBackButton:YES]; [basicController.navigationItem setTitle:@"Find Duo Partners"];'在您给我的代码中按下控制器后立即隐藏后退按钮并向导航栏添加标题。感谢您的帮助!
    【解决方案2】:

    我已经解决了:)我改变了

    navigationItemMain.rightBarButtonItem = searchButton;
    

    self.navigationItem.rightBarButtonItem = searchButton;
    

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

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

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

    感谢您的帮助!

    【讨论】:

    • 我想我解决了你的问题,因为它是关于导航栏不可见的问题。既然你承认我解决了它,请接受我的回答并投票。谢谢。很高兴我能帮上忙。
    • 如果您不希望动画在您推送时使用animated: NO
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多