问题
I am trying to place a UIToolbar at the top of a UIPopoverController in much the same way as Apple have done in pages and keynote with the "media", "tables", "charts" "shapes" toolbar.
I've managed to place one at the bottom using the same method described as in this post but I'm unable to move it to the top.
I can do it by just placing a normal UIToolbar within one of the child view controllers however the toolbar does not seem to follow the same line as the border and doesn't look as nice as it does in the apple apps.
Does anyone have any suggestions on how to do this, or if it's even possible? Any help would be much appreciated.
Thanks!
回答1:
Use a UINavigationController that holds just your one ViewController as the popoverController's content, like so:
MyViewController *myVC = [[myViewController alloc] init];
UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:myVC];
myPopoverController = [[UIPopoverController alloc] initWithContentViewController:navCon];
[myVC release];
[navCon release];
[myPopoverController presentPopoverFromRect:rect
inView:view
permittedArrowDirections:UIPopoverArrowDirectionRight
animated:YES];
Then, in your viewController's init, set the navigationController's items:
self.navigationItem.title = @"myTitle";
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editButtonTapped:)] autorelease];
You can go crazy and put whatever you want in there:
self.navigationItem.titleView = [[MySpecialTitleView alloc] initWithFrame...];
Your navigation controller won't be navigating if there is only one ViewController on its stack.
iOS 7 caveat: NavigationBars inside popovers seem to ignore tint in iOS 7. I think that's a bug and encourage you to file a bug report with apple if you encounter this issue.
回答2:
You can put a toolbar on top of your UIPopoverController.
More precisely you have to put it on top of the UIViewController that will be set as the content of your UIPopoverController when calling initWithContentViewController.
If you are using IB:
1) drag a UItoolbar to the top of your UIViewController
2) select the UIToolbar, go to the size inspector and in the autosizing section:
- click on the top indicator to fix the top margin
- click on the bottom indicator to make the bottom margin flexible
In the little preview window the red bar should stay on top
来源:https://stackoverflow.com/questions/6599918/uipopovercontroller-toolbar-at-top