How to add Segmented controll on UIToolBar in iphone - iphone

How to add segmented control on toolbar, I'm adding but not shows.
my code is: these code is on viewDidload
toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0.0, 325.0, 320.0, 44.0)];
toolBar.barStyle = UIBarStyleDefault;
NSArray *segmentItem = [[NSArray alloc]initWithObjects:#"Day",#"List",#"Month", nil];
UISegmentedControl *segmentControll = [[UISegmentedControl alloc]initWithItems:segmentItem];
segmentControll.frame = CGRectMake(80.0, 325.0, 200.0, 30.0);
//[self.toolBar addSubview:segmentControll];
[self.navigationController.toolbar addSubview:segmentControll];
[self.view addSubview:toolBar];

You can add only the UIBarButtonItem's to UIToolbar.
First, add the segmentControll to a bar button item,
UIBarButtonItem *segBarBtn;
segBarBtn = [[UIBarButtonItem alloc] initWithCustomView:segmentControl];
Add the bar button to the tool bar,
NSArray *toolbarItems = [NSArray arrayWithObject:segBarBtn];
[toolbar setItems:toolbarItems animated:NO];

1. self.navigationController.toolbar is not the same as toolBar.
2. You're doing it wrong. You should add segmentControl as UIBarButtonItem:
UIBarButtonItem *btnItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControll];
toolBar.items = [NSArray arrayWithObject:btnItem];

Try something like this
toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0.0, 325.0, 320.0, 44.0)];
toolBar.barStyle = UIBarStyleDefault;
NSArray *segmentItem = [[NSArray alloc]initWithObjects:#"Day",#"List",#"Month", nil];
UISegmentedControl *segmentControll = [[UISegmentedControl alloc]initWithItems:segmentItem];
segmentControll.frame = CGRectMake(80.0, 325.0, 200.0, 30.0);
//[self.toolBar addSubview:segmentControll];
UIBarButtonItem *toolbarItem = [[[UIBarButtonItem alloc] initWithCustomView:segmentedControl] autorelease];
[toolBar addSubview:toolbarItem];
[self.view addSubview:toolBar];

With a regular UIToolbar you do so by making the segmented control a part of a custom UIBarButtonItem like this.
UIBarButtonItem * segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView: segmentControll];
NSMutableArray *itemCopy = [self.toolbar.items mutableCopy];
[itemCopy addObject:segmentBarItem];
self.toolbar.items = itemCopy;
[itemCopy release];
[segmentBarItem release];
But in your case you are trying to modify the toolbar of a navigation controller which is discouraged.
This property contains a reference to the built-in toolbar managed by
the navigation controller. Access to this toolbar is provided solely
for clients that want to present an action sheet from the toolbar. You
should not modify the UIToolbar object directly.
You will still want to create a custom UIBarButtonItem but you will want to set it as the rightBarButtonItem

Related

Centering UIBarButtonItem in a UIToolbar and adding custom text

I need to add a button to the center of the ToolBar. I have done the adding the button to the toolbar part successfully. My problems are as follows;
1.) I need to center this barbutton. It should be in the center of the Tool Bar
2.) I need to have a text after the refresh button image is displayed.
toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0 , 320 , 60)];
NSMutableArray* button = [[NSMutableArray alloc] initWithCapacity:1];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refreshButtonAction:)];
[button addObject:barButton];
[toolBar setItems:button animated:NO];
[self.view addSubview:toolBar];
1. Add flexible spacers before and after your bar button in the toolbar items array:
toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0 , 320 , 60)];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refreshButtonAction:)];
NSArray *toolbarItems = [NSArray arrayWithObjects:flexibleSpace, barButton, flexibleSpace];
[toolBar setItems:toolbarItems animated:NO];
[self.view addSubview:toolBar];
Configuring toolbars is much easier to do in Interface Builder. If your view controller is inside a UINavigationController stack, you can still use IB to create an outlet collection of UIBarButtonItems and set self.toolbarItems in -viewDidLoad.
2. To get custom content in a toolbar you can create a bar button item with a custom view:
UIView *customView = <# anything, could be a UILabel #>;
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithCustomView:customView];
I know this can be done in IB, but I believe if you want to center a button, you will need to add a fixed or flexible space button on either side to keep your button in the middle. If you are going to do this with just code... try and sandwich your button between the 2 space buttons.

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

segmentedcontrol in navigationbar

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.

tintColor of UISegmentedControl doesnt react

Come oon!
I am almost looking a day at this!
I want to set my tint color of my UISegmentedControl but it doesnt react...
NSArray *segControlItems = [NSArray arrayWithObjects:
#"L",
#"H",
#"A",
nil];
UISegmentedControl *segControl = [[UISegmentedControl alloc] initWithItems:segControlItems];
segControl.frame = CGRectMake(0, 0, 90, 30);
segControl.segmentedControlStyle = UISegmentedControlStyleBar;
segControl.momentary = YES;
segControl.tintColor = [UIColor greenColor];
[segControl addTarget:self action:#selector(segAction:) forControlEvents:UIControlEventValueChanged];
UIBarButtonItem *segBarItem = [[UIBarButtonItem alloc] initWithCustomView:segControl];
self.switchView.navigationItem.rightBarButtonItem = segBarItem;
[segControl release];
[segBarItem release];
Has apple made some property like "makeUISegmentedControlsDontSetAnyTintColor" ore soo?
Please help...
A single button works: http://img257.imageshack.us/i/schermafbeelding2010102.png/
But with the segmented it's messed up: http://img714.imageshack.us/i/schermafbeelding2010102.png/
I've done it without problems using InterfaceBuilder. I had three segments, some with labels, some with labels + pictures, all working fine.
It must be your conversion into a BarButtonItem that doesn't work. You could try adding the segmented control as a subview of the nav bar instead of coaxing it into a button.
This is a really old question but I ran into this problem today (Xcode 7b5/iOS 9). In my case the segmentedControl was a UIBarButtonItem in the navBar. I was able to set the tintColor by waiting for the next run loop, like so:
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] init]
(...additional setup...)
UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
self.navigationItem.rightBarButtonItem = segmentBarItem;
dispatch_async(dispatch_get_main_queue(), ^{
segmentedControl.tintColor=[UIColor purpleColor];
});