Hi i have following scenario :
1) ViewControllerA >> pushes ViewControllerB
2) In ViewControllerB I have 3 buttons as AUTO , LANDSCAPE, PORTRAIT.
3) If user clicks on AUTO button the particular view controller should always be visible in PORTRAIT + LANDSCAPE mode depending on the orientation mode
4) If user clicks on LANDSCAPE button the particular view controller should always be visible in LANDSCAPE mode irrespective of the orientation of device.
5) If user clicks on PORTRAIT the particular view controller should always be visible in PORTRAIT mode irrespective of the orientation of device.
Forcing orientation on a particular view controller
I am working on iOS 7.0 both for iPad and iPhone.
Tried many links on Stack Overflow but all give suggestions for 6.0 and those doesn't work out in 7.0
Can anybody suggest me how to achieve this..
Try this..
Create property orientationStyle as nsinteger in Appdelegate and change the value of orientationStyle from the ViewControllerB according to the button click.
Add the below method in Appdelegate.
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
switch (orientationStyle) {
case 0:
return UIInterfaceOrientationMaskPortrait;
break;
case 1:
return UIInterfaceOrientationMaskLandscape;
break;
case 2:
return UIInterfaceOrientationMaskAll;
break;
default:
return UIInterfaceOrientationMaskAll;
break;
}
}
Hope it will help you
This is working but not perfectly :
force landscape ios 7
Still need to check if its a private API as that may cause issue..
hey RAJA i have already tried this link before : Force Landscape Orientation on iOS 6 in Objective-C
but this was not perfect either....
[will definitely try to change some code may be it can work]
Related
A Question was asked earlier. And i was facing the same issue where the Movie player was not rotating as the project properties didn't allowed to rotate. This issue was only faced in iOS7 over iPhone so i am trying another work around where i enable all the orientation in project Properties but the issue is that when ever i disable the rotation in other view controllers through functions like this
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return FALSE;
}
// Tell the system what we support
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationPortrait;
}
- (BOOL) shouldAutorotate {
return NO;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationPortrait;
}
The view controllers still rotate which i suppose that this is because its allowed in project properties.
So the Question is..
How can i disable Rotation in a specific Media Player View Controller
when Project settings allows all rotation?
OR
How can i Override rotation in a specific Media Player view controller
over project properties (Disabling rotation) which doesn't work in
iOS7
you can implement below method in you AppDelegate class it's working for me:
- (NSUInteger) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if ([[window.rootViewController presentedViewController] isKindOfClass:[MPMoviePlayerViewController class]])
{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
else
{
return UIInterfaceOrientationMaskPortrait;
}
}
When we are talking about orientation, they are 2 things that come into the picture:
Device Orientation Interface Orientation As its clear by the name only, Device orientation tells, in which orientation device is, and Interface orientation says in which orientation your app is presenting its interface.
Here what you are doing is, your app is supporting all orientation. You must have check marked all orientations in project.
Now when you are changing orientation of device from portrait to landscape, when you have set interfaceOrientation to be in portrait mode programmatically, this is what happens. As device orientation is changes, orientation of your status bar also changes. But as you have restricted interface of your app to be in portrait orientation, its not changing its orientation..
So this is what you can do:
Uncheck landscape orientation support for your app & check if problem persists.
Let me know what happened when you followed first step :)
I'm developing an iOS6 App with storyboards and i'm encountering an unexpected beahviour.
My app is almost in portrait mode , and i would keep this orientation in all the views except two.For this reason the project supports both landscape and portrait mode, i've subclassed navigation controller with a category (as explained almost everywhere :-)in Appdelegate.m and every view controller implements
- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait; (landscape where needed)
}
and
-(BOOL)shouldAutorotate{
return YES;
}
Everything seems to work well except the fact that in the transition between a landscape view to a portrait one (not vice versa ?) , all the elements of the ui are displayed in landscape(imagine that you're keeping the phone horizontal), if you turn the phone , the rotation event is fired, the ui turns back in portrait and only now is locked to this orientation.Is there a way to fire the rotation BEFORE the view is presented?
Why the shouldAutorotate is not called at the ViewWillAppear stage?
Thank you!
Remove both the above function and try this it should work
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
Return YES for supported orientations
}
if still it's not working then try this change the appDelegate
[window addSubview:nav.view];
to this code
window.rootViewController=nav;
I found this online chapter very good for explaining UIViewControllers and rotation.
http://www.apeth.com/iOSBook/ch19.html#Rotation
It s a big page, scroll down to Rotation.
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.
I've had to allow landscape orientation in all my views because one of them needs to handle landscape orientation and my app is a tabbar based iphone app. Now some views are obviously showing ugly when device is in landscape mode. I'm thinking of subclassing UIView and use the subclass to show a "warning" screen when user is not in a portrait mode in some views. Any idea on what could be of best practices about it ?
Thx for helping,
Stephane
There is a willRotateToInterfaceOrientation:toInterfaceOrientation method in the UIViewController class. iOS calls it when an end user rotates phone. You may override it to display and hide a warning message.
- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (! UIDeviceOrientationIsPortrait (toInterfaceOrientation))
[self displayWarningMessage];
else
[self hideWarningMessage];
}
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