Can anyone give an example of how to rotate the view of a monotouch application from portrait to landscape and vice versa?
If you set the geometry of your ui elements in Interface Builder, then make sure you set the Autosizing attributes in the Size Inspector (Cmd+3). You can then see how the view will look after rotating by clicking the little "Rotate" button in the upper left of the title bar of your view in Interface Builder.
Once you have all that set up, just override the following method in your ViewController:
public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation)
{
return true;
}
Now in the simulator, you can rotate all you want and your UI will auto-adapt.
Just so everyone knows, it is now on the views Transform object:
View.Transform.Rotate(3.14159f * 0.5f);
For all you dotnet guys like me that don't want to use anything that is CGAffineTransform, thinking it will break other things, just try it. Worked great for my instance. Rotated the NavigationController.View and everything inside it is perfect.
This is what you can try out.
If you want to change the orientation of the view while touching on the view , then follow the steps
Implement the touchesBegan method inside the view.
In the touchesBegan method check the current device orientation,
[[UIDevice currentDevice] orientation];
If you want to change the orientation then use the CGAffineTransformation method on the view as viewRef.transform = CGAffineTransformMakeRotation(angle);
Related
how do I implement a button that will flip the screen 180deg for my game (if the user wishes to play it with the iPhone upside-down), and have it affect all of my 5 different views?
You should apply a transform to the parent view (your UIWindow). The rotation can be made using CGAffineTransformMakeRotation().
It might be better to actually allow the device orientation to cause the rotation though. In App settings set that the app supports autorotation and then in the UIViewController return tru to -(BOOL)shouldAutorotateToInterfaceOrientation when the rotation passed in is UIInterfaceOrientationPortraitUpsideDown
Look at shouldAutorotateToInterfaceOrientation. And maybe [myView setNeedsDisplay]; could help.
It sounds like you just want to use the build in automatic orientation rotation. You may need to tell your app in the build settings that it supports the upsidedown orientation and as dasdom mentioned you need to implement shouldAutorotateToInterfaceOrientation in all of your view controllers.
There is no way to force the phone into an orientation on-demand.
If you are looking to just rotate a view you can apply a CGAffineTransform.
I'm starting to upgrading a iPhone App to Universal App, and I have a question: when the user rotates the iPad, how can I animate a couple of textfields?
In portrait, they are at the middle of the view, but in landscape, I want them in the right side of the view.
How can I do that animated?
Thanks,
Rui
When the interface is rotating, your view controller's willAnimateRotationToInterfaceOrientation:duration: method will be called. It is called from inside the animation block for the rotation, so changes to any animatable properties should automatically be animated. The implementation should probably look something like this:
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration {
if (UIInterfaceOrientationIsLandscape(interfaceOrientation)) {
// Move views to the correct positions for landscape orientation
} else {
// Move views to the correct positions for portrait orientation
}
}
I haven't been able to figure this out. Could someone please help me? I am trying to stop one UIView in my app from rotating when the device is turned.
I am working on a drawing app. Right now, when the device is turned, all UI elements turn with it. What I am trying to do is have all the buttons, menus, etc. turn, but have my canvas UIView be static and ignore the rotation of the phone.
If the canvas view is a subview of a view that rotates, then it will rotate as well. It's inescapable.
If you have one element of a view that you do not want to appear to rotate, you have use a rotation transform to programmatically rotate that one view back to the orientation and frame you want it have.
You could use the following to prevent the view itself from rotating:
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation {
return false;
}
Then manually rotate the individual elements you want rotated.
OP here. Rotating the view using CGAffineTransformMakeRotate() worked like a charm. Thanks, TechZen!
I have been spending many frustrating hours trying to get rotations working on the iPhone version of Tunepal.
Firstly, I have a tab bar controller, with a navigation controller controlling each of the views.
I actually only want one of my views to be able to rotate and that is the TuneDisplay.
I have a subclassed the UITabBarController and overridden the shouldAutorotateToInterfaceOrientation:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
if (self.selectedViewController != nil)
{
return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
else
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
}
In each of the view controllers for each of the tabs I have overridden the method and returned YES for each orientation I want to support. All well and good and everything works as it should. If I try and do a rotation on a tab that doesn’t support the rotation, nothing happens.
The problem occurs if I move from a tab thats rotated to a tab that isnt supposed to support that rotation. The new tab is displayed rotated too! Screenshots for all this are included here:
http://tunepal.wordpress.com/2010/04/20/rotation-woes/
Is there any way I can make it rotate back to portrait on tapping the tab?
I have tried the unsupported setOrientation trick, but firstly it doesnt work correctly and secondly I received a warning from Apple for including it in my last build.
If (as I suspect) there is no way to limit this behavior:
How do I make the microphone image scale when I rotate the device?
How do I make the buttons and the progress bar expand to fit the witdh of the toolbar?
Also, one of the tabs that rotates ok has a table, with a search bar. The first time I rotate to the right or to the left, I get a black bar to the right of the search bar. If I subsequently rotate back and rotate again, the bar disappears! I have enabled the struts and springs things on the search bar in the interface builder and it looks like it should behave correctly.
Any ideas about how to fix this?
Ideas, feedback much appreciated
Bryan
This isn't a full answer. Rotation is seriously inconsistent. You have done the right things. Several aspects don't work in the simulator, so you need to confirm all your testing on a device. Table headers and search bars don't resize to full width in older OS versions, so stick with 3.1.3 or higher.
Commonest problems:
implement the shouldAutorotateToInterfaceOrientation: to return YES;
if you use navigation controllers the root view controller must support the orientation;
if you have a toolbar the view controller for all items must support it;
and same for a tab bar controller.
You may need to turn on orientation notifications to get more useful information out of the device:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification object:nil];
Remember to turn it off and remove yourself from the notifications when you are done; this is supposed to have a high overhead.
Set the view contentMode property for your image to resize; together with autoresizingMask, which you are setting in IB, you should be OK.
Remember also that you can use two different nibs for portrait and landscape modes. There is an example project that sort of does this ("WhichWayIsUp"); see the View Controller Programming Guide also ("Creating an Alternate Landscape Interface").
If the rotation methods are being called, then the UI should be rotated consistently. You will find that they aren't always called when they should be.
It isn't hard to call a rotational transform on your views to force a rotation. It shouldn't be needed, but sometimes that's the only way they will rotate.
view.transform = CGAffineTransformMakeRotation(M_PI * n);
If you get it figured out, let us know.
It sounds like you are handling the rotation correctly while the tab is displayed. However, as you know, there's no quick way to switch rotations. What you will have to do is rotate the view yourself using CGAffineTransform. See this question: Is there a documented way to set the iPhone orientation?
To scale the image, you should be able to click the arrows inside the UIImageView housing the image in Interface Builder. There's a little arrow in the upper right hand corner you can click to see how the view behaves when it's rotated to make sure it scales correctly. But you'd probably be better off not scaling the image and hadling the rotation as in the answer to the linked question.
I have a TabBar bases application, which supports Landscape orientation only for one special view (the rootview of a UINaviagtionController). Now i want to force portrait orientation for all other views for this navigationcontroller.
I have tried to use
[[UIDevice currentDevice] setOrientation:UIDeviceOrientationPortrait];
This works great but this piece of code is a private api call and I can not risk a app rejection.
I have also tried to rotate the next view manually but this rotates only the view and not the navigation or the tab bar.
Is there a similary way to force orientation change?
There is currently no way to do that.
Have a look at this question, I have the same problems. With a tabbar application, you must have everything to autorotate, or nothing. You can find ways to have shouldAutorotate answer differently on each view, but actually this does not work. I don't know if this is an intended behaviour or a bug, anyway the only viable solution I'm aware of (without using undocumented API) is to manage the rotation by yourself, and do not rely on autorotation.
This means in other words that you need to start orientation notifications (look at UIDevice, there are methods to start and stop notifications about the device orientation), then for each view you want to rotate register as observer, and manage orientation manually, like this (don't remember where I got this snippet):
// Rotates the view.
CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI/2);
self.view.transform = transform;