iPhone - adding a UISegmentedControl to a Navigation Controller Toolbar - iphone

Is this an heresy to add a UISegmentedControl to a navigation Controller Toolbar?
I am considering this code:
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
[NSString stringWithString:NSLocalizedString(#"One", #"")],
[NSString stringWithString:NSLocalizedString(#"Two", #"")],
[NSString stringWithString:NSLocalizedString(#"Three", #"")],
[NSString stringWithString:NSLocalizedString(#"Four", #"")],
nil]];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.tintColor = [UIColor blackColor];
[segmentedControl setSelectedSegmentIndex:0];
[segmentedControl addTarget:self action:#selector(changeSegment:)
forControlEvents:UIControlEventValueChanged];
[segmentedControl setFrame:[self.navigationController.toolbar bounds]];
[self.navigationController.toolbar addSubview:segmentedControl];
[segmentedControl release];
I have tried this code and the segments don't appear to be selected with I touch them.
Is this the correct way of doing that?
thanks.

This is most likely due to your tintColor being black. Change the color and you should see the selected segment reflected.

Related

How to add an image & a system bar button item to a UISegmentedControl?

I have an UISegmentedControl as the navigation bar's right bar button item. This is achieved by the following code...
UISegmentedControl *segmentedControl = [ [UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Segment1",#"Segment2",nil]];
[segmentedControl addTarget:self action:#selector(segmentClicked) forControlEvents:UIControlEventValueChanged]; //Where segmentClicked is the method for segment click action
segmentedControl.frame = CGRectMake(0, 0, 90, 35);
UIBarButtonItem *rightBaritem = [ [UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[segmentedControl release];
self.navigationItem.rightBarButtonItem = rightBaritem;
[rightBaritem release];
The above code fine and it will show a segmented Control with two segments "Segment1" & "Segment2".
But I want to show an image instead of Segment1 & a system bar button (Say UIBarButtonSystemItemAdd) instead of Segment2.
Image can be inserted in Segmented control by the code,
UISegmentedControl *segmentedControl = [ [UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:[UIImage imageNamed:#"<image_name.image_type>"],???????,nil]];
But i dont know how to include UIBarButtonSystemItemAdd in the place of ???????.
Thanks in Advance..
This code may solve your problem.This solved my problem that I was facing at the same time.But don't think this is the accurate one.I got the desired which I need to display.This works fine with two buttons.
UISegmentedControl *doneButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Done"]];
doneButton.momentary = YES;
doneButton.frame = CGRectMake(240, 7.0f, 70.0f, 30.0f);
doneButton.tintColor = [UIColor blackColor];
[doneButton addTarget:self action:#selector(Done:) forControlEvents:UIControlEventValueChanged];
[menustyle addSubview:doneButton];
[doneButton release];
doneButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Close"]];
doneButton.momentary = YES;
doneButton.frame = CGRectMake(10, 7.0f, 70.0f, 30.0f);
doneButton.tintColor = [UIColor blackColor];
[doneButton addTarget:self action:#selector(Krishna:) forControlEvents:UIControlEventValueChanged];
[menustyle addSubview:doneButton];
[doneButton release];

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

How to set the UISegmentControl as selected and multiple touches for selected index?

I have one problem :I have UISegmentedControl with three indexes 0,1,2. when i select the index of UISegmentedControl it is working fine but I need to have multiple touches and it should be focued How it can be achieved. how can be programed for Tapping the selected index again and it should work...
NSArray *segmentTextContent = [NSArray arrayWithObjects: allString, favoritesString,filterString, nil];
segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentTextContent];
[segmentedControl setFrame:CGRectMake(60,10,200,30)];
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
//segmentedControl.momentary = YES;
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.tintColor=[UIColor colorWithRed:36/255.0 green:61/255.0 blue:103/255.0 alpha:1];
segmentedControl.selectedSegmentIndex =0;
[toolBar1 addSubview:segmentedControl];
mySegmentedControl.momentary = YES;
I believe this is want you want.

How can I add two UIBarButtonItems to UINavigationItem?

I want two rightBarButtonItem's on my UINavigationBar. How can I accomplish this?
You can use a UISegmentedControl with two buttons and configure it with the momentary property set to YES.
This is what is used in the Mail application to go to next/previous message.
Update
In order to assign the UISegmentedControl]1 as a right button, you have to wrap it inside a UIBarButtonItem (sample code taken from the NavBar sample application):
- (void)viewDidLoad
{
// "Segmented" control to the right
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
[UIImage imageNamed:#"up.png"],
[UIImage imageNamed:#"down.png"],
nil]];
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
segmentedControl.frame = CGRectMake(0, 0, 90, kCustomButtonHeight);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.momentary = YES;
UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[segmentedControl release];
self.navigationItem.rightBarButtonItem = segmentBarItem;
[segmentBarItem release];
}

Disabling UINavigationItem custom right view?

I create a custom right view as follows:
// Build the Segmented Control
NSArray *segmentTextContent = [NSArray arrayWithObjects:[UIImage imageNamed:#"arrow-dice.png"], [UIImage imageNamed:#"arrow-up.png"], [UIImage imageNamed:#"arrow-down.png"], nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentTextContent];
// Customize the Segmented Control
segmentedControl.momentary = YES;
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
[segmentedControl addTarget:self action:#selector(segmentChanged:) forControlEvents:UIControlEventValueChanged];
Then I add it to my navigation bar as follows:
// Add the control to the navigation bar right item
UIBarButtonItem *segmentItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[segmentedControl release];
self.navigationItem.rightBarButtonItem = segmentItem;
self.navigationItem.rightBarButtonItem.title = #"";
[segmentItem release];
I can hide it as follows:
self.navigationItem.rightBarButtonItem.customView.hidden = NO;
QUESTION
...but how can I disable all (or better, a specific element) of the segmented control?
The following does not work.
self.navigationItem.rightBarButtonItem.enabled = NO;
Any ideas appreciated...
Thanks,
matt
UISegmentedControl.h
- (void)removeSegmentAtIndex:(NSUInteger)segment animated:(BOOL)animated;
- (void)removeAllSegments;
- (void)setEnabled:(BOOL)enabled forSegmentAtIndex:(NSUInteger)segment; //default is YES
e.g. [segmentedControl setEnabled:NO forSegmentAtIndex:1];
Hope That helps
[navItem.rightBarButtonItem setEnabled:NO];
as simple as that :)
Reference: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIBarItem_Class/Reference/Reference.html
See the enabled property.