I am trying to show the UIToolBar in the RootView of a UISplitView application, the code is the following:
self.navigationController.toolbarHidden = NO;
UIBarButtonItem *refreshItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
target:self
action:@selector(refresh:)];
self.toolbarItems = [NSArray arrayWithObjects:refreshItem, nil];
[refreshItem release];
However, what I see is:
There's black bar on top (I don't know where this came from, I don'开发者_开发技巧t need this) also the bar at the bottom, is there a way to resize it?
What I want is to get something like this:
Using something like this you can add a bar button item to the top of the controller:
UIBarButtonItem *refreshItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refresh:)];
self.navigationItem.leftBarButtonItem = refreshItem;
[refreshItem release];
You will make the button appear in the main view controller's title bar, as it's meant to be.
If you want to make the button appear in the bottom of the navigation controller you could try using this approach, instead:
UIBarButtonItem *refreshItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refresh:)];
[self setToolbarItems:[NSArray arrayWithObjects:refreshItem, nil animated:YES]];
[self.navigationController setToolbarHidden:NO]; //optional, don't remember if it's required ...
[refreshItem release];
For this piece of code to work correctly the side controller has to be a UINavigationController, otherwise you wouldn't be able to create and handle the toolbar. I tried this approach in a clean project and the toolbar renders perfectly.
I had the same issue and Just fixed it, Due to moving the code out of the Viewdid Load to lower down the Page,
As I had previous put in
- (UIBarButtonItem *)barButtonItem {
Moving the Code You used to under that, Worked and fixed the issue
Stewart
Just a note for anybody else who stumbles upon this question. I was having the same issue as adit. The problem turned out to be I was setting up and unhiding the toolbar in the viewDidLoad
method instead of the viewWillAppear
method. Those gaps are caused by setting up the toolbar before the view knows it's being displayed in landscape mode.
The safest and easiest solution is to setup the UINavigationController
to display the toolbar and navigation bar in Interface Builder.
If it looks as expected in IB, it is very unlikely it will change at run-time.
If the toolbar is to be shown/hidden when navigating you should add the cod to do so in viewWillAppear:
and allways call the super implementation, or unexpected things may occurs. Something like this tends to give the best results in a consistent manner:
-(void)viewWillAppear:(BOOL)animated;
{
[super viewWillApplear:animated];
[self.navigationController setToolbarHidden:NO
animated:animated];
}
Also make sure to show/hide the toolbar as need in viewWillAppear:
for all view controllers in your navigation stack for best result.
精彩评论