以编程方式将按钮添加到 UINavigationController
Posted
技术标签:
【中文标题】以编程方式将按钮添加到 UINavigationController【英文标题】:Programmatically add button to UINavigationController 【发布时间】:2014-01-20 08:59:14 【问题描述】:我认为这应该是一个简单的问题,但我只能摸不着头脑。我有一个在故事板中设置了多个 ViewController 的应用程序。我希望它们中的每一个在每个视图的顶部都有相同的导航栏,但是这个导航控制器在所有视图上都有相同的按钮。导航控制器是在 AppDelegate 中设置的,并且按钮被添加到每个 ViewController 的 ViewDidLoad 中(根据我在这里收到的另一个问题的答案),但目前,按钮(和标题)没有显示。
UINavigationController 的设置位置 - AppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
MainViewController* mainVC = [mainStoryboard instantiateInitialViewController];
UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:mainVC];
[self.window setRootViewController:navVC];
[_window makeKeyAndVisible];
return YES;
其中一种观点:
- (void)viewDidLoad
[self setTitle:@"Congress app"]; // < This does not set the title of the NavController
UIBarButtonItem *showSettingsButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(showSettings:)];
UIBarButtonItem *showMenuButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"menuButton.png"] style:UIBarButtonItemStylePlain target:self action:@selector(revealMenu:)];
//self.navigationItem.leftBarButtonItem = showMenuButton;
//self.navigationItem.rightBarButtonItem = showSettingsButton;
self.navigationController.navigationItem.leftBarButtonItem = showMenuButton;
self.navigationController.navigationItem.rightBarButtonItem = showSettingsButton;
UINavigationController *currentNav;
UIViewController *VCname;
NSLog(@"First left button in navigation controller - %@", [self.navigationController.navigationItem.leftBarButtonItems objectAtIndex:0]);
NSLog(@"First right button in navigation controller - %@", [self.navigationController.navigationItem.rightBarButtonItems objectAtIndex:0]);
[super viewDidLoad];
我有几个 NSLogs 显示导航控制器中的内容(如果有的话),它显示:以及哪些暗示正在添加按钮但未显示?
编辑:
我刚刚记得在上面的代码中,有一个 ViewController (InitViewController.m) 在 VC 之前被触发。 NavigationController 分配在这里:
- (void)viewDidLoad
[super viewDidLoad];
self.topViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"MainVC"];
这很可能是问题的原因。
编辑 2:
感谢 Saurav Mac,我已将问题追踪到我试图在“错误”视图控制器上添加按钮。我在 AppDelegate.m 中设置了导航控制器,第一个视图控制器是 InitViewController,然后调用 MainViewController 作为“顶视图控制器”。不敢相信我错过了,感谢您的帮助。
【问题讨论】:
您是否删除了Project Settings > #target# > Deployment Info > Main Interface
上的“主要”[您的故事板名称](因为您正在手动管理初始控制器)?
老兄让我们回到基础,您是否尝试过先添加一个普通的 barbuttonitem,而不是尝试添加一个按钮和图像?让我知道这是否有效:)
@Pavan - 我刚试过:[self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:@"Title" style:UIBarButtonItemStylePlain target:nil action:@selector(showSettings:)]animated :是的];仍然没有运气......
请查看此问题解决您的问题***.com/a/26269894/4188824
【参考方案1】:
您可以在公共类中创建此按钮,即 NSObject 的子类,然后可以在您要添加按钮的每个类中调用此方法,或者您可以将此代码添加到每个类的 ViewDidLoad 方法 -
self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]init];
UIImage *img1=[UIImage imageNamed:@"image.png"];
CGRect frameimg1 = CGRectMake(0, 0, img1.size.width, img1.size.height);
UIButton *signOut=[[UIButton alloc]initWithFrame:frameimg1];
[signOut setBackgroundImage:img1 forState:UIControlStateNormal];
[signOut addTarget:self action:@selector(BackButtonPressed)
forControlEvents:UIControlEventTouchUpInside];
[signOut setShowsTouchWhenHighlighted:YES];
UIBarButtonItem *barButton=[[UIBarButtonItem alloc]initWithCustomView:signOut];
self.navigationItem.leftBarButtonItem=barButton;
【讨论】:
这似乎也不起作用,我已将 'image.png' 更改为我用于其他用途的图像,但它仍然没有显示。 请仔细检查您使用的图片是否为png格式,图片名称需用双引号括起来。 我正在使用:“menuButton.png”。还有,为什么标题不显示?我认为有什么问题是停止显示标题和按钮。 那你的导航栏肯定有问题。仔细跟踪您的导航栏,您是如何为该特定视图分配它的。 您能否将您正在使用的代码发送到分配栏按钮。【参考方案2】:self.navigationController.navigationItem.leftBarButtonItem = showMenuButton;
self.navigationController.navigationItem.rightBarButtonItem = showSettingsButton;
改成
self.navigationItem.leftBarButtonItem = showMenuButton;
self.navigationItem.rightBarButtonItem = showSettingsButton;
【讨论】:
我已经尝试过了(你可以在我的代码中看到它被注释掉了),但是按钮仍然没有显示。【参考方案3】: try This ...
In AppDelegate.h
@property (strong, nonatomic) UINavigationController *nav; //navigationControlller Object
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
UIViewController* mainVC = [mainStoryboard instantiateInitialViewController];
self.nav = [[UINavigationController alloc] initWithRootViewController:mainVC];
[self.window setRootViewController:self.nav];
[_window makeKeyAndVisible];
// Override point for customization after application launch.
return YES;
One of the Views:
- (void)viewDidLoad
[super viewDidLoad];
[self setTitle:@"Congress app"];
UIBarButtonItem *showSettingsButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(showSettings:)];
self.navigationItem.leftBarButtonItem = showSettingsButton;
-(void)showSettings:(id)sender
NSLog(@"showSettings");
【讨论】:
观察代码..以下方法有变化: 1)- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 2)- (void)viewDidLoad 我已将此代码复制到我的身上,但仍然无法正常工作。我什至无法显示标题。 嘿瑞恩..你有什么解决方案吗? 嗨,是的,我最后做到了。请参阅我在问题中的编辑 - 它归结为另一个导航控制器被分配在另一个视图控制器中。【参考方案4】:试试这个:
显示导航控制器的leftBarButtonItem:
UIImage *faceImage = [UIImage imageNamed:@"back_arrow.png"];
UIButton *face = [UIButton buttonWithType:UIButtonTypeCustom];
face.bounds = CGRectMake( 10, 0, faceImage.size.width, faceImage.size.height );
[face addTarget:self action:@selector(handleBack:) forControlEvents:UIControlEventTouchUpInside];
[face setImage:faceImage forState:UIControlStateNormal];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:face];
self.navigationItem.leftBarButtonItem = backButton;
[self.navigationItem setHidesBackButton:YES animated:YES];
[self.navigationItem setLeftBarButtonItem:nil animated:NO];
[self.navigationItem setBackBarButtonItem:nil];
显示导航控制器的rightBarButtonItem:
UIImage *faceImage1= [UIImage imageNamed:@"slideshowarrow"];
UIButton *face1 = [UIButton buttonWithType:UIButtonTypeCustom];
face1.bounds = CGRectMake( 10, 0, faceImage1.size.width, faceImage1.size.height );
[face1 addTarget:self action:@selector(Goto_nextView) forControlEvents:UIControlEventTouchUpInside];
[face1 setImage:faceImage1 forState:UIControlStateNormal];
UIBarButtonItem *backButton1 = [[UIBarButtonItem alloc] initWithCustomView:face1];
self.navigationItem.rightBarButtonItem = backButton1;
也许对你有帮助。
【讨论】:
这对我不起作用,与其他答案一样,我可以看到导航栏,但它是空的——甚至没有标题。 @Ryan 仅供参考,它显示 btn(左/右)而不是导航标题 抱歉,我好像一开始就犯了一个大错误,你发布的代码没问题,但我的程序不是。【参考方案5】:请试试这个...
- (void)viewDidLoad
[super viewDidLoad];
CGRect frameimgback1 = CGRectMake(0, 0, 60, 35);
UIButton *button = [[UIButton alloc] initWithFrame:frameimgback1];
[button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Ravindra" forState:UIControlStateNormal];
UIBarButtonItem *btnL = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = btnL;
【讨论】:
以上是关于以编程方式将按钮添加到 UINavigationController的主要内容,如果未能解决你的问题,请参考以下文章