I added toolbar to on screen of navigation based application using the following code
//Create an array to hold the list of bar button items
NSMutableArray *items = [[NSMutableArray alloc] initWithCapacity:3];
//Add buttons
//load the image
UIImage *buttonImage ;
buttonImage = [UIImage imageNamed:@"test.png"];
//creat开发者_Python百科e the button and assign the image for window width and level
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:@selector(WWL:) forControlEvents:UIControlEventTouchUpInside];
[button setImage:buttonImage forState:UIControlStateNormal];
//set the frame of the button to the size of the image (see note below)
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
//create a UIBarButtonItem with the button as a custom view
WindowWidthZoom = [[UIBarButtonItem alloc] initWithCustomView:button ];
[items addObject:WindowWidthZoom ];
[self setToolbarItems:items];
[[self navigationController] setToolbarHidden:NO animated:NO];
but when leave the screen , I noticed that the toolbar didn't disappear in the other screens , any suggestion how to hide it before leaving this screen to avoid its presence in other screens , and how to change its color to black
You might add the following to the -viewWillAppear method of all your other view controllers:
[self.navigationController setToolbarHidden:YES animated:NO];
In the view controller where you want the toolbar to appear, make sure you setToolbarHidden to NO also in the -viewWillAppear method. Doing so in the -viewDidLoad method is not enough because this method is not called every time a view appears. For example, when you hit the Back button of a navigation controller and return to a previous view controller, since that view controller has already been loaded, it may not need to load again (and so -viewDidLoad will not be called.)
Please change your last line code:
[[self navigationController] setToolbarHidden:NO animated:NO];
to:
[self setToolbarHidden:NO animated:NO];
And make sure call it in:
- (void)viewDidAppear:(BOOL)animated;
精彩评论