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
Related
I'm adding a view to view controller in which there are certain components. My project needs to support both orientation.
I designed the view controller using story board in landscape mode. When a button is pressed in the view controller the view is shown using the scaling animation.
It works perfect when its in landscape mode. When its in landscape mode and button is pressed it works perfect and also the rotation also works perfect. But if its in landscape mode and button is pressed the view doesn't get scaled according to the portrait mode and rotation is also a big problem.
I'm using autosizing and not auto layout
Can anyone please help me? Sorry for my bad english.
Any help is appreciated.
Autosizing has never proved to be the best solution for frame changes in orientation modes. Instead, change the frames manually in orientation delegate methods:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation;
check for your current orientation like:
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
// change frames here for portrait mode
}
else
{
// change frames here for landscape mode
}
}
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 :)
The iPhone has a orientation lock for portrait only. So I added a landscape orientation lock button to my UI that sets a landscape_orientation_locked variable to YES or NO. Then in shouldAutorotateToInterfaceOrientation: I basically do the following:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if (landscape_orientation_locked)
return (UIInterfaceOrientationIsLandscape(interfaceOrientation));
return YES;
}
This works fine, except for one case. When I turn the iPhone to landscape orientation, toggle the orientation lock button, rotate the iPhone to portrait orientation and hit the button again. The interface orientation won't change from landscape to portrait. The iPhone has to be turned into landscape and then portrait orientation to trigger the interface rotation.
So my question is: Can I somehow 'force' the iPhone to re-evaluate it's current orientation?
Presenting and then dismissing a mock modal view controller should force an orientation check.
EDIT: Found the question where I first read about that:
Is there a documented way to set the iPhone 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
}
}
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.