I have a problem with my tabbar application..
My app can change orientation but when I'm on landscape in (for exemple) my first tab, everything work great, everything is replace but if I stay in landscape and I go in an other tab, the app still on landscape view (that's good) but the objects (labels, buttons, images,..) are not where it should be...
For check if the devise is in landscape or portrait, I use this
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
If (toInterfaceOrientation == UIInterfaceOrientationPortait)
{
image1.hidden = NO;
}
else
{
image2.hidden = YES;
}
}
It work but I have to change the orientation when I want to go on an other...
Hope someone can help me..
Thanks :)
Make sure all your viewcontrollers within the tabs are returning YES for landscape orientation in
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
Related
In my Application I'm using a navigation controller to mange views. My login page support both portrait and landscape views. When user logged in my second view is home and it support only landscape mode. What I want to do is when user login to the home using portrait view home page should appear in landscape view even though the device in portrait.
So what I did was I change the status bar orientation in to landscape int the home page's viewWillAppear method as follows;
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeRight animated:NO];
UIDeviceOrientation orien = [[UIDevice currentDevice] orientation];
}
also I have override the shouldAutorotateToInterfaceOrientation as follows
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
My problem is even the status bar changed to landscape my UIViewController (home) is remains in landscape mode. When i'm debugging I found that even I change the status bar orientation to landscape,[[UIDevice currentDevice] orientation] returns portrait. I went through internet whole day. And implement lot of solutions proviede by other but my whole day wasted. can some one guide me to solve these issue.
you just need to like:-
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
return YES;
}
else
{
return NO;
}
}
for particular class you want to open landscape Only
in ios6:-
-
(BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
Apple does not want you to force the orientation of the device. There is a trick though.
Unfortunately I do not have access to my code.
1. Your app in general supports all orientations.
2. All view controllers only return their supported interface orientation in their overwrites respectivly (in supportedInterfaceOrientations).
3. All view controllers return YES in shouldAutorotateToInterfaceOrientation only for their supported orientations.
That is fine. But it would still require the user to actually rotate the device. Otherwise the whole orientation change mechanism would not be invoked.
Now, when you want to force the orientation change, do the following:
4. Use setStatusBarOrientation to set the orientation before the next view controller is displayed.
That alone would not do anything. Plus it would not take any effect if the next view controller is pushed. It would work fine only when the next view controller is presented modally.
5a. So if you want to present the rotated view controller modally, then do it.
5b. If you still need to push it then:
5b1. Create an empty UIViewController instance. alloc/init will do.
5b2. Present it modally
5b3. Dismiss it modally
Now, the new view controller was not even visible to the user but the device - here comes the magic - is rotated now.
5c4. Next push the view controller that you want to display roated.
And vice versa on your way back :)
All the above gets more complicated when you use a tab bar. Do you use a tab bar?
I managed to get that working with a tab bar which I had to subclass to overwrite its rotation methods. In an app without tab bar I subclassed UIApplication (!) but don't rembember wether that was really required or wether I did that out of convenience (instead of aplying the changes to 50+ view controllers). But in principle the above is it that does the trick.
PS: You find a more detailled answer here along with code samples:
Presenting Navigation Controller in Landscape mode is not working ios 6.0
You can try with
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
and
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)toInterfaceOrientation {
return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}
or try presenting it as a Modal, rather than pushing it.
Yes, I know this question has been asked a lot of times, but I can not find anything that helps me any further.
Using a navigation controller with 3 viewcontrollers, I need to retain the data from previous screens so I do not use segues but like this:
// When button is pressed
- (IBAction)changeView:(id)sender {
NSLog(#"Skipped connection screen");
ViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"SecondView"];
[self.navigationController pushViewController:vc animated:YES];
}
where SecondView is the identifier of the view controller that should appear. Since I only want the rotation to be at horizontal right, I add this snippet of code at the top of every .m file I have for my views:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
return (self.interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
and in my project-Info.plist I have added Initial interface orientation = Landscape (right home button and in my project settings I have added support for this orientation only.
The problem is that when running on the iPhone, the orientation changes from landscape if I turn my phone either way. When I try to turn it back it just wont. I would like to make sure this application is never able to rotate away from landscape right.
Any suggestions? Thank you very much in advance.
I think if you will add below key to your .plist file then it will be fixed.
"Supported interface orientations"
Value of this key would be "Landscape (right home button)" or whatever you want so your application will support only that specified orientations.
Also add this code into every view controller.
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)toInterfaceOrientation{
return (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
You should use the argument in your 'return' code:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
return (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
I did some research but cannot seem to find the answer to have my rootViewController of my navigationController be correct on start up. My original question was here: launch orientation of iPad is incorrect for landscape orientation (upside down).
In my info.plist, I have it set to support both landscape orientations. If I change my rootViewController to be:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// return UIInterfaceOrientationIsLandscape(interfaceOrientation); // original that does not work
return YES;
}
then my app starts in the right orientation. However, I do not want to support portrait modes. I only want to support landscape modes. I thought I could force the orientation and prevent it from switching to portrait modes by doing something like this:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
UIDeviceOrientation orientation = UIInterfaceOrientationLandscapeLeft;
[UIApplication sharedApplication].statusBarOrientation = orientation;
}
else {
}
But this does not prevent the app from being rotated to portrait mode. Is it possible to force the orientation? Is there something else I need to do in order for the startup orientation to be correct (landscape mode only)? Thanks!
I just created a sample project, set my Supported interface orientations (iPad) to Landscape Left and Landscape Right (in info.plist).
I then used:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
and it works fine. It's forced to landscape not matter how i rotate it.
Do you have any other view controllers visible that might be returning YES to all orientations? This could confuse it.
I have an iPad app that works correctly except for an odd issue during launch. I've read several questions & answers regarding orientation, but this still has me stumped.
The root view controller is a UITabBarController with 3 tabs. Two of the tabs are have custom view controllers (one based off of UIViewController, the other off of UITableViewController) and both suffer from this launch orientation problem. The third tab is a custom UITableViewController that's embedded in a UINavigationController.
OK, here's the problem. If I start the app in Portrait orientation, everything works great. If I start it in Landscape orientation, the 3rd tab works perfectly. However, the first 2 tabs come up in Portrait orientation, even though:
The status bar orientation correctly shows as landscape (spread across the screen).
The Tab Bar view correctly shows as landscape with the tabs centered.
All views return YES for shouldAutorotateToInterfaceOrientation for all orientations.
If I call [self interfaceOrientation] or [[UIApplication sharedApplication] statusBarOrientation] in the view controller's viewWillAppear, then the 3rd tab's view controller reports 3 (landscape) but the first two view controllers report 1 (portrait) even though the status bar is clearly landscape!
If I rotate the iPad to portrait and back to landscape, then all 3 tabs' views rotate correctly (and the methods above return 3, as expected).
Also, if I tap on any other tab and then back on tab #1 or #2, then they will now rotate correctly, even without rotating the iPad itself!
What am I missing?
You have to add the supportedDeviceOrientations to your "myApp.plist" .
Click on this list, add the key "Supported interface orientations" and add the supported interface orientations. This solved the problem for me.
For further informationen follow this link and go to the section "The Application Bundle": http://developer.apple.com/iphone/library/documentation/General/Conceptual/iPadProgrammingGuide/CoreApplication/CoreApplication.html
I finally found my answer: I just forgot this in my LoadingController.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
I have found that the device orientation starts out with nothing. And should return YES for Unknown. This will allow it to orient the device with the correct launch orientation.
Here is the code I used to propigate this message up to the legacy messages.
- (BOOL)shouldAutorotate{
UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
if (orientation == UIDeviceOrientationUnknown) return YES;
BOOL result = [self shouldAutorotateToInterfaceOrientation:orientation];
return result;
}
notice I return YES if orientation == UIDeviceOrientationUnknown. This corrected my loading problem.
The solution is to add a key
UISupportedInterfaceOrientation
to you Info.plist with an array of strings specifying the suppored interface orientations at launch time, these are
UIInterfaceOrientationPortrait
UIInterfaceOrientationPortraitUpsideDown
UIInterfaceOrientationLandscapeLeft
UIInterfaceOrientationLandscapeRight
However, there is the follwing issue which may lead to confusion: At least with SDK 3.2 and iPad Simulator from XCode 3.2.4 I found that (at least some) Info.plist settings appeared to be cached and/or are not updated when installing the app. That is, adding the key above and installing and launching the app in the simulator had no effect. However, deleting the app from the simulator fixed the problem an the newly installed app behaved as specified.
In your app delegate's applicationDidFinishLaunchingWithOptions: method, after you add your view controller's view to the window, add the following:
[myViewController viewDidLoad];
If necessary, this will trigger a call to the shouldAutorotateToInterfaceOrientation: method.
just try this
- (BOOL)shouldAutorotateToInterfaceOrientation: UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown);<br>
}
I'm developing an iPhone application that has several nibs, and should be landscape only.
The application is set to start in landscape mode via its Info.plist file.
I have two view controllers:
FirstViewController and SecondViewController.
For each of these I have a nib file, where the view is in landscape. Both view controllers are added to my MainView nib as outlets, and their views are lazily initialized.
When the application loads, the first view displays in landscape, as expected. However, when I switch to the second view, the device (or simulator) remains in landscape, but the view is rotated, as if the device were in portrait mode, braking my interface.
In both UIViewController classes I have the following code:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return interfaceOrientation == UIInterfaceOrientationLandscapeRight;
}
and to switch views, in my app delegate I'm doing:
[viewController.view removeFromSuperview];
[window addSubview:secondViewController.view];
where viewController and secondViewController are the two outlets where the view controllers are connected.
This is how the second view looks in IB:
alt text http://img27.imageshack.us/img27/4898/picture1ni.png
and this is how it looks in the simulator:
alt text http://img402.imageshack.us/img402/4866/picture2wt.png
Why is that the second view is displaying in landscape but with the interface rotated?
I wouldn't like to deal with transform properties, since that seems overkill.
I starred this question hoping someone would give you an insightful response and I'd learn something.. sadly I'm afraid that you might need to use transforms to get this to work properly. Here's the code I've been using lately to solve the problem:
- (void)forceLandscapeForView:(UIView *)theView {
theView.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
theView.bounds = CGRectMake(0, 0, 480, 320);
theView.center = CGPointMake(160, 240);
[theView setNeedsLayout];
[theView setNeedsDisplay];
}
Then when you're adding your new view, check the current orientation and if necessary force the rotation:
if (!UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)) {
[self forceLandscapeForView:_activeViewController.view];
}
Then of course you'll want to respond appropriately to shouldAutorotateToInterfaceOrientation in each of your view controllers:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
I would love to hear about alternate solutions if this isn't all necessary. There is also one caveat I've noticed with this setup: if you have a transition between views, and you rotate the phone during that transition, it's possible for the views orientations to get flipped or "stuck" on the wrong landscape orientation, such that you need to turn the phone over (landscape-right vs landscape-left) as you navigate between views.
it is just a suggestion but you can try to return NO in the shouldAotorotate method for the second view. Or try to make it in the portrait view in the IB. It seems that your view was loaded correctly(in the landscape mode) but then received shouldAutorotate message and had been rotated by 90 degrees.