You can easily create buttons, link actions to them, but they aren’t cool at all.
This is the typical button:

And when being pressed (highlighted):
![]()
In my opinion rounded corners look nice, so do the highlighted version, but this white background doesn’t appeal to me. Open the Clock application and go to the Stopwatch. Don’t you think that these Start/Stop and Reset/Lap buttons are more acceptable? Yes, they are.
You might have noticed that using Interface Builder you can set the background color of UIButton. Exacly. Below you can see the button with green background:

That’s it. UIButton has the green background color. Thank you very much and read my next tutorial.
Oh, sorry for the irony. So, in the case above, button looks like a TV from ’90s rather than a green button. There is a simple way to achieve results similar to the Stopwatch application. All you have to do is to get rid of UIButton and use UISegmentedControl.
UISegmentedControl is typicaly used in options / settings views, where you are able to pick one option of many.
Today, I’m going to show you how to customize this UISegmentedControl:

so it will look like this nice button below:
![]()
Unfortunately you cannot do everything in Interface Builder only, so you will need to create an IBOutlet and connect it to created UISegmentedControl in Interface Builder.
IBOutlet UISegmentedControl *myShinyButton;
In Interface Builder go to UISegmentedControl’s attributes, and set Segments=1 – so your UISegmentedControl will consist of only one segment – button. Check momentary – as you see on above UISegmentedControl example (First | Second), First is selected an it stays in that state until you select Second. Once momentary is checked the segment returns to the normal state after a second. Change the style to Bar – only bar can be customized in the way I want to show you. And the last thing you need to do: uncheck the selected for the current segment. Double click the button to type your own text on it. Your button should look like this:
![]()
Now you can customize the color, but you can only do it in Xcode. Use the property tintColor:
myShinyButton.tintColor = [UIColor darkGrayColor];
Here is how your button will look like after it:
![]()
Please note: avoid setting “strong colors” as a tintColor. Otherwise there will be no visible difference between normal and selected state when the color becomes more intensive. darkGrayColor will be perfect replacement of blackColor. You can also play with setting RGB colors where you have full control of intesitivity of red, green and blue.
Programmatically
codeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"My Button"]]; codeButton.segmentedControlStyle = UISegmentedControlStyleBar; codeButton.momentary = YES; codeButton.tintColor = [UIColor lightGrayColor]; codeButton.center = CGPointMake(160,400); [self.view addSubview:codeButton];
Please note:
It won’t be discussed with details here, but remember, you can always use an image as a button (UIButton type: custom). Simple code:
[button setImage:[UIImage imageNamed:@"myButton.png"] forState:UIControlStateNormal]; [button setImage:[UIImage imageNamed:@"myButton_highlighted.png"] forState:UIControlStateHighlighted]; [button setImage:[UIImage imageNamed:@"myButton_selected.png"] forState:UIControlStateSelected];
Providing the right images will give you any effect you need.
Actions
To link any action to buttons created with UISegmentedControl use a value changed event instead of Touch Up Inside. In Interface Builder it should be quite easy, but to do it programmatically:
[codeButton addTarget:self action:@selector(simpleAction) forControlEvents:UIControlEventValueChanged];
As usuall, sample project for you:


I really wanted to develop a simple message so as to express gratitude to you for all of the unique facts you are giving on this site. My long internet lookup has finally been recognized with wonderful suggestions to talk about with my friends and family. I would point out that many of us readers are unequivocally fortunate to dwell in a good place with many perfect individuals with interesting concepts. I feel truly grateful to have encountered your web page and look forward to plenty of more exciting moments reading here. Thanks again for a lot of things.
I’ve recently started a website, the information you offer on this website has helped me greatly. Thanks for all of your time & work. Elegant London Escorts, 302 Heddon St, London, W1B 3AS, 028 9321 0373
look videos watch
I love the layout of your blog! What theme are you using if you don’t mind me asking? http://theminecraftportal.com/
Thanks. This is th only site where I found detecting tap on Segmented control created programatically
Fairly good post. I recently discovered your blog and also wanted to point out that I’ve truly adored exploring the blog site articles. All things considered I’m going to be subscribing to your own rss feed and i also we imagine you create yet again quickly!
I uncovered your web page via search motors even when looking for for the connected topic, your web page demonstrated up up. give many as a consequence of you for the fabulous blog. Amazingg skills! hold on man, you rock!
Exceedingly informative thank you, I’m sure your trusty subscribers might possibly want a whole lot more items like that carry on the excellent hard work.
Great stuff, but this doesn’t work in iOS 4 – segmented control requires at least 2 segments. If you try to set it to 1 programatically, the app crashes.
Cheers for the marvelous posting… I seriously enjoyed studying Creating a nice glass buttons » Chris-Software.com, you’re a terrific contributor We shall always take a note of this blog page and will often revisit at some point, We wish to encourage you to definitely continue your great posts, enjoy your day! Before I forget our thoughts go with everyone close by Fukushima Daiichi nuclear hopefully your are healthy plus secure too
Thanks … Easy Web Site Builder
http://oron.com/9z46tuqsd9jb/017._Let_it_snow_-_Public_Invasion_-_pi_1624_(24.02.2005).wmv.html
I’m using Xcode/Interface Builder 3.2.3 and it seems that in Interface Builder the UISegmentedControl’s design component sets a minimum of two for the number of segments. I can create a control with two segments and then programatically remove the ones I don’t use, but removing them using removeSegmentAtIndex:animated: seems to trigger a Value Changed action in the process – making my buttons fire when created.
Any ideas, short of customizing my XIB text directly? [I'm new to all things Mac, so I'm trying to get the intended use of things clear in my head.]
True if you are a merchandiser who is interested in merchandising his own Cartesian product, you seat still entree this site, as it was designed to provide you with the best menu. Downloadable plans are the greatest option for advancing your personal computer; programmes available on this site allow every user to leverage a quality merchandise that comes along with professional benefits. By choosing this website, you behind sleep assured that Software System will arrive in no time.
Humans do a wonderful job at destroying the English language through their own misconceptions and faulty thinking. Programmers work tirelessly to read others’ documentation about how code is intended to work.
Yet here some programmer wrote a program specifically to fake English sentences so as to bypass much-needed automated filtering of gibberish.
The attack on truth by the wicked infests our digital world. Be on guard.
But how can i change the height of the button ? the standard height is set to 30 pixel and i can’t change it.
Just adjust the frame :
codeButton.frame = CGRectMake(xoffset,yoffset,width,height);
Thanks for the tutorial, couldn’t be any better.
UIButtons had the ability to be tinted in early versions of the SDK. It was really easy to do and looked great. I filed a radar to get this functionality reinstated – please file a duplicate !
Respectueusement,M. je vous solicite une bourse d’
be careful I got some issue with the latest version of the sdk … few compatibility issue.
I didn’t have any chance to play with new SDK, as long as I’m stucked in developing app for 2.2.1, but as far as I’m concerned I guess programmer will have to customize objects using rather methods then properties:
[sth setTintColor:[UIColor darkGrayColor];
instead of:
sth.tintColor = [UIColor darkGrayColor];
Good article, thanks. I just agreed to your blog site rss.