I added a toolBar with this code:
- (void)viewWillAppear:(BOOL)animated {
UIBarButtonItem *yesterday = [[UIBarButtonItem alloc]initWithTitle:#"Yesterday"
style:UIBarButtonItemStyleBordered target:self action:#selector(yesterday:)];
UIBarButtonItem *today = [[UIBarButtonItem alloc]initWithTitle:#"Today"
style:UIBarButtonItemStyleDone target:self action:#selector(today:)];
UIBarButtonItem *tomorrow = [[UIBarButtonItem alloc]initWithTitle:#"Tomorrow"
style:UIBarButtonItemStyleBordered target:self action:#selector(tomorrow:)];
UIBarButtonItem *month = [[UIBarButtonItem alloc]initWithTitle:#"Month"
style:UIBarButtonItemStyleBordered target:self action:#selector(month:)];
NSArray *items = [NSArray arrayWithObjects:yesterday,today,tomorrow,month, nil];
[yesterday release];
[today release];
[tomorrow release];
[month release];
UIToolbar *toolbar = [[UIToolbar alloc] init];
[toolbar sizeToFit];
[toolbar setFrame:CGRectMake( 0, 20, 320, 40)];
[toolbar setItems:items];
[self.navigationController.view addSubview:toolbar];
}
but when i change the view using the navigation controller the toolbar stays there...
how can i remove that subview?
UINavigationController has a toolbar built in which is hidden by default. You can display it using [navigationController setNavigationBarHidden:animated:];. You might want to use that instead. Then, before you push a view controller, set that view controller's hidesBottomBarWhenPushed property to true.
The reason your toolbar doesn't go away in this instance is that you're adding it to navigationController's view which displays on top of other views it controls. You could instead add it as a subview of self.
To answer your specific question, though, to remove the toolbar from any superview, use [toolbar removeFromSuperview]. In this case, I would go with the cleaner solution of using the toolbar that is built into navigation controllers.
Related
I'm trying to programmatically add a navigationBar to a tableViewController :
When I come from a PUSH Segue the builder adds a navigation bar with navigation items, like I want.
But the problem is that when i come form a MODAL Segue I try to add a bar with code like this:
UINavigationBar *naviBarObj = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, -260, 320, 44)];
[self.view addSubview:naviBarObj];
UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(cancelButtonPressed)];
UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(doneButtonPressed)];
UINavigationItem *navigItem = [[UINavigationItem alloc] initWithTitle:#"Navigation Title"];
navigItem.rightBarButtonItem = doneItem;
navigItem.leftBarButtonItem = cancelItem;
naviBarObj.items = [NSArray arrayWithObjects: navigItem,nil];
....
- (void) cancelButtonPressed { }
- (void) doneButtonPressed { }
But nothing appears... No top bar.
naviBarObj's frame ypoint is -260, maybe this is making nothing appear.
as my title, how do i add in a rightbarbuttontiem in a specific view of tabbarcontroller ?
i'm using storyboard to create the tabarcontroller.
i tried this but the item are not displayed
UIBarButtonItem *settingButton = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"gear.png"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(pushToSetting)];
//BarButtonItem *settingButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(insertNewObject)];
self.navigationItem.rightBarButtonItem = settingButton;
Are you using a NavigationController? If you are, then your code should work. If you are using just a UINavigationBar, then you have to set the items property for the UINavigationBar. As you are not getting a rightbuttonitem, I assume you are using a UINavigationBar. This code should work:
UIBarButtonItem *settingButton = [[UIBarButtonItem alloc] initWithTitle:#"Settings" style:UIBarButtonItemStylePlain target:self action:nil];
UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:#"Test"];
navItem.rightBarButtonItem = settingButton;
NSArray *navItems = [NSArray arrayWithObject:navItem];
[self.navBar setItems:navItems];
You should have the rightbarbuttonitem now. Here, navBar is a UINavigationBar outlet. You can also pop and push navigation items onto a UINavigationBar using these methods:
(void)pushNavigationItem:(UINavigationItem *)item animated:(BOOL)animated;
(UINavigationItem *)popNavigationItemAnimated:(BOOL)animated;
I know how to add a UISegmentedControl to a UIToolBar from within IB, but I am trying to do the same programmatically, because I am using a custom subclass of UISegmentedControl with doesn't have an XIB.
This is the code for the UISegmentedControl:
SVSegmentedControl *navSC = [[SVSegmentedControl alloc] initWithSectionTitles:[NSArray arrayWithObjects:#"List", #"Calendar", nil]];
navSC.delegate = self;
[self.view addSubview:navSC];
[navSC release];
navSC.center = CGPointMake(160, 70);
I was thinking of doing something like [self.toolbar addSubview:navSC], but that didn't show anything.
You need to use the UIToolbar method – setItems:animated: (detailed in the documentation):
UIBarButtonItem *segItem = [[UIBarButtonItem alloc] initWithCustomView:navSC];
UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:NULL];
[toolBar setItems:[NSArray arrayWithObjects:spaceItem,segItem,spaceItem,nil] animated:YES];
[segItem release];
[spaceItem release];
I have a typical UINavigationController scheme but I have many many views which can mean you can end up having quite a few views stacked on top of each other. I want to provide a home button and originally I was going to put that on the right hand side of the navigation bar; however, I have a search bar there and another button so I am hoping to put it next to the back button.
In ascii art:
< Back | |Home| Title |Browse| [Search Bar]
Now I tried to set the backButtonItem of the previous view controller using the following:
UIToolbar* toolbar = [[UIToolbar alloc]
initWithFrame:CGRectMake(0, 0, 100, 45)];
[toolbar setBarStyle: UIBarStyleBlackOpaque];
// create an array for the buttons
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];
UIBarButtonItem *back = [[UIBarButtonItem alloc] init];
back.title = #"Back";
// create a standard save button
[buttons addObject:back];
[back release];
// create a spacer between the buttons
UIBarButtonItem *spacer = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil
action:nil];
[buttons addObject:spacer];
[spacer release];
UIBarButtonItem *homeButton = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"home32.png"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(onHomeButton:)];
homeButton.style = UIBarButtonItemStyleBordered;
[buttons addObject:homeButton];
[homeButton release];
// put the buttons in the toolbar and release them
[toolbar setItems:buttons animated:NO];
[buttons release];
// place the toolbar into the navigation bar
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]
initWithCustomView:toolbar];
[toolbar release];
but that doesn't seem to replace the standard back button. If I just try one UIBarButtonItem and set the backButton, that works.
Now the other approach I tried is that on the View that is pushed on the stack, to set the leftBarButtonItem but I can't seem to find a way to create a back button that has the pointy shape to the left.
Any suggestions?
can you try
self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]
initWithCustomView:toolbar];
good luck
I am trying to put this in a navigationbar, but doesnt show up, can u
have a look at it?
UISegmentedControl *seg1 = [[UISegmentedControl alloc]
initWithItems:[NSArray arrayWithObjects:#"von mir", #"alle", nil]];
[seg1 setSegmentedControlStyle:UISegmentedControlStyleBar];
UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:seg1];
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:self action:nil];
[self.navigationController.navigationBar setItems:[NSArray
arrayWithObjects:flexItem, barItem, flexItem, nil]];
[flexItem release];
[barItem release];
[seg1 release];
UINavigationBar's items property only accepts an array of UINavigationItem objects, not UIBarButtonItem objects. You can't configure a navigation bar the same way as you do a UIToolbar. Instead, in your view controller, do this:
UISegmentedControl * seg1 = [[UISegmentedControl alloc]
initWithItems:[NSArray arrayWithObjects:#"von mir", #"alle", nil]];
[seg1 setSegmentedControlStyle:UISegmentedControlStyleBar];
self.navigationItem.titleView = seg1;
This adds the segmented control to the title view of your view controller's navigation item, which is a custom view that appears centered on the navigation bar.