gpt4 book ai didi

ios - 在没有导航 Controller 的情况下向 View 添加导航栏

转载 作者:IT王子 更新时间:2023-10-29 08:05:34 27 4
gpt4 key购买 nike

我有一个侧边菜单,它可以滑出以显示表格 View ,并且从那里我有使用显示 View Controller 的转场。 segue 必须直接连接到 View Controller ;我不能使用导航 Controller 。

如何在没有导航 Controller 的情况下添加带有栏按钮项的导航栏?

the story board has the navigation item

the app does not

最佳答案

虽然有几种聪明的方法可以回答您的问题。我只是以编程方式解决了它,并在我的 viewWillAppear 中编写了以下代码(注意 - viewDidLoad 也可以,但不建议)-

-(void) viewWillAppear:(BOOL)animated {    UINavigationBar *myNav = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];    [UINavigationBar appearance].barTintColor = [UIColor lightGrayColor];    [self.view addSubview:myNav];    UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc] initWithTitle:@"Cancel"                                                                   style:UIBarButtonItemStyleBordered                                                                  target:self                                                                  action:nil];    UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithTitle:@"Done"                                                                 style:UIBarButtonItemStyleBordered                                                                target:self action:nil];    UINavigationItem *navigItem = [[UINavigationItem alloc] initWithTitle:@"Navigation Title"];    navigItem.rightBarButtonItem = doneItem;    navigItem.leftBarButtonItem = cancelItem;    myNav.items = [NSArray arrayWithObjects: navigItem,nil];    [UIBarButtonItem appearance].tintColor = [UIColor blueColor];}

So, you have a white navigation bar with blue bar button items without a Navigation controller. Again, there are other ways to implement it in your case. Hope, this was helpful.

Output -

enter image description here

Update -

To add images -

UIImageView *myImage = [[UIImageView alloc] initWithFrame:CGRectMake(0,10,32,32)];
[myImage setImage:[UIImage imageNamed:@"image.png"]];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:myImage];
[self.view addSubview:myImage];

关于ios - 在没有导航 Controller 的情况下向 View 添加导航栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23859785/

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