简体   繁体   中英

How to load a UICollectionViewController on clicking a button in the navigation bar?

I have a class, DisplayOptViewController, which is a subclass of UICollectionViewController. I want to display this CollectionViewController when the user clicks a button in the Navigation Bar on my current page. I am able to load the CollectionView on button Click but the Navigation Bar is not coming. I want the user to be able to see a back button in the navigation Bar and clicking the button should take him back to the current page. I tried to do this via storyboard as well as programmatically. When I try this via the Storyboard, the ViewController itself is not displayed and when I create the view controller object programmatically, I am not getting the Navigation Bar. Any idea how to to this?

I tried to add this code to my viewDidLoad method in DisplayOptViewController :

UINavigationBar *navBar=[[UINavigationBar alloc] init];
[[self navigationController] setNavigationBarHidden:NO animated:YES];
[self.navigationController.navigationBar addSubview:navBar];

But the Navigation Bar still didn't come. Kindly help.

update

I am loading the UICollectionView here

UICollectionViewFlowLayout *aFlowLayout = [[UICollectionViewFlowLayout alloc] init];
[aFlowLayout setItemSize:CGSizeMake(140, 50)]; 
[aFlowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
DisplayOptViewController *vc=[[DisplayOptViewController alloc] initWithCollectionViewLayout:aFlowLayout];
[self presentViewController:vc animated:YES completion:nil];

You have a few things that i question,

[self.navigationController.navigationBar addSubview:navBar];

you are adding a navigation bar to a navigation bar.... do this instead

[self.navigationController setNavigationBar:navBar];

second

[self presentViewController:vc animated:YES completion:nil];

you are presenting the controller.... not pushing/poping it....

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

try that in when you do it programmatically

as for the storyboard maybe you don't have the segues set up correctly, or properties not set right or something... but i can't debug it like this

If you have a navigation based project, then you have to initialize the navigation controller in the app delegate itself. Try the code below to make the navigation bar visible,

In application didFinishLaunchingWithOptions method,

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:[self viewController]];
[[self window] setRootViewController:navigationController];
[self.window makeKeyAndVisible];
return YES;

Now your navigation bar will appear.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM