Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISegmentedControl in Mail app

How do I get a UISegmentedControl that is like the one in the Mail App, so that it is the same colour as UIToolbar buttons (as if both segments were in the selected state).

I want to use the segmented control for exactly the same purpose as Mail.

(on the iPad, so a grey not blue color)

like image 762
Jonathan. Avatar asked Sep 22 '10 17:09

Jonathan.


2 Answers

This is code from Apple Sample codes... NavBar and both the images used in the code.. you shoud be able to get exact same view as mail App.

alt textalt text

// "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, 30);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.momentary = YES;

defaultTintColor = [segmentedControl.tintColor retain];    // keep track of this for later

UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[segmentedControl release];

self.navigationItem.rightBarButtonItem = segmentBarItem;
[segmentBarItem release];
like image 66
k-thorat Avatar answered Oct 03 '22 00:10

k-thorat


You seek the tintColor property!

When you use a UISegmentedControl you can change its tint color to any color you can dream up. So, if you added the UISegmentedControl in Interface Builder then you would style it in your - (void)viewWillAppear:(BOOL)animated method as such (assuming you had it hooked up to a @synthesized ivar:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    // Set the tintColor to match the navigation bar
    self.mySegmentedControl.tintColor = [UIColor colorWithRed:.94 green:.94 blue:.94 alpha:1];

    ... do whatever else in your viewWillAppear ...
}

Now obviously you will want to play with the red, green, blue, and alpha's that I've put in the sample code above, but you can literally tint the UISegmentedController any color you would like (or make it as transparent as you would like), so it's just a matter of finding the RGBA values that look perfect to you.

Remember that per Apple's docs that the default value of this property is nil (no color). UISegmentedControl uses this property only if the style of the segmented control is UISegmentedControlStyleBar.

Good luck!

like image 33
Neal L Avatar answered Oct 03 '22 01:10

Neal L