<?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>Arash Payan &#124; Blog &#187; uitabbarcontroller</title>
	<atom:link href="http://arashpayan.com/blog/index.php/tag/uitabbarcontroller/feed/" rel="self" type="application/rss+xml" />
	<link>http://arashpayan.com/blog</link>
	<description>(Place witty tagline here)</description>
	<lastBuildDate>Sun, 23 May 2010 22:28:56 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Change iPhone/iPod app orientation within a UITabBarController</title>
		<link>http://arashpayan.com/blog/index.php/2008/09/04/change-iphoneipod-app-orientation-within-a-uitabbarcontroller/</link>
		<comments>http://arashpayan.com/blog/index.php/2008/09/04/change-iphoneipod-app-orientation-within-a-uitabbarcontroller/#comments</comments>
		<pubDate>Thu, 04 Sep 2008 23:59:33 +0000</pubDate>
		<dc:creator>Arash</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[uitabbarcontroller]]></category>

		<guid isPermaLink="false">http://arashpayan.com/blog/?p=25</guid>
		<description><![CDATA[The documentation for the official iPhone SDK is less than stellar, and as a result I&#8217;ve encountered lots of frustrating little problems. In particular, I wasn&#8217;t able to get my app to change its orientation when the phone was rotated. UIViewController has a method shouldAutorotateToInterfaceOrientation: that gets called when the iPhone/iPod orientation is changed by [...]]]></description>
			<content:encoded><![CDATA[<p>The documentation for the official iPhone SDK is less than stellar, and as a result I&#8217;ve encountered lots of frustrating little problems. In particular, I wasn&#8217;t able to get my app to change its orientation when the phone was rotated. <code>UIViewController</code> has a method <code>shouldAutorotateToInterfaceOrientation:</code> that gets called when the iPhone/iPod orientation is changed by the user, and overriding this method to return <code>YES</code> should cause your view to be rotated. You may have implemented this method and to your frustration, nothing happened when you rotated the device. The trick is that Cocoa only calls <code>shouldAutorotateToInterfaceOrientation:</code> on the top most <code>UIViewController</code>. Meaning if your program lives inside a <code>UITabBarController</code> (possibly with a <code>UINavigationController</code> inside some of the tab bar items), the <code>UIViewController</code> that the user is currently interacting with, is not necessarily the one receiving the method call.</p>
<p>In my app, everything lives inside a <code>UITabBarController</code>, so that&#8217;s what was receiving the <code>shouldAutorotateToInterfaceOrientation:</code> call. In order to get orientation changes to work you need to subclass <code>UITabBarController</code> (contrary to Apple doc recommendations) and override the method so you can return <code>YES</code> when it&#8217;s called. Below is some code for a simple <code>UITabBarController</code> subclass used inside a program that supports a horizontal view.</p>
<p>RotatingTabBarAppDelegate.h</p>
<div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; "><span style="color:#683821;">#import &lt;UIKit/UIKit.h&gt;<br />
</span><br />
<span style="color:#683821;">#import &quot;RotatingTabBarController.h&quot;<br />
</span><br />
<span style="color:#236e25;">/*<br />
&nbsp;Nothing special implemented in our delegate for this example.<br />
&nbsp;*/</span><br />
<span style="color:#881350;">@class</span> RotatingTabBarAppViewController;</p>
<p><span style="color:#881350;">@interface</span> RotatingTabBarAppAppDelegate : <span style="color:#400080;">NSObject</span> &lt;UIApplicationDelegate&gt; {<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#881350;">IBOutlet</span> UIWindow *window;<br />
}</p>
<p><span style="color:#881350;">@property</span><span style="color:#003369;"> </span>(<span style="color:#881350;">nonatomic</span>, <span style="color:#ff0000;">retain</span>) UIWindow *window;</p>
<p><span style="color:#881350;">@end</span></div>
<p>RotatingTabBarAppDelegate.m</p>
<div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; "><span style="color:#683821;">#import &quot;RotatingTabBarAppAppDelegate.h&quot;<br />
</span><br />
<span style="color:#881350;">@implementation</span> RotatingTabBarAppAppDelegate</p>
<p><span style="color:#881350;">@synthesize</span> window;</p>
<p><span style="color:#236e25;">/*<br />
&nbsp;We&#8217;ll programmatically lay out a simple GUI for this demonstration.<br />
&nbsp;*/</span><br />
- (<span style="color:#881350;">void</span>)<span style="color:#6c0540;">applicationDidFinishLaunching:</span>(UIApplication *)application { &nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#236e25;">// Just make two empty view controllers for fun<br />
</span>&nbsp;&nbsp;&nbsp;&nbsp;UIViewController *tab1 = [[UIViewController <span style="color:#ff0000;">alloc</span>] <span style="color:#6c0540;">init</span>];<br />
&nbsp;&nbsp;&nbsp;&nbsp;tab1.tabBarItem = [[UITabBarItem <span style="color:#ff0000;">alloc</span>] <span style="color:#6c0540;">initWithTabBarSystemItem:</span>UITabBarSystemItemTopRated <span style="color:#6c0540;">tag:</span><span style="color:#0000ff;">0</span>];</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;UIViewController *tab2 = [[UIViewController <span style="color:#ff0000;">alloc</span>] <span style="color:#6c0540;">init</span>];<br />
&nbsp;&nbsp;&nbsp;&nbsp;tab2.tabBarItem = [[UITabBarItem <span style="color:#ff0000;">alloc</span>] <span style="color:#6c0540;">initWithTabBarSystemItem:</span>UITabBarSystemItemSearch <span style="color:#6c0540;">tag:</span><span style="color:#0000ff;">1</span>];<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#236e25;">// Now create an instance of our rotating tab bar controller<br />
</span>&nbsp;&nbsp;&nbsp;&nbsp;RotatingTabBarController *tbc = [[RotatingTabBarController <span style="color:#ff0000;">alloc</span>] <span style="color:#6c0540;">init</span>];<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#236e25;">// Add the two view controllers to the tab bar<br />
</span>&nbsp;&nbsp;&nbsp;&nbsp;[tbc <span style="color:#6c0540;">setViewControllers:</span>[<span style="color:#400080;">NSArray</span> <span style="color:#6c0540;">arrayWithObjects:</span>tab1, tab2, <span style="color:#881350;">nil</span>]];<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#236e25;">// Add the tab bar controller&#8217;s view to the window<br />
</span>&nbsp;&nbsp;&nbsp;&nbsp;[window <span style="color:#6c0540;">addSubview:</span>tbc.view];<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#236e25;">// Make our program visible<br />
</span>&nbsp;&nbsp;&nbsp;&nbsp;[window <span style="color:#6c0540;">makeKeyAndVisible</span>];<br />
}</p>
<p>
- (<span style="color:#881350;">void</span>)<span style="color:#6c0540;">dealloc</span> {<br />
&nbsp;&nbsp;&nbsp;&nbsp;[window <span style="color:#ff0000;">release</span>];<br />
&nbsp;&nbsp;&nbsp;&nbsp;[<span style="color:#881350;">super</span> <span style="color:#6c0540;">dealloc</span>];<br />
}</p>
<p>
<span style="color:#881350;">@end</span></div>
<p>RotatingTabBarController.h</p>
<div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; "><span style="color:#683821;">#import &lt;UIKit/UIKit.h&gt;<br />
</span><br />
<span style="color:#236e25;">/*<br />
&nbsp;The subclass doesn&#8217;t need any new methods or members.<br />
&nbsp;*/</span><br />
<span style="color:#881350;">@interface</span> RotatingTabBarController : UITabBarController {</p>
<p>}</p>
<p><span style="color:#881350;">@end</span></div>
<p>RotatingTabBarController.m</p>
<div style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; "><span style="color:#683821;">#import &quot;RotatingTabBarController.h&quot;<br />
</span><br />
<span style="color:#881350;">@implementation</span> RotatingTabBarController</p>
<p><span style="color:#236e25;">/*<br />
&nbsp;Just override this single method to return YES when we want it to.<br />
&nbsp;*/</span><br />
- (<span style="color:#881350;">BOOL</span>)<span style="color:#6c0540;">shouldAutorotateToInterfaceOrientation:</span>(UIInterfaceOrientation)interfaceOrientation {<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#236e25;">// Always returning YES means the view will rotate to accomodate any orientation.<br />
</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#881350;">return</span> <span style="color:#881350;">YES</span>;<br />
}</p>
<p><span style="color:#881350;">@end</span></div>
<p>The one thing this doesn&#8217;t address, is when you only want certain views in your program to rotate. In that case you just need to make your <code>UITabBarController</code> ask the currently visible view controller in the program if the app should rotate, and return that <code>BOOL</code>.</p>
]]></content:encoded>
			<wfw:commentRss>http://arashpayan.com/blog/index.php/2008/09/04/change-iphoneipod-app-orientation-within-a-uitabbarcontroller/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>
