0

I need to show a simple popover view in which a navigation bar with a done button and a picker view is to be shown. i am doing everything programmatically and code is shown below.

CGRect displayFrame = CGRectMake(0, 0, 300.0f,500.0f);
//((CGFloat)[self.pocModelData.arrayOfDistricts count] + 25)

UIView *popoverView = [[UIView alloc] initWithFrame:displayFrame];
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0,0,popoverView.frame.size.width,20)];

UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:@"some title"];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissPopoverAnimated:)];
navItem.rightBarButtonItem = doneButton;
navBar.items = [NSArray arrayWithObjects:navItem, nil];

[popoverView addSubview:navBar];

CGRect displayFramePopOver = CGRectMake(0, 0, 600.0f,((CGFloat)[self.pocModelData.arrayOfDistricts count] - 25));

UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:displayFramePopOver];
[popoverView addSubview:pickerView];

UIViewController *tvc = [[UIViewController alloc] init];
//tvc.view = popoverView;
[tvc setView:popoverView];
self.popvc = [[UIPopoverController alloc] initWithContentViewController:tvc];
self.popvc.delegate = self;

//[popvc setPopoverContentSize:CGSizeMake(400, 200)];
[self.popvc presentPopoverFromRect: anchor.frame inView: anchor.superview permittedArrowDirections: UIPopoverArrowDirectionAny animated: YES];

Unfortunately since i dont have a reputation of 10, i cant post the result of the execution of code, so i will try to explain what is shown. Popover is shown but with a view which has a black rectangle shown over what seems to be the navy blue background.What i am seeking is how to figure out setting the size of the view and popover correctly, so that it shows proper popover with a navigation bar and a uipickerview.

MJN
  • 10,748
  • 1
  • 23
  • 32
ggthedev
  • 47
  • 8
  • I am doing this via storyboard, and could have done using segues etc, but i am targeting to learn dynamic object creation and associated functionality. I have tried my best not to replicate any question, but in case moderators find it a duplicate, apologies in advance. – ggthedev May 23 '13 at 19:06

1 Answers1

0

You are going through this in quite an odd way. Here is what you should do. Instead of making your own navigation bar, just use the one that comes with UINavigationController.

1.) create a navigationController, call it navC
2.) create a viewController, call it vC
3.) create a pickerView, call it pV

4.) set vC up with navigation bars and such using property self.navigationItem.rightBarButtonItem
5.) insert pV into vC -> [vC.view addSubview:pV]
6.) insert vC into navC -> navC.rootViewController = vC

7.) insert navC into the popOver -> self.popvc = [[UIPopoverController alloc] initWithContentViewController:navC];

8.) present the view.

Profit. (P.S. Do not name your variable like that. name it long and descriptive)

Byte
  • 2,920
  • 3
  • 33
  • 55
  • i would try your way, but in the mean time, can u let me know where am i going wrong since as in is it the order of insertion or is it CGRectMake call which is wrong. – ggthedev May 23 '13 at 19:22
  • @Byte.umm...i tried ur solution, but i am getting the same result, good thing is i got more clarity on what i want to know. How can i set the size of the popvercontroller dynamically. basically i want to dynamically set the size of the popover to fit in pickerview in which number of rows could be different in runtime, so its height would be changing and that should be taken care dynamically as well...hope i have conveyed my concern. – ggthedev May 23 '13 at 19:41
  • Well, first off, UIPickerView has pretty much a same height no matter how many rows you have. So, in this case one size fits all. Ignoring that fact, you can set the popover content size to be whatever by `[self.popvc setPopoverContentSize:ANY_SIZE]`, which in this case ANY_SIZE = `pV.bound.size`. Now, you want to make sure that your pV is already allocated and sized up properly before asking for its size. – Byte May 23 '13 at 20:36