'willRotateToInterfaceOrientation' not working for my DetailViewController - objective-c

I'm developing this application on the iPad. My MainWindow contains a Split View Controller which loads the RootViewController and DetailViewController.
I have this image that is placed at the bottom-right of the DetailViewController.
My application allows different orientations, therefore i want the image to appear at different positions for different orientations.
This is my code in DetailViewController:
- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
myImage.frame = CGRectMake(183.0f, 257.0f, 548.0f, 447.0f);
}
else if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
myImage.frame = CGRectMake(244.0f, 518.0f, 548.0f, 447.0f);
}
}
When i launch my application and my iPad is at portrait orientation, the image will be correctly placed at (X:244, Y:518) which is at the bottom right.
But when i launch my application and my iPad is at landscape orientation, the image will not be shown and i suspect that it is still at the position for portrait orientation. Only when i rotate my iPad to portrait orientation and then back to landscape orientation, then the image will appear at (X:183, Y:257) which is correct.
What i tried to do is to place these codes in my DetailViewController's 'viewWillAppear' method:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if(self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft || self.interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
myImage.frame = CGRectMake(183.0f, 257.0f, 548.0f, 447.0f);
}
else if(self.interfaceOrientation == UIInterfaceOrientationPortrait || self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
myImage.frame = CGRectMake(244.0f, 518.0f, 548.0f, 447.0f);
}
}
But when i launch my application, it still has the same problem.
What can i do so that when i first launch my application in landscape orientation, it will be correctly placed at (X:244, Y:518) ?

You might want to try putting your resizing logic in didRotateFromInterfaceOrientation instead of willRotateToInterfaceOrientation.
My custom layout code only worked when I called it after the rotation had completed it, rather than before.
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
NSLog(#"didRotateFromInterfaceOrientation");
float width;
float height;
if ((fromInterfaceOrientation == UIInterfaceOrientationLandscapeRight) ||
(fromInterfaceOrientation == UIInterfaceOrientationLandscapeLeft))
{
width = [[UIScreen mainScreen] applicationFrame].size.width;
height = [[UIScreen mainScreen] applicationFrame].size.height;
NSLog(#"Changing to portrait: %f x %f", width, height);
}
else
{
width = [[UIScreen mainScreen] applicationFrame].size.height;
height = [[UIScreen mainScreen] applicationFrame].size.width;
NSLog(#"Changing to landscape: %f x %f", width, height);
}
NSLog(#"Changed orientation: %f x %f", width, height);
// Call my custom layout function to setFrame on all my UI controls
[self doLayout:width height:height];
[super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
}
If your app shows the status bar then you probably need to call self.view.frame.size.width instead of getting the size from the applicationFrame.

Make sure that you're overriding
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation; // Override to allow rotation. Default returns YES only for UIInterfaceOrientationPortrait
To return YES for each UIInterfaceOrentation that you want to support, otherwise willRotateToInterfaceOrientation and didRotateToInterfaceOrientation wont get called.

Try calling this early in your applicationDidFinishLauching:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

Since the detailViewController wouldn't sometimes know its orientation, I solved this issue by having a shared variable in the appDelegate that would store the device orientation throughout the app. You can initialize it in the splitViewController with its orientation so when the detailViewController loads it can read it from there.

Related

Launching Image Picker in Portrait Mode on iPad

I am using the following code to launch the image picker in my iPad app (which uses Cocos2D) :
UIImagePickerController * imagePickerController = [[UIImagePickerController alloc] init];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerController.showsCameraControls = YES;
imagePickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
imagePickerController.delegate = self;
imagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceFront;
[self.view addSubview:imagePickerController.view];
[self presentModalViewController:imagePickerController animated:YES];
}
I want it to launch in the portrait mode all the time, but it always launches like this :
The image appears in portrait and the image picker UI is in landscape mode. But When I capture the image, it get's rotated 90 degrees clockwise.
I want the image picker UI and the taken image, both to be in portrait mode. How can I fix this ?
There are three things you need to do:
Tell the viewController that is presenting the imagePicker to only
support portrait orientation.
Tell the imagePicker to not to rotate the live camera feed.
Un-rotate captured images.
The imagePicker receives notifications of device orientation changes directly. To keep the live camera feed from rotating with the device you can stop it from receiving these notifications by telling UIDevice to endGenerating the orientation notifications after the imagePicker is presented via:
while ([currentDevice isGeneratingDeviceOrientationNotifications])
[currentDevice endGeneratingDeviceOrientationNotifications];
The reason why you put it in a loop is because the imagePicker beginsGeneratingDeviceOrientationNotifications, and then ends its when it is dismissed, bringing the notification count on a normal device from 1 to 2 back to 1.
After the imagePicker is dismissed, you can call:
while (![currentDevice isGeneratingDeviceOrientationNotifications])
[currentDevice beginGeneratingDeviceOrientationNotifications];
so that your ViewControllers can continue to receive orientation change notifications.
Unfortunately, even after turning this off, the images will still be saved with the correct imageOrientation of the camera on capture, so before you save the image or do anything with it, you'll have to remove the applied orientation transformation by manually countering it:
-(UIImage *)turnMeAround:(UIImage *)image{
CGAffineTransform transform = CGAffineTransformIdentity;
CGFloat scale = [[UIScreen mainScreen] scale]; //retina
CGSize size = CGSizeMake(image.size.width*scale,image.size.height*scale);
switch (image.imageOrientation) {
case UIImageOrientationUp:
return image;
case UIImageOrientationDown:
size = CGSizeMake(size.height,size.width);
transform = CGAffineTransformRotate(transform, M_PI);
break;
case UIImageOrientationLeft:
transform = CGAffineTransformRotate(transform, -M_PI_2);
break;
case UIImageOrientationRight:
transform = CGAffineTransformRotate(transform, M_PI_2);
break;
}
CGContextRef context = CGBitmapContextCreate(NULL, size.width, size.height, CGImageGetBitsPerComponent(image.CGImage), 0, CGImageGetColorSpace(image.CGImage), CGImageGetBitmapInfo(image.CGImage));
CGContextConcatCTM(context, transform);
CGContextDrawImage(context, CGRectMake(0,0,size.width,size.height), image.CGImage);
CGImageRef ref = CGBitmapContextCreateImage(context);
UIImage *upsideRight = [UIImage imageWithCGImage:ref];
CGContextRelease(context);
CGImageRelease(ref);
return upsideRight;
}
Thanks so much guys for all the help, but what worked for me is as follows :
So I found out that it was Xcode's Cocos2D template which was the culprit. It had generated the following code in the RootViewController :
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
//
// There are 2 ways to support auto-rotation:
// - The OpenGL / cocos2d way
// - Faster, but doesn't rotate the UIKit objects
// - The ViewController way
// - A bit slower, but the UiKit objects are placed in the right place
//
#if GAME_AUTOROTATION==kGameAutorotationNone
//
// EAGLView won't be autorotated.
// Since this method should return YES in at least 1 orientation,
// we return YES only in the Portrait orientation
//
return ( interfaceOrientation == UIInterfaceOrientationPortrait );
#elif GAME_AUTOROTATION==kGameAutorotationCCDirector
//
// EAGLView will be rotated by cocos2d
//
// Sample: Autorotate only in landscape mode
//
if( interfaceOrientation == UIInterfaceOrientationLandscapeLeft ) {
[[CCDirector sharedDirector] setDeviceOrientation: kCCDeviceOrientationLandscapeRight];
} else if( interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
[[CCDirector sharedDirector] setDeviceOrientation: kCCDeviceOrientationLandscapeLeft];
}
// Since this method should return YES in at least 1 orientation,
// we return YES only in the Portrait orientation
return ( interfaceOrientation == UIInterfaceOrientationPortrait );
#elif GAME_AUTOROTATION == kGameAutorotationUIViewController
//
// EAGLView will be rotated by the UIViewController
//
// Sample: Autorotate only in landscpe mode
//
// return YES for the supported orientations
return ( UIInterfaceOrientationIsLandscape( interfaceOrientation ) );
#else
#error Unknown value in GAME_AUTOROTATION
#endif // GAME_AUTOROTATION
// Shold not happen
return NO;
}
In the above code I changed this :
return ( UIInterfaceOrientationIsLandscape( interfaceOrientation ) );
To this :
return ( UIInterfaceOrientationIsPortrait( interfaceOrientation ) );
And that fixed it.

Reset frames when rotating orientation

I have an app that has some buttons and a text view and a label. I want to rotate the device and have them redrawn on the screen to fit. I want to do it programatically. Currently I have these methods:
-(void)willRotateToInterfaceOrientation:
(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration {
[super willRotateToInterfaceOrientation:toInterfaceOrientation
duration:duration];
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight ||
toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
[self reOrderSideways];
} else {
[self reOrderUpDown];
}
}
-(void)reOrderSideways{
self.viewLabel.frame=CGRectMake(175.0,140.0,130.0,20.0);
self.buttonOne.frame=CGRectMake(20.0,20.0,440.0,100.0);
self.buttonTwo.frame=CGRectMake(20.0,180.0,440.0,100.0);
self.textContent.frame=CGRectMake(20.0, 290.0, 440.0, 100.0);
self.theScroller.contentSize = CGSizeMake(480.0, 350.0);
}
-(void)reOrderUpDown{
self.viewLabel.frame=CGRectMake(95.0,15.0,130.0,20.0);
self.buttonOne.frame=CGRectMake(20.0,50.0,280.0,190.0);
self.buttonTwo.frame=CGRectMake(20.0,250.0,280.0,190.0);
self.textContent.frame=CGRectMake(20.0, 450.0, 280.0, 190.0);
self.theScroller.contentSize = CGSizeMake(320.0, 460.0);
}
It doesn't quite work because when i rotate sideways, the buttons and labels and textview get cutoff the right side. I checked it and it looks like its using the coordinates I gave it but it is still using the portrait frame or bounds. How can i fix this?
Try using willAnimateRotationToInterfaceOrientation: instead.

Objective C: Orientation Change With UIScrollview

I am trying to make an app with UIScrollView (pagingEnabled) that should adapt on the device's interface orientation.
to do this, i made two UIScrollViews loaded with UIImageviews and UIButtons inside and animations (CA). one for portrait and the other one for landscape (you'll see it in my code).
now my problem is whenever i rotate the device, yes! it respond to the orientation change but it goes back to the first page.
ok picture this... you are already on the 10th page of the scrollView in portrait then when you switched to landscape it is on the first page again.
i hope explained it clearly.
and... it crashes on iPad when i rotate it.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown || interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){
//self.view = portraitView;
[self changeTheViewToPortrait:YES andDuration:duration];
}
else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeRight || toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft){
//self.view = landscapeView;
[self changeTheViewToPortrait:NO andDuration:duration];
}
}
- (void) changeTheViewToPortrait:(BOOL)portrait andDuration:(NSTimeInterval)duration{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:duration];
if(portrait){
//change the view and subview frames for the portrait view
[self forPortrait];
[self removeMainSVLContents];
}
else{
//change the view and subview frames for the landscape view
[self forLandscape];
[self removeMainSVPContents];
}
[UIView commitAnimations];
}
What's inside "forPortrait" is the same as in "forLandscape" they just have different frames and it has different images too that will fit the screen on portrait and landscape.
at the time of the animation use **scrollRectToVisible:animated:** method to show or stay on the desire place of scroll view
may this will be useful to you.

Manual Rotation Method Breaks with Side Switch

Currently I'm workign on a drawing app for the iPad. I need to reposition and rotate the toolbar in the app when it is put into a different orientation while keeping the the drawing area in the same place.
I found a method here for doing this. It uses the NSNotificationCenter to monitor for rotation changes. This calls a custom didRotate: method that will rotate and reposition my toolbar based on the UIDeviceOrientation.
This part works fine. However, whenever the side switch on the iPad is engaged to lock the orientation, the toolbar repositions to the location is was at launch.
For example: If I start the application in landscape left and rotate it to portrait, the toolbar will reposition to the bottom of the screen. However as soon as I engage the slide switch, it moves to the side of the screen for the landscape left orientation.
The methods I'm using for this are all below.
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(didRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
- (void)didRotate:(NSNotification *)notification {
UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
UIInterfaceOrientation interfaceOrientation;
bool orientationFound = YES;
if (deviceOrientation == UIDeviceOrientationPortrait) {
interfaceOrientation = UIInterfaceOrientationPortrait;
} else if (deviceOrientation == UIDeviceOrientationPortraitUpsideDown) {
interfaceOrientation = UIInterfaceOrientationPortraitUpsideDown;
} else if (deviceOrientation == UIDeviceOrientationLandscapeLeft) {
interfaceOrientation = UIInterfaceOrientationLandscapeRight;
} else if (deviceOrientation == UIDeviceOrientationLandscapeRight) {
interfaceOrientation = UIInterfaceOrientationLandscapeLeft;
} else {
orientationFound = NO;
}
if (orientationFound) {
[self.toolbar changeToOrientation:interfaceOrientation withDuration:.25];
[self.tutorialOverlay changeToOrientation:interfaceOrientation];
}
}
- (void)changeToOrientation:(UIInterfaceOrientation)orientation withDuration:(float)duration {
float angle;
CGPoint origin;
if (orientation == UIInterfaceOrientationPortrait) {
angle = portraitAngle;
origin = self.portraitOrigin;
} else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
angle = portraitUpsideDownAngle;
origin = self.portraitUpsideDownOrigin;
} else if (orientation == UIInterfaceOrientationLandscapeLeft) {
angle = landscapeLeftAngle;
origin = self.landscapeLeftOrigin;
} else if (orientation == UIInterfaceOrientationLandscapeRight) {
angle = landscapeRightAngle;
origin = self.landscapeRightOrigin;
}
[UIView animateWithDuration:duration animations:^{
self.transform = CGAffineTransformMakeRotation(angle);
CGRect rect = self.frame;
rect.origin = origin;
self.frame = rect;
}];
}
I'd strongly recommend against using this notification-based approach. Note that the name of the notification is UIDeviceOrientationDidChangeNotification; the device orientation is not the same as the interface orientation, and it leads to lots of little issues like this. (The device orientation, for example, includes UIDeviceOrientationFaceDown, which is never associated with an interface orientation.)
I'd suggest letting your view controller automatically rotate itself; this will place the toolbar, etc, for you. You can then override - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration to put the drawing area back to the orientation you want to maintain. (You'd use similar code to your changeToOrientation method above, but for the drawing area instead, and you don't need to create your own animation block. Also, the angles would all be negated, because you're undoing the change the view controller made.)
I just answered a similar question here.
Basically just allow the interface to rotate, but rotate the view you don't want to rotate 'back' in willRotateToInterfaceOrientation:duration:.

Changing UIView on orientation change

Hey all. I have a fairly simple question. I am developing a "rich" iPad app, and I have two background images specifically designed for landscape and portrait. I'd like this ImageView to automatically change depending on the devices orientation. (like pretty much all of Apples iPad apps).
Can anyone point me in the right direction? I'm assuming it would be something I do on viewDidLoad..
The best thing you can do is to change the frames of your subview frames according to your interface orientations. You can do it like:
#pragma mark -
#pragma mark InterfaceOrientationMethods
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (UIInterfaceOrientationIsPortrait(interfaceOrientation) || UIInterfaceOrientationIsLandscape(interfaceOrientation));
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
if(UIInterfaceOrientationIsPortrait(toInterfaceOrientation)){
//self.view = portraitView;
[self changeTheViewToPortrait:YES andDuration:duration];
}
else if(UIInterfaceOrientationIsLandscape(toInterfaceOrientation)){
//self.view = landscapeView;
[self changeTheViewToPortrait:NO andDuration:duration];
}
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------
- (void) changeTheViewToPortrait:(BOOL)portrait andDuration:(NSTimeInterval)duration{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:duration];
if(portrait){
//change the view and subview frames for the portrait view
}
else{
//change the view and subview frames for the landscape view
}
[UIView commitAnimations];
}
Hope this helps.
I actually figured out a very simple alternative way around this. Since I am just changing the background image, adding this..
`
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation duration:(NSTimeInterval)duration {
if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation ==
UIInterfaceOrientationPortraitUpsideDown) {
[brownBackground setImage:[UIImage imageNamed:#"Portrait_Background.png"]];
} else {
[brownBackground setImage:[UIImage imageNamed:#"Landscape_Background.png"]];
}
}
`
Changes the background of a declared UIImageView based on orientation. Only downside is, the current background image is not visible in Interface builder as it is handled with code.
One small addition to Madhup's approach, which is great. I found I needed to add this to viewDidLoad to set initial background image for portrait or landscape:
// set background image
if (self.interfaceOrientation == UIInterfaceOrientationPortrait || self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"portraitBG.png"]];
} else {
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"landscapeBG.png"]];
}
thanks again Madhup
You can encapsulate this entirely in your UIView by watching whether bounds.width > bounds.height
This may be desirable if you're writing a small, self aware control.
class MyView: UIView {
override func layoutSubviews() {
super.layoutSubviews()
if bounds.height > bounds.width {
println("PORTRAIT. some bounds-impacting event happened")
} else {
println("LANDSCAPE")
}
}
}