Problem in set view in Landscape mode - iphone

I have a tab based application with 4 views associated with 4 tabs. I want all 3 views in portrait form and only one landscape form.
I have done these settings for that particular view:
Set the required value in Supported interface orientations of info.plist.
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight)
}
In IB set the orientation attribute as landscape.
In -(void)viewDidLoad I have set self.view.frame=CGRectMake(0, 0, 480, 320);
After done all above setting, its still not coming in landscape mode.

If I understand your question correctly, and please correct me if I do not, then you want to have 3 views in portrait all of the time and one view in landscape all of the time. To accomplish this, set the orientation of the status bar for each viewController in viewWillAppear:
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeRight;
}

Related

Auto Rotate while switching between View Controllers

I have a problem in programming. I have two view controllers. The first runs only in landscape left / right, the other runs only in portrait. If I switching between the views the orientation do not change automaticly. Only when I turn the device the orientation is changing. After that the orientation is fixed. Is it possible to change the orientation automaticly when I switching between the controllers?
As Example when I press the BackButton in the Landscape View Controller and it switch to the Portrait View Controller that the orientation automaticly switch from Landscape to Portrait?
Sorry for my bad english, it is not my native language.
Thanks,
Tim
Try the following Code for Portrait in Second View.
- (void)viewWillAppear:(BOOL)animated {
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];
}
Try the following Code for Landscape in First View.
- (void)viewWillAppear:(BOOL)animated {
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscape];
}
if(UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation))
{
self.view.frame = CGRectMake(0,0,1024,768);
}
else
{
self.view.frame = CGRectMake(0,0,768,1024);
}
----use this code where you want to change---

Unable to lock screen orientation to portrait only in xcode 4?

In xcode 4 I am unable to lock the screen orientation to only portrait even though I have portrait selected only. How to I do this programmatically?
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
this code works in a new blank project
The accepted answer did not work for me either. This did:
In the properties file for your app (YOURAPPNAME-Info.plist), located in the "supporting files" group, there is an array called "Supported interface orientations". Remove both landscape values from the array and your app will be locked in portrait orientation.
For each view controller (or just the parent), implement -shouldAutorotateToInterfaceOrientation: Set the default orientation to portrait and then do:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return NO;
}
Alternately, you can do:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return interfaceOrientation == UIInterfaceOrientationPortrait;
}
which will allow a rotation to portrait but not away from it.

iPhone Landscape Mode returns incorrect view sizes

Getting a view's bounds rect while in Landscape Mode returns the wrong sizes:
(I know there's been a lot of posts on Landscape mode, but nothing on this problem.)
Starting from a new UIView Template Project, I'm adding a single UIView to the ViewController in Interface Builder (in Landscape Mode) and setting the view size to width=400, height=200;
However when I add a breakpoint in the ViewController
Code:
- (void)viewDidAppear:(BOOL)animated {
CGRect viewRect = [testView bounds];
}
the sizes are w=220, h=380!
(Even though the view clearly is correct on the screen)
In myViewController.m I've set:
- (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight); // home button on right
}
In myAppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
application.statusBarOrientation = UIInterfaceOrientationLandscapeRight; //home on right
[window addSubview:viewController.view];
[window makeKeyAndVisible];
return YES;
}
and in the info.plist I've set: Initial interface orientation = Landscape (right home button)
I'm not trying to rotate the view with the iPhone, it is meant to be fixed in Landscape only.
Is there a workaround for this? I need to create some CALayers dependent on the correct view size.
Thanks
Steve
Sounds like a status-bar-related issue. That said I don't have a good solution for you.
In addition to looking at the frame, you can look at the bounds. Sometimes this gives better info.

Tabbar Nav app, allowing one view to rotate while others do not

I have a tab bar application in which i have 3 diffrent views each with there own view controller.
In the tab bar code i have this, to handle rotation.
#import "RotatingTabBarController.h"
#implementation RotatingTabBarController
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
#end
Then in the 2nd view controller that i want to rotate depending on device orientation i have:
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
And For the other two views that i do not want to rotate i have this method set.
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
The PROBLEM: so this works fine in view 1 and view 3 when u rotate the device they stay in portrait mode which is desired. When in view 2 i rotate to landscape, the view does as expected and rotates to landscape. BUT when click view 1 or view 3 tab while in lanscape mode in view 2, View 1 and View 3 are in landscape mode.
I can't figure out how to force them in portrait even if view 2 rotates to lanscape.
Any one know how to do this?
There's a big discussion[1] on this dating back from 2008 until now (look at comments down a few pages) -- summarily it seems like
application.statusBarOrientation = UIInterfaceOrientationLandscapeRight;
or
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
or
[application setStatusBarOrientation: UIInterfaceOrientationLandscapeRight animated:NO];
will let you force it to landscape -- you would want to do this when the user goes back to your landscapey view(s) programmatically.
[1] iPhone app in landscape mode, 2008 systems

iPhone screen rotates at random ?

I use a tabBar Controller as root controller. It has 4 tabs and each of its ViewControllers has
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return YES;
}
as well as the tabBarController itself.
But when I rotate the device (real or simulator), the screen turns at random! If it doesn't turn when I open the application it would have the same behavior until I quit the app.
I tried to add the 4 viewControllers one by one in IB to see if one was problematic, but I obtained the same issue. It only always turns when there is no tabs at all!
Please tell me if you have any ideas. Thanks!
You set every view controller to say that it responds to any possible orientation. Therefore, every view will attempt to rotate to every orientation.
Views don't really automatically rotate. You usually have to manage the placement of subview programmatically in all but the simplest views.
If you have no custom orientation code, you're probably seeing the views try to draw the portrait view in the landscape frame or vice versa. If you have autoresize subviews set your subviews will appear to scatter across the screen in a seemingly random pattern. The more you change orientation, the more random the placement becomes.
For complex views, I like to create separate viewController/view pairs for each orientation. Then I put the views in a nav controller. As the orientation changes, each view controller will push or pop the appropriate view controller for the coming orientation onto/off the stack. To the user, this looks like a single view is gracefully redrawing itself. (This is especially useful if you have non-standard UI elements that have to be manually rotated with transforms)
You have to subclass UITabBarController and implement shouldAutorotateToInterfaceOrientation:
Actually, I just want my first tab view controller to rotate. So I put this code in my custom tabBarController :
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
if (self.selectedIndex == 0) {
return toInterfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
}else {
return toInterfaceOrientation == UIInterfaceOrientationPortrait;
}
}
but I had the same problem. I use a custom orientation code for my first tab view controller when turning to landscape. Called with the following function in my custom tabBarcontroller:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (toInterfaceOrientation == UIInterfaceOrientationPortrait) {
//rotation to Portrait
lastOrientation = toInterfaceOrientation;
[[UIApplication sharedApplication] setStatusBarHidden:NO animated:NO];
[self.selectedViewController willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
}
else if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
if (!UIInterfaceOrientationIsLandscape(lastOrientation)) {
//rotation to Landscape
[self.selectedViewController willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
}
lastOrientation = toInterfaceOrientation;
}
}
I found that if you set the selected tab programmatically the tabViewController rotates erratically.