I have an app that changes the view when the orientation changes using: [[UIApplication sharedApplication] statusBarOrientation]. On the iPhone, it works but on the iPad, it gives me landscape instead of portrait and portrait instead of landscape.
When I call the getRelevantFrame method outside of the UIDeviceOrientationDidChangeNotification, it will return the correct frame.
It will return the opposite frame (if landscape, will return portrait and vice versa) when responding to the notification.
Both the versions (iPod/iPhone + iPad) uses the same code but this only breaks on the iPad version
Here is the code I used to calculate the relevant frame:
EDIT: Used rob5408's advice. Changed to use UI_USER_INTERFACE_IDIOM().
+ (CGRect)getRelevantFrame {
//Get orientation
UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) { //iPhone/iPod
if(interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
return [self frameiPhoneLandscape];
}
else {
return [self frameiPhonePortrait];
}
} else if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { //iPad
if(interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
NSLog(#"iPad Landscape");
return [self frameiPadLandscape];
}
else {
NSLog(#"iPad Portrait");
return [self frameiPadPortrait];
}
}
return CGRectMake(0, 0, 0, 0);
}
Here is the notification I used:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(didRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];
Here is the code I used to read the orientation change:
- (void) didRotate:(NSNotification *)notification
{
NSLog(#"Parent: %#", self.parentController);
if(self.parentController) {
[self setFrame:[TASFrames getRelevantFrame]];
}
}
I transferred the code to a new project. And now it works as it's supposed to. No idea as to why this happens. Code didn't change.
Related
+(CGFloat)maxTextWidth
{
if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPhone)
{
if(UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
{
return 220.0f;
}
else
{
return 400.0f;
}
}
else
{
if(UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
{
return 400.0f;
}
else
{
return 700.0f;
}
}
}
when my device in portrait orientation according to the above method it return the portrait value but the problem is that in portrait orientation sometimes it return the portrait value and sometimes it return landscape value but my device always in portrait orientation.
In your if statement check status bar orientation instead of device orientation..
+(CGFloat)maxTextWidth
{
if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPhone)
{
if([UIApplication sharedApplication].statusBarOrientation==UIInterfaceOrientationPortrait)
{
return 220.0f;
}
else
{
return 400.0f;
}
}
else
{
if([UIApplication sharedApplication].statusBarOrientation==UIInterfaceOrientationPortrait)
{
return 400.0f;
}
else
{
return 700.0f;
}
}
}
Copy this code in your viewdidload.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(didrotatedevice:) name:UIDeviceOrientationDidChangeNotification object:nil];
and copy this method in your class
- (void)didrotatedevice:(NSNotification *)notification
{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIDeviceOrientationLandscapeLeft)
{
NSLog(#"Landscape Left!");
}
else if (orientation == UIDeviceOrientationLandscapeRight)
{
NSLog(#"Landscape Right!");
}
else
{
NSLog(#"Potrait!");
}
}
Let me know if you have any doubts?
Why you didn’t use
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
Or you can use this
-(void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
Or this
-(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
These methods detect any orientation changes.
Set your UI changes in these methods.
I have seen multiple copies of the same question.but it has not supported my cause.
i have tried subclassing the navigationController adding category for the navigation controller and when i tried all these the methods shouldAutoRotate and supportedInterfaceOrientations gets invoked for Both the navigationcontroller and view controller but the device stays in portrait view only.
#import "UINavigationController+Rotation.h"
#implementation UINavigationController (Rotation)
- (BOOL)shouldAutorotate {
BOOL result = self.topViewController.shouldAutorotate;
return result;
}
- (NSUInteger)supportedInterfaceOrientations {
NSUInteger result = self.topViewController.supportedInterfaceOrientations;
return result;
}
#end
Is some thing that i do wrong?
Call this method form viewdidload:
-(void)rotateCameraView {
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver: self selector: #selector(deviceOrientationDidChange:) name:
UIDeviceOrientationDidChangeNotification object: nil];
}
When you rotate device this method will call automatically
-(void)deviceOrientationDidChange:(NSNotification *)notification {
//Obtaining the current device orientation
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
//Ignoring specific orientations
if (orientation == UIDeviceOrientationFaceUp
|| orientation == UIDeviceOrientationFaceDown || orientation ==
UIDeviceOrientationUnknown) { return; }
if ((UIDeviceOrientationIsPortrait(orientation)
||UIDeviceOrientationIsPortrait(orientation)) ||
(UIDeviceOrientationIsLandscape(orientation) ||
UIDeviceOrientationIsLandscape(orientation))) {
//still saving the current orientation
currentOrientation = orientation; } [self
performSelector:#selector(orientationChangedMethod) withObject:nil
afterDelay:0];
}
-(void)orientationChangedMethod
{
switch (currentOrientation) {
case UIDeviceOrientationPortrait:
if (isOrientationEffect==YES)
{
appDelegate.imagePickerController.cameraViewTransform = CGAffineTransformMakeRotation(M_PI);
// if (!isPurchased)
// {
//
// [self buttonFrameWithIad];
// }
// else
// {
// [self buttonFrameWithoutIad];
// }
[self MakeTransformRotation];
}
[UIView beginAnimations:#"rotateView" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:0.35f];
camDefaultImg.transform = CGAffineTransformMakeRotation(0);
[UIView commitAnimations];
break;
case UIDeviceOrientationLandscapeLeft:
if (isOrientationEffect==YES)
{
}
}
I'm working on an iPad app that start with a splash screen and then moves to the login screen. My app should support all the orientation, and iOS 4.3 and later. To do that, I added the four orientations in the plist, and the following code in the app delegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self.window makeKeyAndVisible];
// Override point for customization after application launch
SplashViewController *aController = [[SplashViewController alloc] initWithNibName:#"SplashView" bundle:nil];
self.mainViewController = aController;
[aController release];
mainViewController.view.frame = [UIScreen mainScreen].bounds;// no effect
[window setFrame:[[UIScreen mainScreen] bounds]]; //no effect
//[window addSubview:[mainViewController view]];
[window setRootViewController:mainViewController];
return YES;
}
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
return UIInterfaceOrientationMaskAll;
}
In the splash screen
- (void) loadView {
[super loadView];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(orientationChanged) name:#"UIDeviceOrientationDidChangeNotification" object:nil];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
- (BOOL)shouldAutorotate{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
- (void) orientationChanged
{
UIInterfaceOrientation interfaceOrientation =[[UIApplication sharedApplication] statusBarOrientation];
if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
NSLog(#"Portrait");
else
NSLog(#"Landscape");
}
Here I get the correct orientation but when rotating, I get an inverted result, I get landscape for portrait and portrait for landscape. I tried to convert the code like this:
- (void) orientationChanged
{
UIInterfaceOrientation interfaceOrientation =[[UIApplication sharedApplication] statusBarOrientation];
if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
NSLog(#"Landscape");
else
NSLog(#"Portrait");
}
I get the first result wrong, after that the result is correct. can you help?
note that I have put some uielement to test and the test come with the same result.
Thank you
I managed to solve this matter after two days of searching and testing, none of the answers in the forum was complete, and here is how I solved:
//this for the orientation
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
[self orientationChanged];
}
// here is the tricky thing, when startup you should have a special function for the orientation other than the orientationchanged method, and must be called here in viewDidAppear, otherwise it won't work
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self StartUpOrientation];
}
#pragma mark -
#pragma mark Orientation Methods
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
- (BOOL)shouldAutorotate {
return YES;
}
-(NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}
- (void) orientationChanged {
UIInterfaceOrientation interfaceOrientation =[[UIApplication sharedApplication] statusBarOrientation];
if (interfaceOrientation != UIInterfaceOrientationLandscapeLeft && interfaceOrientation != UIInterfaceOrientationLandscapeRight) {
[self.view setBounds:CGRectMake(0, 0, 1024, 748)];
// your code here
}
else {
[self.view setBounds:CGRectMake(0, 0,768,1004)];
//your code here
}
}
- (void) StartUpOrientation {
UIInterfaceOrientation interfaceOrientation =[[UIApplication sharedApplication] statusBarOrientation];
if (interfaceOrientation != UIInterfaceOrientationLandscapeLeft && interfaceOrientation != UIInterfaceOrientationLandscapeRight) {
[self.view setBounds:CGRectMake(0, 0,768,1004)];
// your code here
}
else {
[self.view setBounds:CGRectMake(0, 0, 1024, 748)];
// your code here
}
}
hope it will help someone someday
Im currently working with a IPhone-app that has a "timetable".
in portrait i want it to have a regular table-view with some customization! When i have the IPhone in landscape i want it to change to a more "timetable"-view, with tables and rows.
Is it possible?
Try This
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[self adjustViewsForOrientation:toInterfaceOrientation];
}
-(void) adjustViewsForOrientation:(UIInterfaceOrientation)orientation
{
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
{
//write code for portrait mode
}
else if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
//write code for landscape mode
}
}
See Apples documentation Creating an Alternate Landscape Interface
Make sure to read the documentation, but here is the code from the example implementation:
#implementation PortraitViewController
- (void)awakeFromNib
{
isShowingLandscapeView = NO;
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
}
- (void)orientationChanged:(NSNotification *)notification
{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(deviceOrientation) &&
!isShowingLandscapeView)
{
[self performSegueWithIdentifier:#"DisplayAlternateView" sender:self];
isShowingLandscapeView = YES;
}
else if (UIDeviceOrientationIsPortrait(deviceOrientation) &&
isShowingLandscapeView)
{
[self dismissViewControllerAnimated:YES completion:nil];
isShowingLandscapeView = NO;
}
}
Answer is Yes it's possible through the following method:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if(UIInterfaceOrientationIsLandscape(toInterfaceOrientation)){
\\ your timetable customisation goes here
}
}
You also need this:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
It works perfect fine when starting in portrait and also works when you rotate from portrait to landscape and back.
It does not work when starting in landscape. But then it works when you rotate from landscape to portrait and back.
In landscape starting mode, the screen does not respond with any touch where screen coordinateX greater than 768.
What happens in code is, I use status bar orientation to determine original orientation and rotate each view manually. The views display correctly but does not receive touch properly.
Then my root view controller will get called when ipad start rotating with:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
which will rotate every subviews.
Root controller:
- (void)loadView {
self.view = [[UIView alloc]init ];
//initialize child views
[self willRotateToInterfaceOrientation:0 duration:0];
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if ([model isLandscape]) {
self.view.frame = CGRectMake(0, 0, 1024, 768-80);
}
else {
self.view.frame = CGRectMake(0, 0, 768, 1024-80);
}
//rotate child views
}
My code [model isLandscape] works so I don't need to provide details as to how it works but here are the code anyway:
- (bool)isLandscape {
if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
return true;
else
return false;
}
-(id) init
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
- (void)orientationChanged:(NSNotification *)notification
{
UIInterfaceOrientation curOrientation = [[UIDevice currentDevice] orientation];
if (curOrientation == UIDeviceOrientationPortrait ||
curOrientation == UIDeviceOrientationPortraitUpsideDown ||
curOrientation == UIDeviceOrientationLandscapeLeft ||
curOrientation == UIDeviceOrientationLandscapeRight)
{
orientation = curOrientation;
((AppDelegate*)([UIApplication sharedApplication].delegate)).savedOrientationForRestart = orientation;
NSLog(#"changed");
}
}
-(void)validateOrientation { //first time when initializing orientation
UIInterfaceOrientation curOrientation = [[UIDevice currentDevice] orientation];
if (curOrientation != UIDeviceOrientationPortrait &&
curOrientation != UIDeviceOrientationPortraitUpsideDown &&
curOrientation != UIDeviceOrientationLandscapeLeft &&
curOrientation != UIDeviceOrientationLandscapeRight)
{
orientation = [[UIApplication sharedApplication] statusBarOrientation];
}
}
OK. so it turns out among the tens of backgrounds and layers some were not rotated to landscape/portrait correctly...it was extremely hard to debug this issue...
in the end, I hooked up a touch debugger to see which views are receiving touch correctly and which are not...
extremely helpful!
Just use the UIWindow subclass below instead of UIWindow in your code, should work.
#import <UIKit/UIKit.h>
#interface MyUIWindow : UIWindow {
}
-(void)rotate;
#end
and
#import "MyUIWindow.h"
#implementation MyUIWindow
-(id) init{
self = [super init];
if(self)
{
}
return self;
}
- (void)sendEvent:(UIEvent *)event {
[super sendEvent:event];
NSSet *touches = [event allTouches];
if (touches.count != 1)
return;
UITouch *touch = touches.anyObject;
UIView * view = touch.view;
NSLog(NSStringFromClass([touch.view class])); //this prints out touch view
}
#end