segmentedcontrol in navigationbar - iphone

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.

Related

How to center UISegmentControl in a toolbar at top of UIPopoverController

I've seen a few different posts on this, but I can't seem to get it working. I basically have a UITableView and want sort buttons at the top of a popover controller. I followed this post: UIPopoverController toolbar at top in order to get started. In my controller that is the rootViewController of the navigationController, I can create a UISegmentControl and place it at the top. However, it does not look like the picture in that it's not centered. Maybe because the way I get it into the popover is in the viewDidLoad of the popover like this:
UISegmentedControl *topSegmentControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"One", #"Two", #"Three", #"Four", nil]];
topSegmentControl.backgroundColor = [UIColor clearColor];
topSegmentControl.segmentedControlStyle = UISegmentedControlStyleBar;
UIBarButtonItem *toolBarCustom = [[UIBarButtonItem alloc] initWithCustomView:topSegmentControl];
// UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
self.navigationItem.leftBarButtonItem = toolBarCustom;
Also, if I want to present data at the bottom of the popovercontroller in a toolbar, I'm not sure where to do that. Following the same example: UIPopoverController toolbar at top, I thought in my navigationController, I would do something like this:
UISegmentedControl *topSegmentControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"BottomOne", #"BottomTwo", #"BottomThree", nil]];
// topSegmentControl.backgroundColor = [UIColor clearColor];
topSegmentControl.segmentedControlStyle = UISegmentedControlStyleBar;
UIBarButtonItem *toolBarCustom = [[UIBarButtonItem alloc] initWithCustomView:topSegmentControl];
UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
NSArray *array = [NSArray arrayWithObjects:spaceItem, toolBarCustom, spaceItem, nil];
[navController setToolbarItems:toolBarCustom];
[navController setToolbarHidden:NO];
When I try this, I see a toolBar with nothing in it, that is a lighter tint than the rest of the popover.
To summarize, I'm not sure as to where you would initialize toolbar or barbuttonitems for a popover that has a navigationcontroller like in the example. I'm also not sure how to center the data. Thanks.
You’ll probably be best suited by just setting your UISegmentedControl as the titleView of your root view controller’s navigationItem.

UISegmentedControl Within UIToolBar

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];

Buttons don't appear on the toolbar

A simple question. How do I add toolbar items in combination with a TTLauncherViewController. I must do something very basic wrong, as the toolbar appears, but the button does not show:
self.navigationController.toolbarHidden = NO;
self.navigationController.toolbar.tintColor = [UIColor blackColor];
UIBarButtonItem *updateBttn = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
target:self
action:#selector(updateData:)];
UIBarButtonItem *btnSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil ];
self.navigationController.toolbarItems = [NSArray arrayWithObjects:updateBttn, btnSpacer, nil];
[btnSpacer release];
[updateBttn release];
Could this be the issue?
http://www.iphonedevsdk.com/forum/iphone-sdk-development/22180-navigationcontroller-toolbar-wont-show-any-buttons.html
In short, it says that the toolbar is not a child of the navigation controller but the parent view.

how to add 4 buttons in UInav bar for iphone

i want to add 4 buttons in UINAV bar how to do that
should i make them in UINAVBAR controller or navItem or UIBarbutton???
any code will be appreciated
Thanks
UINavigationBar is used to navigate backward or forward in application. You can't use it for any other functionality. How ever if you want to show four button on top use UIToolBar. A tool bar can multiple button with different functionality.
` create toolbar using new
toolbar = [UIToolbar new];
toolbar.barStyle = UIBarStyleDefault;
[toolbar sizeToFit];
toolbar.frame = CGRectMake(0, 410, 320, 50);
//Add buttons
UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:#selector(pressButton1:)];
UIBarButtonItem *systemItem2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction
target:self
action:#selector(pressButton2:)];
UIBarButtonItem *systemItem3 = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCamera
target:self action:#selector(pressButton3:)];
//Use this to put space in between your toolbox buttons
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
//Add buttons to the array
NSArray *items = [NSArray arrayWithObjects: systemItem1, flexItem, systemItem2, flexItem, systemItem3, nil];
//release buttons
[systemItem1 release];
[systemItem2 release];
[systemItem3 release];
[flexItem release];
//add array of buttons to toolbar
[toolbar setItems:items animated:NO];
[self.view addSubview:toolbar];`

How to remove a Subview from navigationController?

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.