Weird landscape UITabBarController Application startup - iphone

My application is quite simple, but I have some problems when it starts. I setted in the Info.plist to be landscaped, but it seems to ignore the order. In fact, when the app is loading the Simulator is landscaped, but then it returns in portrait mode.
This is the hierarchy of the views and controllers:
MainViewController (extends UITabBarController just to override shouldAutorotateToInterfaceOrientation:)
Three extended UITableViewControllers as tabs (also those have the shouldAutorotateToInterfaceOrientation correctly setted up).
If I kinda force the orientation of the device to Landscape with:
[[UIDevice currentDevice] setOrientation: UIInterfaceOrientationLandscapeRight];
Then for an instant the Simulator flashes in portrait mode, and then it goes landscaped. The problem is that in this way, the auto-rotation animations gets started, which is something I cannot tollerate. I just want a fixed, landscaped application.
Any clues? Am I missing something?

Try the following. Not sure why it does not work for you
1) set the key UIInterfaceOrientation
to UIInterfaceOrientationLandscapeRight in your .plist file
2) override your UITabBarController shouldAutorotateToInterfaceOrientation() method; in the following the code only deals with tab zero and one, and only with one controller: if you have a navigation controller and you want to control different controllers that may be on the stack, you have to modify the code accordingly
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
BOOL tabZeroController = [[[self.viewControllers objectAtIndex:0] visibleViewController] isKindOfClass:[YourTabZeroTableViewController class]];
BOOL tabOneController = [[[self.viewControllers objectAtIndex:1] visibleViewController] isKindOfClass:[YourTabOneTableViewController class]];
if(self.selectedIndex == 0 && tabZeroController)
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
if(self.selectedIndex == 1 && tabOneController)
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
return NO;
}
2) setting
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
in your delegate's applicationDidFinishLaunching() method is only required for the simulator, not on a device
3) add the following shouldAutorotateToInterfaceOrientation(method) in your controllers
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
4) run the app on your device and verify that it works properly by using the Hardware menu item Rotate Left and Rotate Right. You should see the display in landscape-mode.

maybe this can help
http://www.dejoware.com/blogpages/files/iphone_programming_landscape_view_tutorial.html

Related

How to change the orientation of the app without changing the device orientation in iphone app

I want to change the orientation of the app without changing the device orientation in iphone app.
I want to change my view from portraid mode to landscap mode programmatically.
And also want to know that will this be accepted by the apple store or not ?
Thanks
Now I got the solution from other that is as follow
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
when you add this line at that time one warning appear and for remove this warning just add bellow code on you implementation file..
#interface UIDevice (MyPrivateNameThatAppleWouldNeverUseGoesHere)
- (void) setOrientation:(UIInterfaceOrientation)orientation;
#end
and after that in bellow method just write this code if required..
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
But now want to know is this accepted by apple app store or not ?
thanks
use this line for programmatically change orientation...
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
and also when you add this line at that time one warning appear and for remove this warning just add bellow code on you implementation file..
#interface UIDevice (MyPrivateNameThatAppleWouldNeverUseGoesHere)
- (void) setOrientation:(UIInterfaceOrientation)orientation;
#end
and after that in bellow method just write this code if required..
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
// return NO;
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
i hope this help you..
:)
Add a class variable
Bool isInLandsCapeOrientation;
in viewDidLoad
set this flag to
isInLandsCapeOrientation = false;
Add the following function
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (!isInLandsCapeOrientation) {
return (UIInterfaceOrientationIsPortrait(interfaceOrientation));
}else {
return (UIInterfaceOrientationIsLandscape(interfaceOrientation));
}
}
To changing orientation from portrait to landscape, let it happens on a button action
- (IBAction)changeOrientationButtonPressed:(UIButton *)sender
{
isInLandsCapeOrientation = true;
UIViewController *viewController = [[UIViewController alloc] init];
[self presentModalViewController:viewController animated:NO];
[self dismissModalViewControllerAnimated:NO];
}
This works fine for me.
To change Orientation portraid mode to landscap mode use this code
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
use this code for programmatically change orientation...
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
If you want to change the particular view only in landscape..then u can try the following in its viewDidLoad
float angle = M_PI / 2;
CGAffineTransform transform = CGAffineTransformMakeRotation(angle);
[ [self.view] setTransform:transform];
The documentation describes the orientation property as being read-only, so if it works, I'm not sure you can rely on it working in the future (unless Apple does the smart thing and changes this; forcing orientation changes, regardless of how the user is currently holding their device, is such an obvious functional need).
As an alternative, the following code inserted in viewDidLoad will successfully (and somewhat curiously) force orientation (assuming you've already modified you shouldAutorotateToInterfaceOrientation ):
if (UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation]))
{
UIWindow *window = [[UIApplication sharedApplication] keyWindow];
UIView *view = [window.subviews objectAtIndex:0];
[view removeFromSuperview];
[window addSubview:view];
}
Clearly, this does it if the user is currently holding their device in portrait orientation (and thus presumably your shouldAutorotateToInterfaceOrientation is set up for landscape only and this routine will shift it to landscape if the user's holding their device in portrait mode). You'd simply swap the UIDeviceOrientationIsPortrait with UIDeviceOrientationIsLandscape if your shouldAutorotateToInterfaceOirentation is set up for portrait only.
For some reason, removing the view from the main window and then re-adding it forces it to query shouldAutorotateToInterfaceOrientation and set the orientation correctly. Given that this isn't an Apple approved approach, maybe one should refrain from using it, but it works for me. Your mileage may vary. But this also refers to other techniques, too. Check
SO discussion

auto-rotation in iOS5/6?

I updated to Xcode 4.5 and am working with iOS6--a mistake I will definitely not make next time there's an update; it's been sort of nightmarish for somebody so new to iOS--and I've just noticed an app I'm working on is autorotating. I never noticed it autorotatin before the update, but it's also possible I just didn't rotate the phone while testing, so I can't be sure. I've added the following code to the main UIViewController and it's still rotating:
- (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation
{
return NO;
}
Is this the right way to disable autorotation? If it is, then maybe there's some change in iOS6 and I'll have to wait until the full release to find out. But if I've gotten it wrong, what code should I use instead?
Thanks, as always, for your help.
EDIT: Here's the code I changed it to, but it's still rotating. Have I gotten it wrong?
- (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation
{
if (interfaceOrientation == UIInterfaceOrientationPortrait)
{
return YES;
}
else
{
return NO;
}
}
that is because there was never a success. You should choose one of the orientations.
Hold command and click on UIInterfaceOrientation you will see an enumeration of the possible options.
then you can test against those to decide your YES Scenario.
I may have originally misunderstood your problem. It seems that you may have been saying that your app is allowing rotation. but the code should disallow that.
I was thinking you were saying it was still firing the code. Trying to find a Yes
One thing to think about. is there may be more than one view controller available. perhaps your code is not being hit.
A couple of possible issues for this.
Your code is not even being used. because the view is being allocated as UIViewController as opposed to your custom view controller.
You code is being used but that View controller is not the one being asked about the Orientation. therefore that specific code is not being hit.
A bad build keeps putting the wrong assemblies onto the device.
Your solutions can be as follows.
Ensure your code is the one being allocated. Either there is a direct alloc on your custom class. or the xib file is inflating it. Check out the Identity Inspector when you have your xib file open. select your View Controller and ensure that custom class is set to your class type
Look at the hierarchy. what other view controllers are there. Perhaps one of those are telling the app it can autorotate to any orientation.
Find your "DerivedData" folder and remove it entirely. Sometimes this works from the organizer. other times you need to delete directly off the disk. Then clean and build again.
Also another solution could be as simple as setting the settings in the Project file.
Select your project file from the file browser and you will see the iPad and iPod settings in the summary. You can "UnPress" buttons for the orientations that you want to disallow. and any view controllers that you do not otherwise code orientation into. will use these by default.
My apologies for the confusion.
Update
I commonly use this code to handle my autorotation.
It not only differentiates the ipad from the other ios devices, but it also forwards the request onto presented controllers so a view that is shown modal may respond how it wants.
Orientation is a pain when you dont understand it :)
// Detect iPad
#define IS_IPAD() ([[UIDevice currentDevice] respondsToSelector:#selector(userInterfaceIdiom)] ? \
[[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad : NO)
// Set preferred orientation for initial display
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
if (IS_IPAD()){
return UIInterfaceOrientationLandscapeRight;
}
else {
return UIInterfaceOrientationPortrait;
}
}
// Return list of supported orientations.
- (NSUInteger)supportedInterfaceOrientations{
if (self.presentedViewController != nil){
return [self.presentedViewController supportedInterfaceOrientations];
}
else {
if (IS_IPAD()){
return UIInterfaceOrientationMaskLandscapeRight;
}
else {
return UIInterfaceOrientationMaskAll;
}
}
}
// Determine iOS 6 Autorotation.
- (BOOL)shouldAutorotate{
UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
// Return yes to allow the device to load initially.
if (orientation == UIDeviceOrientationUnknown) return YES;
// Pass iOS 6 Request for orientation on to iOS 5 code. (backwards compatible)
BOOL result = [self shouldAutorotateToInterfaceOrientation:orientation];
return result;
}
// handle iOS 5 Orientation as normal
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
// Return YES for supported orientations
if (self.presentedViewController != nil){
return [self.presentedViewController shouldAutorotate];
}
else {
if (IS_IPAD()){
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
else {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
}
}
Rotation APIs have changed in iOS6. The new API's are apparently supposed to be opt in however they seem to be enabled for all debug builds on simulator or device. To register for the new API calls throw something like this in your APP Delegates didFinishLoading method.
//Register for new API rotation calls
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"UIApplicationSupportedInterfaceOrientationsIsEnabled"];
At the heart of the rotation changes are two methods (theres a third but Im still figuring this out myself)
- (BOOL)shouldAutorotate
- (NSUInteger)supportedInterfaceOrientations
You need to override these methods in your windows rootViewController. This means you need to subclass UINavigationController or UITabBarController if either is your root controller (this seems bizarre to me, but Apple says Jump).
Now if all you want to do is keep your app in portrait implement the two methods and you're golden.
- (BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
Note that apple has further added to the confusion by adding interface orientation masks, ie. UIInterfaceOrientationMaskPortrait != UIInterfaceOrientationPortrait. If you return UIInterfaceOrientationPortrait instead the behaviour will be different. Also you can combine masks the same way you combine orientations so if you wanted to support both portrait orientations you could use.
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}
That should work for forcing a portrait orientation. Now if you if you want do do something like allow a child controller to use a different orientation I have no clue.
A very simple way to handle autorotation in both iOS6 and iOS5 is to use supportedInterfaceOrientations & shouldAutorotateToInterfaceOrientation. There are some macros to make it just a line of code. UIInterfaceOrientationIsLandscape & UIInterfaceOrientationMaskLandscape.
I discovered UIInterfaceOrientationMaskLandscape & UIInterfaceOrientationMaskPortrait by auto-completion in xCode. It is not in the Apple docs about autorotation.
Add this code block to your root ViewController to force it to support only landscape mode.
//iOS6 code to support orientations
- (NSUInteger)supportedInterfaceOrientations
{
return (UIInterfaceOrientationMaskLandscape);
}
//iOS5 code to support orientations
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
{
return (UIInterfaceOrientationIsLandscape(orientation));
}
For iOS6 you can use the following to detect orientation:
UIInterfaceOrientationMaskLandscape
UIInterfaceOrientationMaskPortrait
UIInterfaceOrientationMaskLandscapeRight
UIInterfaceOrientationMaskLandscapeLeft
UIInterfaceOrientationMaskPortraitUpsideDown
UIInterfaceOrientationMaskAllButUpsideDown
UIInterfaceOrientationMaskAll
For iOS5 and below you can use the following to detect orientation:
UIInterfaceOrientationIsLandscape (A macro)
UIInterfaceOrientationIsPortrait
UIInterfaceOrientationIsLandscapeRight
UIInterfaceOrientationIsLandscapeLeft
UIInterfaceOrientationIsPortraitUpsideDown

Any idea why this view is not rotating?

I'm working on a photo view controller to display some photos in portrait and landscape view as well. What I did is edit the -(BOOL)shouldAutorotateToInterfaceOrientation orientation with the code below but when testing in xcode (menu hardware > Rotate right) the view does not rotate in landscape. Any thing wrong in what I did ?
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
The view controller is a part of a tab based iphone app and this view is not root view: is this the problem ?
Also check your supported Orientations. For XCode 4 (Project->Summary(Tab)->Supported Device Orientation->Desired Orientations).
My first idea would be to just return true; to make sure there's no issue with the parameters being passed in / your comparison (it looks good to me but you never know).
Next would be, if this view is not directly attached to the window (or other top-level object if you're using xib's) you may have to also return true in any parent views. For the sake of testing you might just want to overwrite:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIIinterfaceOrientation)interfaceOrientation
{
return true;
}
for all view (controllers) in the tree.
this could be just a typo, but its not UIIinterfaceOrientation, its UIInterfaceOrientation
The method you want to override is shouldAutorotateToInterfaceOrientation. Your method signature is missing the Orientation on the end.
Make sure that the ViewController in which you implement this method, is the window's rootViewController.
Also, using return (interfaceOrientation != UIIinterfaceOrientationPortraitUpsideDown) is nicer to read. ;)
Does -shouldAutorotateToInterfaceOrientation get called at all? Try logging something in this method...
Your looking in the wrong place: in your RootViewController.m file look for the following code:
#elif GAME_AUTOROTATION == kGameAutorotationUIViewController
//
//lots of useless comments
//
return (UIInterfaceOrientationIsPortrait(interfaceOrientation) ); //THIS LINE HERE
the line that says return (UIInterface... Portrait) is the line that determines your app's rotating capabilities. You can change this to whatever to allow you to be able to rotate completely, keep it at a certain orientation, or whatever you desire...
also in this
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIIinterfaceOrientation)interfaceOrientation
{
/* return (interfaceOrientation == UIIinterfaceOrientationPortrait || interfaceOrientation == UIIinterfaceOrientationLandscapeLeft || interfaceOrientation == UIIinterfaceOrientationLandscapeRight); */ //GET RID OF ALL THIS CRAP
return true; //do this instead, and if this doesn't work, try return YES;
}
I'm not sure it's a type error or edition difference, in my latest Xcode the method is like this:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
and it works well, are you missing the "to" of the argument?

Handle orientation when showing a view

I have a few changes that get made to a UIView when the orientation changes. This works fine. The problem arises when adding the view after the phones orientation is already switched. This causes none of the rotate methods to be called and therefor does not give me the opportunity to make changes.
What would be the correct way of handling this, probably in ViewDidLoad? Would I be able to detect the current orientation at that point?
Bare in mind that its a few minor changes that I would need to make, so I dont want to load a different nib or anything like that
Thanks you very much :)
EDIT* just to clear things up: As I mentioned, the view is not even instantiated yet when the device orientation changes. The orientation changes to landscape -> the user clicks a button that shows another view -> this new view gets created and shown, but its default positioning is for portrait orientation -> when the view is shown, the elements I rearrange in the willAnimateRotationToInterfaceOrientation method are in the wrong position.
Typically I put the animations that occur when the user rotates the device (manipulating the frames of views mostly) in the willAnimateToInterfaceOrientation method. In it's skeleton form it would look like this:
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration
{
//NSLog(#"willAnimateRotationToInterfaceOrientation: %d", toInterfaceOrientation);
if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation))
{
// portrait
}
else
{
// landscape
}
}
EDIT: In situations where I need to remember the device rotation for future use, I set up an ivar in my view controller class called currentOrientation (type int), and then do this:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
//NSLog(#"shouldAutorotateToInterfaceOrientation: %d", toInterfaceOrientation);
if (toInterfaceOrientation == UIDeviceOrientationPortrait || toInterfaceOrientation == UIDeviceOrientationPortraitUpsideDown ||
toInterfaceOrientation == UIDeviceOrientationLandscapeLeft || toInterfaceOrientation == UIDeviceOrientationLandscapeRight)
{
currentOrientation = toInterfaceOrientation;
}
return YES;
}
Then while running the methods in the view controller, I know which orientation the device is in.

Programmatically change device orientation in iPhone

I have navigation based app, when I click on any row in root view, the next view should come in Landscape mode.
I am not able to find a proper way to implement this. What I tried is:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
But this is not working. The view should open only in Landscape mode (not when user rotate it).
My sandbox app:
https://github.com/comonitos/programatical_device_orientation
The solution is easy:
in interface (h file) :
BOOL rotated;
in implementation (m file):
1. rewrite
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return rotated;
}
2 call [self setup]
-(void) setup {
rotated = YES;
[[UIDevice currentDevice] setOrientation:UIDeviceOrientationLandscapeLeft];
rotated = NO;
}
Try designing your view in landscape mode and in it's view controller, and in
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
.....
}
return No for portrait orientation and YES for landscape orientation
This will display your view in landscape mode.
you can force your Device to change Orientation.......using magic line in View will Appear
[[UIDevice currentDevice] setOrientation:UIDeviceOrientationLandscapeRight];
All the Best..
Here is some code from my landscape-view based app:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
if (interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft){
return YES;
}
else {
return NO;
}
}
Put this in a landscape-based viewcontroller to have it display only in landscape mode and to switch between landscape left and landscape right. You will also need your xib to be landscape based.