How to do orientations in iphone? - iphone

I have a Setting page which contains setting functions of the application,there is a button called lock-rotation button,by default my application supports all the orientations,but my need is i want to set lock the rotation in my lock-rotation button,if the user tap the lock-rotation button it popups two button lock horizontally and lock vertically,if the user tap any one of this button it automatically stops the rotation of the corresponding orientation .How to do this?
Thanks in advance.

Sorry to say, but i dont think so that we can lock the orientations in iphone on Button Tap...Have you seen any App which does this orientation controlling... if yes plz do tell to me... Thanks
you try these methods for controlling the orientations...
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
}
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
}

Related

When an iOS device rotates, how do I change the orientation only one element?

I would like to be able to handle orientation changes in my current iPhone application. The problem is that I don't want the view of my controller to rotate or resize. When the device orientation changes, I just want one UI element to rotate 90 degrees on the screen.
Apple's camera application is a perfect example of this - when the device orientation changes, all of the buttons rotate, but the view itself does not rotate.
I suppose that I could just resize the view, move all of my elements around, and then animate the buttons, but I feel like there must be an easier way to do it.
Thanks in advance.
1) You can create 2 view hierarchies and change which is on screen in willRotateToInterfaceOrientation:
see this question
2) Or you can handle all rotations yourself
see this question
You can use
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if(interfaceOrientation==UIInterfaceOrientationPortrait || interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown)
{
//Change frames as per orientations
}
else
{
//Change frames as per orientations
}
return NO;
}
and based on the orientation manually set the frames of the UI elements you want to change.
you can access all the orientation from Constants section of this doc.
EDIT:
You can use NSNotification for the issue.
UIApplicationDidChangeStatusBarOrientationNotification
Posted when the orientation of the application's user interface
changes.
The userInfo dictionary contains an NSNumber object that encapsulates
a UIInterfaceOrientation value (see UIInterfaceOrientation). Use
UIApplicationStatusBarOrientationUserInfoKey to access this value
Availability
Available in iOS 2.0 and later.
Declared In UIApplication.h

How to change orientation of iPhone on button click?

I've been searching through lots of posts trying to find a way to change the orientation of the view manually. However I can't seem to find a definitive answer that's not outdated. Please can someone suggest the best way to switch the orientation based on when a user presses a button?
I.e. I need some help filling in the gap...
- (IBAction)buttonPressed:(id)sender
{
// Switch the orientation to ...
}
The Apple guideline doesn't allow that. There's some hack to do that but it's not a good idea to use it.
Your app will run in a real device and there isn't any piece of code that can physically turn the phone. Device orientation is constraint by real world constraint, you can't force your user to turn his phone.
[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeLeft
Change the orientation to Landscape or Portrait.
You might have to also apply a transformation to complete the change:
self.view.transform = CGAffineTransformRotate(self.view.transform, -(M_PI / 2.0));

Prevent users to see some views in landscape

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];
}

iOS how to stop view rotate

I want my iPad app to stop rotation as you rotate the iPad. I want to stop rotate every view.
Any ideas help?
if you want stop rotation for whole app then simply in app info.plist file changed Supported interface orientations ,Initial interface orientation property to portrait or landscape depends on you
In iOS6 shouldAutorotateToInterfaceOrientation has be deprecated. Override both supportedInterfaceOrientations and preferredInterfaceOrientationForPresentation instead.
Please see
Just check the auto-resizing property of your view controller.
(Fixed syntax error)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if(interfaceOrientation == UIInterfaceOrientationPortrait)
return YES;
return NO;
}
In My Project's info.plist I have deleted some key on the iPad Supported interface orientations like the following image (I have only given support for the portrait orientation)
The main idea of global controllable rotation lock is to write UIViewController category containing lock mechanism for every view controller.
You simply need to modify supportedInterfaceOrientations method globally
- (NSUInteger)supportedInterfaceOrientations
{
return __orientation;
}
Here __orientation is the static variable which can be set via category method.
The full realization of the category is presented here
Please update your projectname.plist like this. Supported interface orientations have only one object "Portrait (bottom home button)"
I strongly advise against stop rotation on iPad because supporting rotation is a must on the iPad. This is because the iPad does not have a normal way in which it will be held unlike the iPhone, which is normally held in portrait view (AKA Vertical). So you have to leave the choice to the user to eventually lock the orientation
The HIG do not actually state this as a requirement, but as a recommendation but there are many app that was rejected by this issue.
By the way if you want to this for a limit number of view controller you should implement:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if(interfaceOrientation == UIInterfaceOrientationPortrait){
return YES;
}
}

iPhone OS: Rotate just the button images, not the views

I am developing an iPad application which is basically a big drawing canvas with a couple of button at the side. (Doesn't sound very original, does it? :P)
No matter how the user holds the device, the canvas should remain in place and should not be rotated. The simplest way to achieve this would be to support just one orientation.
However, I would like the images on the buttons to rotate (like in the iPhone camera app) when the device is rotated. UIPopoverControllers should also use the users current orientation (and not appear sideways).
What is the best way to achieve this?
(I figured I could rotate the canvas back into place with an affineTransform, but I don't think it is ideal.)
Thanks in advance!
Just spouting off an idea (not sure if it would work or not)...
Perhaps you could have your screen controlled by a UIViewController that supports all orientations, but have the canvas be controlled by one that only supports a single orientation (ie, returns NO in its shouldAutorotate... method).
If that doesn't work, I'd probably just go with the affineTransform route.
I discovered a way to do this, similar to what Dave DeLong proposed.
Using a transform worked, but it wasn't ideal. Although the end result (end of the animation) was what I wanted, it would stay show some kind of shaky rotation animation.
Then I found this:
https://developer.apple.com/iphone/library/qa/qa2010/qa1688.html
Which says that a second (or third etc.) UIViewController added to the WINDOW would not receive rotation events, and therefore would never rotate. And that worked!
I created a 'fake' UIViewController with a blank view and added that as the first view controller. This receives the rotation events which I then pass on to the other view controllers that can then choose whether to rotate - the entire view or just button labels.
It is a bit hacky... But I guess the user won't notice.
Using the willRotateToInterfaceOrientation method, you should be able to use some simple logic to swap out the images:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
//The following if statement determines if it is an iPad, if it is then the interface orientation is allowed. This line can be taken out to support both iPhones an iPads.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
//Swap images and use animations to make the swap look "smooth"
//NSLog(#"Landscape Right");
} else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
//Swap images and use animations to make the swap look "smooth"
//NSLog(#"Landscape Left");
} else {
//Swap images and use animations to make the swap look "smooth"
//NSLog(#"Portrait");
}
} else {
}
}
To change an image in your interface programmatically:
[myUIImageView setImage:[UIImage imageNamed:#"myImage.png"]];
Also, to make sure the view doesn't auto-rotate use the shouldAutorotateToInterfaceOrientation method to tell your app to stay in portrait.