I need to block rotation to landscape orientation because my app crashes when it attempts to rotate views to landscape.
How do I block rotation to landscape and where can I setup the block such that it applies to all my views?
Thanks
In your view controllers:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
if (interfaceOrientation==UIInterfaceOrientationPortrait ) {
return YES;
} else {
return NO;
}
}
This will prevent your views from trying to rotate to landscape or upsidedown-portrait. However, this should be default. If you haven't set up code to handle landscape orientation then the views shouldn't be trying to rotate anyway. Your crashing is most likely coming from somewhere else.
Related
I used to allow rotate my ios app in ipad:
My issue is the white space in the right in landscape orientation, but in portrait is well.
I think that I put a condition if the orientation in landscape or in portrait and adjust the width of the view controller, but how can I adjust it? and what shall the condition be?
Please help!
Here are macros UIDeviceOrientationIsLandscape and UIDeviceOrientationIsPortrait
so rather checking separately you can do it like this ...
if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
{
// code for landscape orientation
}
OR
if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
{
// code for Portrait orientation
}
After check the orientation. you can apply condition.
and also please read autosizing
I browsed through most of the questions and tried almost everything. But bad part is that the issue is still there.
I have a UIVIew which is always launched in landscape mode and I am presenting a second view (detailView) as a full screen modal view.
The detailview has a UIwebview on top of it.
When I present the detailView as a modal view, the webview is being shown in portrait mode.
I am returning "YES" in shouldAutoRotateToInterfaceOrientation and also have set autoresize , autoresizingMask and scalePageToFit properties.
When I rotate the device, and when the detailView is in front, the webview arranges to landscape properly.
The issue is only when I present the modalView for the first time.
Rotating the device is adjusting the layout properly.
As far as I am aware ModalViews on the iPhone do not support Landscape View. The case may be different for iPhone 5.
But it sounds like you are setting the ModalView not the WebView to landscape, I'd suggest a different approach to handling this.
For Example you could animate the DetailView in like a ModalView so it starts in the correct orientation
If you are running your app on iOS 6 you will need the following code in the modal view controller to support the landscape orientation:
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
- (BOOL) shouldAutorotate {
return YES;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationLandscapeLeft;
}
Also, make sure you are testing on the actual device as opposed to the simulator because auto-rotation behaves differently on the simulator.
That will do the trick:
CGAffineTransform transform = CGAffineTransformIdentity;
webView.transform = CGAffineTransformRotate(transform,-M_PI/2);
OK. First of all, the question I had posted was not clear. My question was that, when I load a webview, in portrait, it was loading the webview elements in landscape mode(CSS), and was getting loaded as portrait mode(CSS) in landscape orientation.
Turns out that I was not applying the correct CSS style.
The fix I did was:
In ViewDidLoad and willAnimateRotation method, I am posting a notification to my javascript to update the style based on orientation :)
I'm creating a puzzle game for the iphone. All of my menus, etc are only in the portrait orientation, but my gameplay can be in either portrait or landscape.
So when I'm in the menu and the device is being held in landscape, then when I go to the gameplay viewcontroller, it does the layout with the landscape coordinates, but the screen is still actually oriented portrait-wise:
I want to force the device to re-orient the screen before I do the layout. How do I do that?
right now I'm testing for device orientation like this:
- (void) viewDidLoad {
.
.
.
if (!UIDeviceOrientationIsLandscape([[UIDevice currentDevice] orientation])) {
// Run Portrait Layout
}
else {
// Run Landscape Layout
}
}
So I know that the device is returning an orientation of landscape, but the screen is still oriented portrait for some reason.
Perhaps try..
[UIViewController attemptRotationToDeviceOrientation];
In viewDidLoad or viewWillAppear, that should make it attempt an orientation change
There are a few methods in UIViewController that you can override to catch the view-did-rotate event:
– willRotateToInterfaceOrientation:duration:
– willAnimateRotationToInterfaceOrientation:duration:
– didRotateFromInterfaceOrientation:
Do check the UIViewController class reference
I have a tableview and detail view app, in the tableview I have this to stop it from being able to rotate.
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation {
return false;
}
so the main view should always be in portrait mode.
however the subview is able to change rotation freely.. if i go from the detail view while in landscape view to the parent tableview then the tableview appears in landscape and cannot rotate back.. hoping someone can help me fix this.
You're telling it never to rotate, what you actually want is:
return (interfaceOrientation == UIInterfaceOrientationPortrait)
which says it should only rotate when it's to portrait orientation.
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
}
}