<?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>iOS Developer Tips Blog</title>
	<atom:link href="http://iPhoneDeveloperTips.com/feed" rel="self" type="application/rss+xml" />
	<link>http://iPhoneDeveloperTips.com</link>
	<description>iOS Developer Tips, Tricks and Tutorials.</description>
	<lastBuildDate>Wed, 08 Feb 2012 13:40:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Remind Users to Restart Your App</title>
		<link>http://iPhoneDeveloperTips.com/core-services/encourage-users-to-restart-your-app.html</link>
		<comments>http://iPhoneDeveloperTips.com/core-services/encourage-users-to-restart-your-app.html#comments</comments>
		<pubDate>Wed, 08 Feb 2012 08:03:02 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Core Services]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=11021</guid>
		<description><![CDATA[This tip shows you how to create a local notification that will be trigged when a user exits your application. I would recommend you use this sparingly, for example, if the user was in the middle of setting up configuration information needed by an application. Let&#8217;s start with a quick look at how this appears [...]]]></description>
			<content:encoded><![CDATA[<p>This tip shows you how to create a local notification that will be trigged when a user exits your application. I would recommend you use this sparingly, for example, if the user was in the middle of setting up configuration information needed by an application.</p>
<p>Let&#8217;s start with a quick look at how this appears to the user. Assume the application TestApp is running in the foreground. When the application is put into the background by pressing the home button, within 1 second (see the code below), the following local notifications will appear (iOS 5 and iOS 4):<br />
<span id="more-11021"></span></p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2012/02/localNotif2.gif" alt="" title="localNotif2" width="414" height="50" class="alignnone size-full wp-image-11023" /></p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2012/02/localNotif1.gif" alt="" title="localNotif1" width="297" height="146" class="alignnone size-full wp-image-11022" /></p>
<p>If the user taps on the message, the application return to the foreground.</p>
<p>The code is quite simple, it begins with creating an NSDate object with the current date/time, adding one second to that time, and setting the fire date for a local notification to the configured date. From there, setup the local notification messaging and wrap up by scheduling the notification with the OS.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>applicationDidEnterBackground<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIApplication <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>application
<span style="color: #002200;">&#123;</span>
  UILocalNotification <span style="color: #002200;">*</span>localNotification <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UILocalNotification alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Current date</span>
  <span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span>date <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDate</span> date<span style="color: #002200;">&#93;</span>; 
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Add one second to the current time</span>
  <span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span>dateToFire <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>date dateByAddingTimeInterval<span style="color: #002200;">:</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Set the fire date/time</span>
  <span style="color: #002200;">&#91;</span>localNotification setFireDate<span style="color: #002200;">:</span>dateToFire<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>localNotification setTimeZone<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSTimeZone</span> defaultTimeZone<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;    
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Setup alert notification</span>
  <span style="color: #002200;">&#91;</span>localNotification setAlertBody<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Tap to return to TestApp&quot;</span> <span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>localNotification setAlertAction<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Open TestApp&quot;</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>localNotification setHasAction<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIApplication sharedApplication<span style="color: #002200;">&#93;</span> scheduleLocalNotification<span style="color: #002200;">:</span>localNotification<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Once the notification has been presented to the user, and the user acknowledges, the method below in your application will be called once the app comes to the foreground:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>application<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIApplication <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>application didReceiveLocalNotification<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UILocalNotification <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>notification
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Your code here...</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Again, consider this option as a reminder to the user only in cases where an important process was underway, not as the default operation when the app goes into the background.</p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/core-services/encourage-users-to-restart-your-app.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Debugging with GDB: Print-Object and UIView recursiveDescription</title>
		<link>http://iPhoneDeveloperTips.com/debugging/debugging-with-gdb-print-object-and-uiview-recursivedescription.html</link>
		<comments>http://iPhoneDeveloperTips.com/debugging/debugging-with-gdb-print-object-and-uiview-recursivedescription.html#comments</comments>
		<pubDate>Mon, 30 Jan 2012 07:09:56 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Debugging]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=10996</guid>
		<description><![CDATA[In the post Debugging with GDB: Introduction to Commands, Print and Print-Object I covered the basics of the command line inteface in GDB. In this post I&#8217;ll show you a trick to print out the entire view hierarchy for UIView objects. In Objective-C, all objects (derived from NSObject) have a description method, which returns an [...]]]></description>
			<content:encoded><![CDATA[<p>In the post <a href="http://iphonedevelopertips.com/debugging/debugging-with-gdb-introduction-to-commands.html">Debugging with GDB: Introduction to Commands, Print and Print-Object</a> I covered the basics of the command line inteface in GDB. In this post I&#8217;ll show you a trick to print out the entire view hierarchy for <strong>UIView</strong> objects.</p>
<p>In Objective-C, all objects (derived from <strong>NSObject</strong>) have a description method, which returns an <strong>NSString</strong> describing the object. When using GDB, you can print the description using print-object, for example, let&#8217;s use the interface definition below to look at a few descriptions:<br />
<span id="more-10996"></span></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> SandboxViewController <span style="color: #002200;">:</span> UIViewController &lt;UITextFieldDelegate&gt;
<span style="color: #002200;">&#123;</span>
  UITextField  <span style="color: #002200;">*</span>username;
  UIButton     <span style="color: #002200;">*</span>testButton;
  UILabel      <span style="color: #002200;">*</span>label;
  UIView       <span style="color: #002200;">*</span>view;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>When stopped on a breakpoint in the <strong>SandboxViewController</strong> object, GDB can display information on each object:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2012/01/po1.gif" alt="" title="po1" width="588" height="120" class="alignnone size-full wp-image-11005" /></p>
<p>Note, <strong>po</strong> is equivalent to <strong>print-object</strong>.</p>
<h5>Print-Object and recursiveDescription</h5>
<p>When working with <strong>UIView</strong> objects, there is an additional method for obtaining information, <strong>recursiveDescription</strong>. When using print-object and calling this method you can see the entire view hierarchy for the object.</p>
<p>With the <strong>SandboxViewController</strong> object defined above, in the <strong>loadView</strong> method I created a <strong>UIView</strong> as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>self setView<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIView alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIScreen mainScreen<span style="color: #002200;">&#93;</span> applicationFrame<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>Within GDB, when on a breakpoint within the <strong>SandboxViewController</strong> object, I can now view the entire view hierarchy as follows:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2012/01/po2.gif" alt="" title="po2" width="589" height="209" class="alignnone size-full wp-image-11008" /></p>
<p>The same idea works for a <strong>UIView</strong> that is not associated with a view controller. Using the view defined below, and the label added as a subview:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Create UIView</span>
view <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIView alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span> CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">10</span>, <span style="color: #2400d9;">10</span>, <span style="color: #2400d9;">300</span>, <span style="color: #2400d9;">50</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self view<span style="color: #002200;">&#93;</span> addSubview<span style="color: #002200;">:</span>view<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Add label to UIView</span>
UILabel <span style="color: #002200;">*</span>tmplabel <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UILabel alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">10</span>, <span style="color: #2400d9;">20</span>, <span style="color: #2400d9;">280</span>, <span style="color: #2400d9;">80</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;    
<span style="color: #002200;">&#91;</span>tmplabel setTextColor<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIColor whiteColor<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>tmplabel setTextAlignment<span style="color: #002200;">:</span>UITextAlignmentLeft<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>tmplabel setText<span style="color: #002200;">:</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Enter your password:&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>view addSubview<span style="color: #002200;">:</span>tmplabel<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>Here is the recursive description:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2012/01/po3.gif" alt="" title="po3" width="590" height="55" class="alignnone size-full wp-image-11013" /></p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/debugging/debugging-with-gdb-print-object-and-uiview-recursivedescription.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Updated: Tracking Down EXC_BAD_ACCESS Errors with NSZombieEnabled in Xcode 4</title>
		<link>http://iPhoneDeveloperTips.com/debugging/updated-tracking-down-exc_bad_access-errors-with-nszombieenabled-in-xcode-4.html</link>
		<comments>http://iPhoneDeveloperTips.com/debugging/updated-tracking-down-exc_bad_access-errors-with-nszombieenabled-in-xcode-4.html#comments</comments>
		<pubDate>Fri, 27 Jan 2012 03:04:16 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Debugging]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=10972</guid>
		<description><![CDATA[In a previous post Tracking Down EXC_BAD_ACCESS Errors with NSZombieEnabled I explained how the environment variable NSZombieEnabled can help track down EXC_BAD_ACCESS errors, which are typically caused by attempting to access objects that have already been released. With Xcode 4 the process for setting NSZombieEnabled is different than in earlier versions of Xcode. To configure [...]]]></description>
			<content:encoded><![CDATA[<p>In a previous post <a href="http://iphonedevelopertips.com/debugging/tracking-down-exc_bad_access-errors-with-nszombieenabled.html">Tracking Down EXC_BAD_ACCESS Errors with NSZombieEnabled</a> I explained how the environment variable NSZombieEnabled can help track down EXC_BAD_ACCESS errors, which are typically caused by attempting to access objects that have already been released.</p>
<p>With Xcode 4 the process for setting NSZombieEnabled is different than in earlier versions of Xcode. To configure zombies, enter ⌥-⌘-r (alt-command-r). Once the dialog is shown, choose the option <strong>Enable Zombie Objects</strong><br />
<span id="more-10972"></span></p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2012/01/Zombie.gif" alt="" title="Zombie" width="429" height="349" class="alignnone size-full wp-image-10993" /></p>
<p>When using a zombie, any message sent to an object that has already released will result in an exception being thrown, which will look similar to the screenshot below:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2012/01/exc_error.gif" alt="" title="exc_error" width="551" height="45" class="alignnone size-full wp-image-10990" /></p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/debugging/updated-tracking-down-exc_bad_access-errors-with-nszombieenabled-in-xcode-4.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Delete All Files in Documents Directory</title>
		<link>http://iPhoneDeveloperTips.com/data-file-management/delete-all-files-in-documents-directory.html</link>
		<comments>http://iPhoneDeveloperTips.com/data-file-management/delete-all-files-in-documents-directory.html#comments</comments>
		<pubDate>Tue, 17 Jan 2012 02:09:15 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Data / File Management]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=10941</guid>
		<description><![CDATA[In a recent project, I was using iTunes File Sharing to store various log files and test data. Although you can quickly delete all the files in the Documents directory using iTunes, I also was looking for a way to clean up the same directory when my device was not connected. The first approach I [...]]]></description>
			<content:encoded><![CDATA[<p>In a recent project, I was using <a href="http://iphonedevelopertips.com/debugging/using-uifilesharingenabled-for-creating-debug-log-files.html" target="_blank">iTunes File Sharing</a> to store various log files and test data. Although you can quickly delete all the files in the Documents directory using iTunes, I also was looking for a way to clean up the same directory when my device was not connected.</p>
<p>The first approach I took was to loop through all the files, building a path to each, one by one:<br />
<span id="more-10941"></span></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Path to the Documents directory</span>
<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>paths <span style="color: #002200;">=</span> NSSearchPathForDirectoriesInDomains<span style="color: #002200;">&#40;</span>NSDocumentDirectory, NSUserDomainMask, <span style="color: #a61390;">YES</span><span style="color: #002200;">&#41;</span>;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>paths count<span style="color: #002200;">&#93;</span> &gt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
  <span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;  
  <span style="color: #400080;">NSFileManager</span> <span style="color: #002200;">*</span>fileManager <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSFileManager</span> defaultManager<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Print out the path to verify we are in the right place</span>
  <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>directory <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>paths objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span>;
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Directory: %@&quot;</span>, directory<span style="color: #002200;">&#41;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// For each file in the directory, create full path and delete the file</span>
  <span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>file <span style="color: #a61390;">in</span> <span style="color: #002200;">&#91;</span>fileManager contentsOfDirectoryAtPath<span style="color: #002200;">:</span>directory error<span style="color: #002200;">:&amp;</span>error<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
  <span style="color: #002200;">&#123;</span>    
    <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>filePath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>directory stringByAppendingPathComponent<span style="color: #002200;">:</span>file<span style="color: #002200;">&#93;</span>;
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;File : %@&quot;</span>, filePath<span style="color: #002200;">&#41;</span>;
&nbsp;
    <span style="color: #a61390;">BOOL</span> fileDeleted <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>fileManager removeItemAtPath<span style="color: #002200;">:</span>filePath error<span style="color: #002200;">:&amp;</span>error<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>fileDeleted <span style="color: #002200;">!=</span> <span style="color: #a61390;">YES</span> || error <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
      <span style="color: #11740a; font-style: italic;">// Deal with the error...</span>
    <span style="color: #002200;">&#125;</span>
  <span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>The code above works fine, however, I was interested in something that didn&#8217;t build each path separately &#8211; the code below deletes all the files in one fell swoop:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">&nbsp;
<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>paths <span style="color: #002200;">=</span> NSSearchPathForDirectoriesInDomains<span style="color: #002200;">&#40;</span>NSDocumentDirectory, NSUserDomainMask, <span style="color: #a61390;">YES</span><span style="color: #002200;">&#41;</span>;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>paths count<span style="color: #002200;">&#93;</span> &gt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Path: %@&quot;</span>, <span style="color: #002200;">&#91;</span>paths objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
  <span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;  
  <span style="color: #400080;">NSFileManager</span> <span style="color: #002200;">*</span>fileManager <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSFileManager</span> defaultManager<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Remove Documents directory and all the files</span>
  <span style="color: #a61390;">BOOL</span> deleted <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>fileManager removeItemAtPath<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>paths objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> error<span style="color: #002200;">:&amp;</span>error<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>deleted <span style="color: #002200;">!=</span> <span style="color: #a61390;">YES</span> || error <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span>
  <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Deal with the error...</span>
  <span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Given the requested item to remove was a directory, as you&#8217;d expect, not only were the files deleted, so was the Documents directory. Problem is, I ran into errors when I attempted to drag/drop new files into the file sharing areas iTunes.</p>
<p>To get things working again, I changed up the code above to create the Documents directory:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>paths <span style="color: #002200;">=</span> NSSearchPathForDirectoriesInDomains<span style="color: #002200;">&#40;</span>NSDocumentDirectory, NSUserDomainMask, <span style="color: #a61390;">YES</span><span style="color: #002200;">&#41;</span>;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>paths count<span style="color: #002200;">&#93;</span> &gt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Path: %@&quot;</span>, <span style="color: #002200;">&#91;</span>paths objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
  <span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;  
  <span style="color: #400080;">NSFileManager</span> <span style="color: #002200;">*</span>fileManager <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSFileManager</span> defaultManager<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Remove all files in the documents directory</span>
  <span style="color: #a61390;">BOOL</span> deleted <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>fileManager removeItemAtPath<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>paths objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> error<span style="color: #002200;">:&amp;</span>error<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>deleted <span style="color: #002200;">!=</span> <span style="color: #a61390;">YES</span> || error <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span>
  <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Deal with the error...</span>
  <span style="color: #002200;">&#125;</span>
  <span style="color: #a61390;">else</span>
    <span style="color: #11740a; font-style: italic;">// Recreate the Documents directory</span>
    <span style="color: #002200;">&#91;</span>fileManager createDirectoryAtPath<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>paths objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> withIntermediateDirectories<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span> attributes<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> error<span style="color: #002200;">:&amp;</span>error<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Although there is less code to delete the Documents directory, something tells me it may simply be a better (and more robust) solution to loop and delete each file in sequence.</p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/data-file-management/delete-all-files-in-documents-directory.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>iOS Open Source : SVProgressHUD Heads Up Display</title>
		<link>http://iPhoneDeveloperTips.com/open-source/ios-open-source-svprogresshud-heads-up-display.html</link>
		<comments>http://iPhoneDeveloperTips.com/open-source/ios-open-source-svprogresshud-heads-up-display.html#comments</comments>
		<pubDate>Fri, 13 Jan 2012 07:44:38 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Open Source]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=10926</guid>
		<description><![CDATA[Although I&#8217;ve covered HUDs previously, Sam Vermette has done a nice job with his design and implementation. I like Sam&#8217;s approach, the methods for displaying and dismissing heads-up-displays are class methods &#8211; this is a nice touch in that you don&#8217;t need to worry about allocating and releasing objects. There is an abundance of interesting [...]]]></description>
			<content:encoded><![CDATA[<p>Although I&#8217;ve covered HUDs <a target="_blank" href="http://iphonedevelopertips.com/open-source/ios-open-source-heads-up-display-hud-effects.html">previously</a>, <a target="_blank" href="http://samvermette.com/">Sam Vermette</a> has done a nice job with his design and implementation. I like Sam&#8217;s approach, the methods for displaying and dismissing heads-up-displays are class methods &#8211; this is a nice touch in that you don&#8217;t need to worry about allocating and releasing objects. </p>
<p>There is an abundance of interesting code in the project, including numerous block animations. Many of the little things Sam has already dialed in &#8211; for example, the size of an HUD is dynamically adjusted when displaying text, the longer the text, the wider the view (and support for wrapping text works out of the box).<br />
<span id="more-10926"></span></p>
<p>Below are a few screenshots, the first shows a long message that is wrapped to fit the display:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2012/01/hud1.gif" alt="" title="hud1" width="295" height="424" class="alignnone size-full wp-image-10927" /></p>
<p>The images below show two of the options for dismissing messages, one for successful operations, one for error conditions:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2012/01/hud2.gif" alt="" title="hud2" width="590" height="450" class="alignnone size-full wp-image-10928" /></p>
<h5>Download SVProgressHUD</h5>
<p>You can <a  target="_blank" href="https://github.com/samvermette/SVProgressHUD">download SVProgressHUD from github</a>.</p>
<h5>Open Source Recommendations ?</h5>
<p>Have you written or worked with iOS or Cocoa open source that you think others would find interesting, <a href="http://iOSDeveloperTips.com/contact" class="smcf-link">send me a note</a>.</p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/open-source/ios-open-source-svprogresshud-heads-up-display.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Debugging with GDB: Introduction to Commands, Print and Print-Object</title>
		<link>http://iPhoneDeveloperTips.com/debugging/debugging-with-gdb-introduction-to-commands.html</link>
		<comments>http://iPhoneDeveloperTips.com/debugging/debugging-with-gdb-introduction-to-commands.html#comments</comments>
		<pubDate>Tue, 10 Jan 2012 08:01:40 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Debugging]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=10886</guid>
		<description><![CDATA[GDB is the debugging system built into Xcode. Xcode handles much of the interaction with GDB to provide support for breakpoints, stepping through/over code, wowever, GBD also provides a command line that you can use to work directly with the debugger. This tutorial walks through the basics of the command line interface along with an [...]]]></description>
			<content:encoded><![CDATA[<p>GDB is the debugging system built into Xcode. Xcode handles much of the interaction with GDB to provide support for breakpoints, stepping through/over code, wowever, GBD also provides a command line that you can use to work directly with the debugger. This tutorial walks through the basics of the command line interface along with an introduction to a handful of commands for viewing variable and object data.</p>
<p>The first thing to understand is that commands can only be entered into GDB when the debugging process is stopped, which is done via breakpoints. If you hit a breakpoint, the debug console will look as follows:<br />
<span id="more-10886"></span></p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2012/01/gdb1.gif" alt="" title="gdb1" width="563" height="237" class="alignnone size-full wp-image-10893" /></p>
<p>Notice the <strong>(gdb)</strong> prompt, politely waiting your input. No better place to start than requesting help information. While in the debug console (and on a breakpoint), from the GDB prompt, type in &#8220;help&#8221;</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2012/01/gdb2.gif" alt="" title="gdb2" width="560" height="287" class="alignnone size-full wp-image-10894" /></p>
<p>Once the high-level help categories are shown, you can look further by viewing one of the classes of commands. For example, below is a list of all the commands for examing data:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2012/01/gdb3.gif" alt="" title="gdb3" width="557" height="464" class="alignnone size-full wp-image-10895" /></p>
<h5>GDB Print Command</h5>
<p>From the help info shown above, let&#8217;s look at the print command:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2012/01/gdb4.gif" alt="" title="gdb4" width="551" height="289" class="alignnone size-full wp-image-10897" /></p>
<p>The expression (EXP) to be printed can be as simple as showing the current value of a variable &#8211; for example, using the code below, here&#8217;s how to print the value of the variable &#8216;counter&#8217;</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#define MAX_CLICK  5</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>someMethod<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>counter title<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>title message<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>msg
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>counter<span style="color: #002200;">++</span> &gt; MAX_CLICK<span style="color: #002200;">&#41;</span>
  <span style="color: #002200;">&#123;</span>
    UIAlertView<span style="color: #002200;">*</span> alertView <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIAlertView alloc<span style="color: #002200;">&#93;</span> initWithTitle<span style="color: #002200;">:</span>title 
      message<span style="color: #002200;">:</span>msg delegate<span style="color: #002200;">:</span>self cancelButtonTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Ok&quot;</span> otherButtonTitles<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>alertView show<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Each time the method above stops on breakpoint at the if statement, the value of counter can be viewed:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2012/01/gdb5.gif" alt="" title="gdb5" width="148" height="69" class="alignnone size-full wp-image-10909" /></p>
<p>Notice the shortcut for print (p) command is used above in the second command.</p>
<p>You can also use expressions when working with variables. Understand the examples that follow are a bit contrived, however, for the sake of illustration please bear with me. Notice that depending on how use define an expression, the value of the expression will vary:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2012/01/gdb6.gif" alt="" title="gdb6" width="220" height="67" class="alignnone size-full wp-image-10903" /></p>
<h5>GDB Print-Object Command</h5>
<p>Looking at the help information for the data commands, notice there is also a print-object command, which will call the description method of an Objective-C object. Let&#8217;s assume we call <strong>someMethod</strong> (which is listed above), using the following call:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>self someMethod<span style="color: #002200;">:</span>x<span style="color: #002200;">++</span> title<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Error Message&quot;</span> message<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Max Counter Value Reached.&quot;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>If we were to set a breakpoint at the line <strong>if (counter++ > MAX_CLICK)</strong>, we could view the objects <strong>title</strong> and <strong>msg</strong>:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2012/01/gdb7.gif" alt="" title="gdb7" width="203" height="71" class="alignnone size-full wp-image-10904" /></p>
<p>Notice the shortcut for print object (po) is used in the second example above.</p>
<p>Print-object is very helpful when looking at objects that are collections, for example NSDictionary and NSArray objects. We can view the following dictionary object using the print-object command:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>arrayOfDictionaryObjects <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithObjects<span style="color: #002200;">:</span>
      <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;1234&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;accountID&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithBool<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>,  <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;isActive&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>,
      <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;2345&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;accountID&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithBool<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>,  <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;isActive&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>,
      <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;3456&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;accountID&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithBool<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;isActive&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>,
      <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;4567&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;accountID&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithBool<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>,  <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;isActive&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2012/01/gdb8.gif" alt="" title="gdb8" width="269" height="251" class="alignnone size-full wp-image-10905" /></p>
<p>At this point, even with this cursory introduction to GDB, hopefully you have a basic idea of the power of GDB. In future posts I dive further into GDB and show you how to make the most of debugging from the command interface.</p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/debugging/debugging-with-gdb-introduction-to-commands.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>iOS Open Source : Doorway Transition with CoreAnimation</title>
		<link>http://iPhoneDeveloperTips.com/open-source/ios-open-source-doorway-transition-with-coreanimation.html</link>
		<comments>http://iPhoneDeveloperTips.com/open-source/ios-open-source-doorway-transition-with-coreanimation.html#comments</comments>
		<pubDate>Fri, 06 Jan 2012 08:41:52 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Open Source]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=10859</guid>
		<description><![CDATA[Ken Matsui has written a unique transition for moving between views that I want to share with you &#8211; the effect is similar to sliding open two doors, with the following view moving toward the front of the display in a smooth, sliding motion. The images below show the basic idea, however, you owe it [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://mkftr.com/" target="_blank">Ken Matsui</a> has written a unique transition for moving between views that I want to share with you  &#8211; the effect is similar to sliding open two doors, with the following view moving toward the front of the display in a smooth, sliding motion.</p>
<p>The images below show the basic idea, however, you owe it to yourself to download and run the demo project to get the full effect.<br />
<span id="more-10859"></span></p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2012/01/door1.gif" alt="" title="door1" width="443" height="338" class="alignnone size-full wp-image-10862" /></p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2012/01/door2.gif" alt="" title="door2" width="443" height="338" class="alignnone size-full wp-image-10863" /></p>
<p>And the best part of integrating the transition into your project, the code is as simple as shown below:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">MFDoorwayTransition <span style="color: #002200;">*</span>transition <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>MFDoorwayTransition alloc<span style="color: #002200;">&#93;</span> 
   initWithBaseView<span style="color: #002200;">:</span>self.view
   firstView<span style="color: #002200;">:</span>self.currentView 
   lastView<span style="color: #002200;">:</span>self.nextView<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>transition buildAnimation<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>transition release<span style="color: #002200;">&#93;</span>;</pre></div></div>

<h5>Download Doorway Transition</h5>
<p>You can <a  target="_blank" href="https://github.com/mkftr/DoorwayTransition">download DoorwayTransition from github</a>.</p>
<h5>Open Source Recommendations ?</h5>
<p>Have you written or worked with iOS or Cocoa open source that you think others would find interesting, <a href="http://iOSDeveloperTips.com/contact" class="smcf-link">send me a note</a>.</p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/open-source/ios-open-source-doorway-transition-with-coreanimation.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>iOS 5 : Customize UINavigationBar and UIBarButtonItem with the Appearance API</title>
		<link>http://iPhoneDeveloperTips.com/user-interface/ios-5-customize-uinavigationbar-and-uibarbuttonitem-with-appearance-api.html</link>
		<comments>http://iPhoneDeveloperTips.com/user-interface/ios-5-customize-uinavigationbar-and-uibarbuttonitem-with-appearance-api.html#comments</comments>
		<pubDate>Mon, 02 Jan 2012 08:09:20 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[User Interface]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=10815</guid>
		<description><![CDATA[In a previous post, UIImage and resizableImageWithCapInsets, I explained how to use the method resizableImageWithCapInsets in a UIImage object to set cap insets, which specify areas of an image that are not to be changed when resizing or scaling an image. That post was a segue to this post, which will use images (and cap [...]]]></description>
			<content:encoded><![CDATA[<p>In a previous post, <a href="http://iphonedevelopertips.com/user-interface/ios-5-uiimage-and-resizableimagewithcapinsets.html" target="_blank">UIImage and resizableImageWithCapInsets</a>, I explained how to use the method <strong>resizableImageWithCapInsets</strong> in a <strong>UIImage</strong> object to set cap insets, which specify areas of an image that are not to be changed when resizing or scaling an image. That post was a segue to this post, which will use images (and cap insets) as part of the iOS 5 appearance API and customizing the navigation bar.</p>
<h5>Change UINavigationBar Background</h5>
<p>With the release of iOS 5, the <strong>UIAppearance</strong> protocol is used to access the appearance proxy for a class you would like to configure. Customization is done by sending messages to the target class appearance proxy. When changing the appearance of an object, all instances of the object can be updated or only specific instances within a container class.</p>
<p>Let&#8217;s see how this looks as it relates to updating the background color of a <strong>UINavigationBar</strong>, it&#8217;s text and the back button which will allow a user to return to a previous view controller.<br />
<span id="more-10815"></span></p>
<p>The image below is the gradient that I will use for the background:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2012/01/BlueGradientBackground.png" alt="" title="BlueGradientBackground" width="54" height="51" class="alignnone size-full wp-image-10819" /></p>
<p>Using the above image, I created a one pixel wide image for both portrait and landscape variations of the navbar, reason being, the height of the navar bar when the device is in portrait mode is 44, whereas the height for landscape is 32.</p>
<p>The images in my project are NavigationPortraitBackground.png and NavigationLandscapeBackground.png, both shown below:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2012/01/NavigationPortraitBackground.png" alt="" title="NavigationPortraitBackground" width="1" height="44" class="alignnone size-full wp-image-10822" /></p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2012/01/NavigationLandscapeBackground.png" alt="" title="NavigationLandscapeBackground" width="1" height="32" class="alignnone size-full wp-image-10837" /></p>
<p>I now create two <strong>UIImage</strong> objects:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Create image for navigation background - portrait</span>
UIImage <span style="color: #002200;">*</span>NavigationPortraitBackground <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;NavigationPortraitBackground&quot;</span><span style="color: #002200;">&#93;</span> 
                              resizableImageWithCapInsets<span style="color: #002200;">:</span>UIEdgeInsetsMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Create image for navigation background - landscape</span>
UIImage <span style="color: #002200;">*</span>NavigationLandscapeBackground <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;NavigationLandscapeBackground&quot;</span><span style="color: #002200;">&#93;</span> 
                              resizableImageWithCapInsets<span style="color: #002200;">:</span>UIEdgeInsetsMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>To set these images as the new backgrounds for all navigation bars, here&#8217;s all I need to do:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Set the background image all UINavigationBars</span>
<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UINavigationBar appearance<span style="color: #002200;">&#93;</span> setBackgroundImage<span style="color: #002200;">:</span>NavigationPortraitBackground 
                                     forBarMetrics<span style="color: #002200;">:</span>UIBarMetricsDefault<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UINavigationBar appearance<span style="color: #002200;">&#93;</span> setBackgroundImage<span style="color: #002200;">:</span>NavigationLandscapeBackground 
                                     forBarMetrics<span style="color: #002200;">:</span>UIBarMetricsLandscapePhone<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>In addition to the color, I can change the appearance of the text on all navigations bars as well:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Set the text appearance for navbar</span>
<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UINavigationBar appearance<span style="color: #002200;">&#93;</span> setTitleTextAttributes<span style="color: #002200;">:</span>
    <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span>
    <span style="color: #002200;">&#91;</span>UIColor whiteColor<span style="color: #002200;">&#93;</span>, UITextAttributeTextColor, 
    <span style="color: #002200;">&#91;</span>UIColor redColor<span style="color: #002200;">&#93;</span>, UITextAttributeTextShadowColor, 
    <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSValue</span> valueWithUIOffset<span style="color: #002200;">:</span>UIOffsetMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>, UITextAttributeTextShadowOffset, 
    <span style="color: #002200;">&#91;</span>UIFont fontWithName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Verdana-Bold&quot;</span> size<span style="color: #002200;">:</span><span style="color: #2400d9;">16</span><span style="color: #002200;">&#93;</span>, UITextAttributeFont, 
    <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>As the final step, I will update the back button with a custom image, which is shown below (the button is shown on a gray backdrop so you can see the white border):</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/12/blueButton.gif" alt="" title="blueButton" width="121" height="60" class="alignnone size-full wp-image-10771" /></p>
<p>Using what I described in the early post about cap insets, I can create a <strong>UIImage</strong> object and use the appearance API of the <strong>UIBarButtonItem</strong> to set the back button look. Notice that each value in the cap inset is set to 12, indicating all four corners are to hold steady at 12 pixels, even if the images stretched or resized.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Set back button for navbar</span>
UIImage <span style="color: #002200;">*</span>backButton <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;blueButton&quot;</span><span style="color: #002200;">&#93;</span>  resizableImageWithCapInsets<span style="color: #002200;">:</span>UIEdgeInsetsMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">12</span>, <span style="color: #2400d9;">12</span>, <span style="color: #2400d9;">12</span>, <span style="color: #2400d9;">12</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIBarButtonItem appearance<span style="color: #002200;">&#93;</span> setBackButtonBackgroundImage<span style="color: #002200;">:</span>backButton forState<span style="color: #002200;">:</span>UIControlStateNormal barMetrics<span style="color: #002200;">:</span>UIBarMetricsDefault<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>The last step is to create a button for the user interface that will push a new view controller, which will update the navigation bar to show the custom back button created above.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">...
&nbsp;
testButton <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIButton alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">80</span>, <span style="color: #2400d9;">30</span>, <span style="color: #2400d9;">160</span>, <span style="color: #2400d9;">44</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;  
<span style="color: #002200;">&#91;</span>testButton setTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Test Button&quot;</span> forState<span style="color: #002200;">:</span>UIControlStateNormal<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Notice cap insets are different from above</span>
UIImage <span style="color: #002200;">*</span>buttonImage <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;blueButton&quot;</span><span style="color: #002200;">&#93;</span>  resizableImageWithCapInsets<span style="color: #002200;">:</span>UIEdgeInsetsMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">16</span>, <span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">16</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>testButton 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>buttonPressed<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> forControlEvents<span style="color: #002200;">:</span> UIControlEventTouchUpInside<span style="color: #002200;">&#93;</span>;        
<span style="color: #002200;">&#91;</span>testButton setBackgroundImage<span style="color: #002200;">:</span>buttonImage forState<span style="color: #002200;">:</span>UIControlStateNormal<span style="color: #002200;">&#93;</span>;
&nbsp;
...</pre></div></div>

<p>An important concept to understand here, I&#8217;ve used the same image, blueButton.png, for both the custom back button as well as the button on the primary user interface &#8211; setting the cap insets specifies the rules, if you will, of how the image can be stretched, yet keep the look appropriate for the context in which the button will appear (that is, on the navbar or main UI).</p>
<p>A few screenshots follow that show the custom navigation bar, text and buttons in both portrait and landscape modes:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2012/01/navbar1.png" alt="" title="navbar1" width="340" height="160" class="alignnone size-full wp-image-10834" /></p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2012/01/navbar2.png" alt="" title="navbar2" width="340" height="120" class="alignnone size-full wp-image-10833" /></p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2012/01/navbar3.png" alt="" title="navbar3" width="500" height="160" class="alignnone size-full wp-image-10832" /></p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2012/01/navbar4.png" alt="" title="navbar4" width="500" height="110" class="alignnone size-full wp-image-10831" /></p>
<h5>Download the Xcode Project</h5>
<p>You can download the entire project source code <a href="http://iPhoneDeveloperTips.com/wp-content/uploads/2012/01/CustomNavbar.zip">here</a>.</p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/user-interface/ios-5-customize-uinavigationbar-and-uibarbuttonitem-with-appearance-api.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Get List of Installed International Keyboards</title>
		<link>http://iPhoneDeveloperTips.com/core-services/get-list-of-installed-international-keyboards.html</link>
		<comments>http://iPhoneDeveloperTips.com/core-services/get-list-of-installed-international-keyboards.html#comments</comments>
		<pubDate>Tue, 27 Dec 2011 07:22:24 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Core Services]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=10800</guid>
		<description><![CDATA[The NSUserDefaults object is typically used to save/restore your application related preferences, configuration data, etc &#8211; see Write and Read User Preferences for more information. In addition to application specifics, there is a system wide default list that is available to all applications, accessible using the class method standardUserDefaults in the NSUserDefaults object. To get [...]]]></description>
			<content:encoded><![CDATA[<p>The <strong>NSUserDefaults</strong> object is typically used to save/restore your application related preferences, configuration data, etc &#8211; see <a href="http://iphonedevelopertips.com/cocoa/read-and-write-user-preferences.html" target="_blank">Write and Read User Preferences</a> for more information. In addition to application specifics, there is a system wide default list that is available to all applications, accessible using the class method <strong>standardUserDefaults</strong> in the <strong>NSUserDefaults</strong> object.</p>
<p>To get a list of the system wide settings:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSDictionary</span><span style="color: #002200;">*</span> defaults <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSUserDefaults</span> standardUserDefaults<span style="color: #002200;">&#93;</span> dictionaryRepresentation<span style="color: #002200;">&#93;</span>;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Defaults: %@&quot;</span>, defaults<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p><span id="more-10800"></span><br />
A partial list of the output on my device follows:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Defaults: {
    AppleICUForce24HourTime = 0;
    AppleITunesStoreItemKinds =     (
        wemix,
        podcast,
        ...
    );
    AppleKeyboards =     (
        &quot;en_US@hw=US;sw=QWERTY&quot;,
        &quot;zh_Hant-HWR@sw=HWR&quot;,
        &quot;emoji@sw=Emoji&quot;
    );
    AppleKeyboardsExpanded = 1;
    AppleLanguages =     (
        en,
        &quot;zh-Hant&quot;,
        fr,
        de,
  ...
}</pre></div></div>

<p>As you can tell, the defaults returned above are contained within a dictionary object. The list of keyboards in the dictionary is stored as an <strong>NSArray</strong> object. To retrieve the array, simply request the object for the key &#8220;<strong>AppleKeyboards</strong>&#8221;</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>array <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSUserDefaults</span> standardUserDefaults<span style="color: #002200;">&#93;</span> objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;AppleKeyboards&quot;</span><span style="color: #002200;">&#93;</span>;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Keyboards: %@&quot;</span>, array<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>The output of all the installed international keyboards looks as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Keyboards: (
    &quot;en_US@hw=US;sw=QWERTY&quot;,
    &quot;zh_Hant-HWR@sw=HWR&quot;,
    &quot;emoji@sw=Emoji&quot;
)</pre></div></div>

<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/core-services/get-list-of-installed-international-keyboards.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>iOS 5 : UIImage and resizableImageWithCapInsets</title>
		<link>http://iPhoneDeveloperTips.com/user-interface/ios-5-uiimage-and-resizableimagewithcapinsets.html</link>
		<comments>http://iPhoneDeveloperTips.com/user-interface/ios-5-uiimage-and-resizableimagewithcapinsets.html#comments</comments>
		<pubDate>Tue, 20 Dec 2011 07:02:10 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[User Interface]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=10765</guid>
		<description><![CDATA[I recently began writing a short example to learn more about the iOS 5 Appearance API and customizing UINavigationBar objects. The goal was to add a custom background, title and text to the navbar. Once I had this working, to keep a consist look across my application, I began tweaking the buttons on the navbar [...]]]></description>
			<content:encoded><![CDATA[<p>I recently began writing a short example to learn more about the iOS 5 Appearance API and customizing <strong>UINavigationBar</strong> objects. The goal was to add a custom background, title and text to the navbar. Once I had this working, to keep a consist look across my application, I began tweaking the buttons on the navbar using the same Appearance API.</p>
<p>As I got further into the customization of the buttons, I ran into a method within <strong>UIImage</strong> that was introduced in iOS 5, <strong>resizableImageWithCapInsets</strong>. I found myself getting side-tracked from the original idea of navbar look and feel, to understanding how cap insets work. This post delves into what I learned.<br />
<span id="more-10765"></span></p>
<h5>Cap Insets with UIButton</h5>
<p>As the documentation describes, you use <strong>resizableImageWithCapInsets</strong> to add cap insets to an image, when the image is resized or scaled, cap areas are not affected. The best way to understand this is through an example.</p>
<p>Let&#8217;s assume I want all the buttons on my UI to have a similar look, a gradient with a white border. Below is the image used for the examples in this post (the button is shown on a gray backdrop so you can see the white border):</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/12/blueButton.gif" alt="" title="blueButton" width="121" height="60" class="alignnone size-full wp-image-10771" /></p>
<p>Depending on the context of where the button appears, its size may vary. The code to create a button with the image and the corresponding output follow:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">UIButton <span style="color: #002200;">*</span>button <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIButton alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">80</span>, <span style="color: #2400d9;">130</span>, <span style="color: #2400d9;">160</span>, <span style="color: #2400d9;">44</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;  
<span style="color: #002200;">&#91;</span>button setTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Test Button&quot;</span> forState<span style="color: #002200;">:</span>UIControlStateNormal<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Image with without cap insets</span>
UIImage <span style="color: #002200;">*</span>buttonImage <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;blueButton&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>button 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>buttonPressed<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> forControlEvents<span style="color: #002200;">:</span> UIControlEventTouchUpInside<span style="color: #002200;">&#93;</span>;        
<span style="color: #002200;">&#91;</span>button setBackgroundImage<span style="color: #002200;">:</span>buttonImage forState<span style="color: #002200;">:</span>UIControlStateNormal<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self view<span style="color: #002200;">&#93;</span> addSubview<span style="color: #002200;">:</span>button<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/12/button0.gif" alt="" title="button0" width="238" height="76" class="alignnone size-full wp-image-10767" /></p>
<p>As you can see, the button is stretched in all directions. Let&#8217;s change the code to include cap insets, however, before we do that, let&#8217;s look at the signature of the cap insets method:</p>
<p>- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets</p>
<p>Looking on step further, <strong>UIEdgeInserts</strong> is defined as:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">typedef</span> <span style="color: #a61390;">struct</span> <span style="color: #002200;">&#123;</span>
   CGFloat top, left, bottom, right;
<span style="color: #002200;">&#125;</span> UIEdgeInsets;</pre></div></div>

<p><strong>UIEdgeInsets</strong> is structure that specifies float values for each cap inset: top, left, bottom and right areas of an image. To apply this to the image for the button, here is all we need to do:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Image with cap insets</span>
UIImage <span style="color: #002200;">*</span>buttonImage <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;blueButton&quot;</span><span style="color: #002200;">&#93;</span>  
   resizableImageWithCapInsets<span style="color: #002200;">:</span>UIEdgeInsetsMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">16</span>, <span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">16</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>This requests that the left and right 16 pixels of the original image are not scaled or resized when stretching the image to accomodate the button size frame defined above. The end results is as shown below:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/12/button1.gif" alt="" title="button1" width="239" height="76" class="alignnone size-full wp-image-10768" /></p>
<h5>Cap Insets with UIBarButtonItem</h5>
<p>We can use the same image for a button on a navbar (I&#8217;ll show the specifics in the next post on customizing the navbar). Without specifying the cap insets, the button looks as follows:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/12/backButton0.gif" alt="" title="backButton0" width="340" height="80" class="alignnone size-full wp-image-10778" /></p>
<p>The code below specifies an image where 12 pixels on the top, left, bottom and right be preserved when stretching/resizing the button:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">UIImage <span style="color: #002200;">*</span>backButton <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;blueButton&quot;</span><span style="color: #002200;">&#93;</span>  
   resizableImageWithCapInsets<span style="color: #002200;">:</span>UIEdgeInsetsMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">12</span>, <span style="color: #2400d9;">12</span>, <span style="color: #2400d9;">12</span>, <span style="color: #2400d9;">12</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>The output nows looks as follows:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/12/backButton1.gif" alt="" title="backButton1" width="340" height="80" class="alignnone size-full wp-image-10779" /></p>
<h5>Next Post</h5>
<p>With that under our belt, in the next post I&#8217;ll continue with my original idea of customizing <strong>UINavigationBar</strong>, including background, title and text, as well as the buttons on the navbar.</p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/user-interface/ios-5-uiimage-and-resizableimagewithcapinsets.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

