简体   繁体   English

单击导航栏中的按钮时如何加载UICollectionViewController?

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

I have a class, DisplayOptViewController, which is a subclass of UICollectionViewController. 我有一个类DisplayOptViewController,它是UICollectionViewController的子类。 I want to display this CollectionViewController when the user clicks a button in the Navigation Bar on my current page. 当用户单击当前页面上导航栏中的按钮时,我想显示此CollectionViewController。 I am able to load the CollectionView on button Click but the Navigation Bar is not coming. 我可以在按钮Click上加载CollectionView,但导航栏不会出现。 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. 当我通过情节提要板进行尝试时,不会显示ViewController本身,而当我以编程方式创建视图控制器对象时,却无法获得导航栏。 Any idea how to to this? 任何想法如何做到这一点?

I tried to add this code to my viewDidLoad method in DisplayOptViewController : 我试图将此代码添加到DisplayOptViewController viewDidLoad方法中:

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 我在这里加载UICollectionView

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 至于情节提要,也许您没有正确设置segues,或者属性设置不正确或什么……但是我无法像这样调试它

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, 在应用程序didFinishLaunchingWithOptions方法中,

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. 现在,您的导航栏将出现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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