<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Matteo Caldari &#187; uikit</title>
	<atom:link href="http://matteocaldari.it/tag/uikit/feed" rel="self" type="application/rss+xml" />
	<link>http://matteocaldari.it</link>
	<description>freelance developer</description>
	<lastBuildDate>Sat, 17 Sep 2011 14:01:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>A UISegmentedControl with custom color</title>
		<link>http://matteocaldari.it/2010/05/a-uisegmentedcontrol-with-custom-color</link>
		<comments>http://matteocaldari.it/2010/05/a-uisegmentedcontrol-with-custom-color#comments</comments>
		<pubDate>Mon, 24 May 2010 09:24:58 +0000</pubDate>
		<dc:creator>matteo</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[iphone sdk]]></category>
		<category><![CDATA[cocoa]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[colors]]></category>
		<category><![CDATA[objective c]]></category>
		<category><![CDATA[subclassing]]></category>
		<category><![CDATA[uikit]]></category>

		<guid isPermaLink="false">http://matteocaldari.it/?p=364</guid>
		<description><![CDATA[Update: code for the class is now on GitHub. Also updated with BSD license and a fix for the next iOS version coming soon. If you have issues please use GitHub. The color of a UISegmentedControl in the UIKit is customizable only in the bar style mode, so if one uses the plain or bordered [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Update: </strong> code for the class is now on <a href="https://github.com/oettam/MCSegmentedControl">GitHub</a>. Also updated with BSD license and a fix for the next iOS version coming soon. If you have issues please use GitHub.</p>
<p>The color of a UISegmentedControl in the UIKit is customizable only in the bar style mode, so if one uses the plain or bordered mode, the only possible color is blue. This could be a problem if blue does not fit well in your app or in a view.</p>
<p>The only way to support other colors is to subclass the UISegmentedColor and draw the view yourself. That&#8217;s what I did in the MCSegmentedControl class, here&#8217;s how it looks with a green tint color.</p>
<p><img class="aligncenter size-full wp-image-367" title="Screen shot 2010-05-24 at 09.53.17" src="http://matteocaldari.it/wp-content/uploads/Screen-shot-2010-05-24-at-09.53.17.png" alt="" width="315" height="68" /><br />
<span id="more-364"></span><br />
Here  compared with the standard UISegmentedControl, bordered style (mine is above):</p>
<p><img class="aligncenter size-full wp-image-368" title="Screen shot 2010-05-24 at 09.51.49" src="http://matteocaldari.it/wp-content/uploads/Screen-shot-2010-05-24-at-09.51.49.png" alt="" width="301" height="137" /></p>
<p>and plain style:</p>
<p><img class="aligncenter size-full wp-image-369" title="Screen shot 2010-05-24 at 09.52.28" src="http://matteocaldari.it/wp-content/uploads/Screen-shot-2010-05-24-at-09.52.28.png" alt="" width="313" height="141" /></p>
<p>and now with some ugly colors and font:</p>
<p><img class="aligncenter size-full wp-image-370" title="Screen shot 2010-05-24 at 09.53.49" src="http://matteocaldari.it/wp-content/uploads/Screen-shot-2010-05-24-at-09.53.49.png" alt="" width="308" height="66" /></p>
<p>My implementation is not pixel identical to the Apple one, but it is very similar.</p>
<p>I added support for customizing the tint (i.e. the background of the selected segment), the color of the not selected items and of the selected item, plus, if the segment has an image, it is drawn as a mask, so it will always be in the same color as the text.</p>
<h3>Example</h3>
<div class="codecolorer-container objc default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #11740a; font-style: italic;">// Instantiate as usual</span><br />
<a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/"><span style="color: #400080;">NSArray</span></a> <span style="color: #002200;">*</span>items <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/"><span style="color: #400080;">NSArray</span></a> arrayWithObjects<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;first&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;second&quot;</span>, <span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;image.png&quot;</span><span style="color: #002200;">&#93;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;<br />
MCSegmentedControl <span style="color: #002200;">*</span>segmentedControl <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>MCSegmentedControl alloc<span style="color: #002200;">&#93;</span> initWithItems<span style="color: #002200;">:</span>items<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <br />
<span style="color: #11740a; font-style: italic;">// set frame, add to view, set target and action for value change as usual</span><br />
segmentedControl.frame <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span>10.0f, 10.0f, 300.0f, 44.0f<span style="color: #002200;">&#41;</span>;<br />
<span style="color: #002200;">&#91;</span>self.view addSubview<span style="color: #002200;">:</span>segmentedControl<span style="color: #002200;">&#93;</span>;<br />
<span style="color: #002200;">&#91;</span>segmentedControl addTarget<span style="color: #002200;">:</span>self action<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>segmentedControlDidChange<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> forControlEvents<span style="color: #002200;">:</span>UIControlEventValueChanged<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <br />
<span style="color: #11740a; font-style: italic;">// Set a tint color</span><br />
segmentedControl.tintColor <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIColor orangeColor<span style="color: #002200;">&#93;</span>;<br />
&nbsp; &nbsp; <br />
<span style="color: #11740a; font-style: italic;">// Customize font and items color</span><br />
segmentedControl.selectedItemColor <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIColor yellowColor<span style="color: #002200;">&#93;</span>;<br />
segmentedControl.unselectedItemColor <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIColor darkGrayColor<span style="color: #002200;">&#93;</span>;</div></div>
<p>If you use Interface Builder, add a normal UISegmentedControl, set its class as MCSegmentedControl in the Identity Inspector, set the Tint in the Attributes Inspector.</p>
<p>At the moment, animations and the following UISegmentedControl methods are not supported:</p>
<div class="codecolorer-container objc default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setWidth<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGFloat<span style="color: #002200;">&#41;</span>width forSegmentAtIndex<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSUInteger<span style="color: #002200;">&#41;</span>segment;<br />
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setContentOffset<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGSize<span style="color: #002200;">&#41;</span>offset forSegmentAtIndex<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSUInteger<span style="color: #002200;">&#41;</span>segment</div></div>
<p>Here are the files: <a href="http://matteocaldari.it/download/MCSegmentedControl.zip">MCSegmentedControl.zip</a><br />
<div id="tweetbutton364" class="tw_button" style=""><a href="http://twitter.com/share?url=http%3A%2F%2Fbit.ly%2FcAV7Z0&amp;via=matteo&amp;text=A%20UISegmentedControl%20with%20custom%20color&amp;related=matteo&amp;lang=en&amp;count=none&amp;counturl=http%3A%2F%2Fmatteocaldari.it%2F2010%2F05%2Fa-uisegmentedcontrol-with-custom-color" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('https://matteocaldari.it/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div></p>
]]></content:encoded>
			<wfw:commentRss>http://matteocaldari.it/2010/05/a-uisegmentedcontrol-with-custom-color/feed</wfw:commentRss>
		<slash:comments>26</slash:comments>
		</item>
	</channel>
</rss>

