I simply want to lock rotation for the status bar when some conditions are fulfilled:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
if(imageView.hidden == NO){
//lock the status bar rotation
}else{
//unlock the status bar rotation
}
}
}
Anyway to lock/unlock the status bar programmatically? Thanx.
You need to override the proper methods in your view controller class. In iOS 6+ you need to override supportedInterfaceOrientations. If you also need to support iOS 5 also override shouldAutorotateToInterfaceOrientation:.
Both implementations should return appropriate values on the currently allowed orientations.
Related
I ma trying to lock screen orientation to only landscape orientation when a certain image is still visible, then when the image is hidden, unlock all orientations (targeting iOS 6):
-(BOOL)shouldAutorotate{
if (self.splashImageView.hidden == NO) {
return UIInterfaceOrientationMaskPortrait;//gets called when image is visible
}else{
return UIInterfaceOrientationMaskAll;//gets called when image is hidden
}
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[self shouldAutorotate];
}
As you may notice, shouldAutorotate is called properly but the screen is always supporting landscape orientation even when the image is still visible, is there something missing?
P.S: Please note I am trying to get that to work on a tabbar view controller (a UIViewController subclass).
In your appdelegate you have those two methods.but do you have setting like in your project settings -> go to summary tab and see if orientation is set to only landscape or all.Just try to set those.
For our iPad application, I would like to allow for auto rotation of the screen in landscape mode (not portrait) to support both possible orientations. However, there are portions of our application during which the user needs shake and move the iPad into directions and orientations, that will trigger the auto rotation feature but should not.
is it possible to de- and re-activate the autoorientation, such that the orientation will be locked when entering this section of the app and unlocked when exiting?
if the specific section means another view controller, YES it is possible
just add this line of code to the controller ..
// which orientation that this view controller support
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
// prevent rotation
- (BOOL)shouldAutorotate
{
return NO;
}
// after present this view controller, which orientation do you prefer ?
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
my app has been working fine until I've tried making some changes using the ios 6 SDK
The app runs in portrait mode 99% of the time. This has been constrained by only allowing portait mode to be available in the info.plist
There is one view controller which needs to be shown in landscape mode. This is achieved "manually" by simply rotating the view by 90 degrees, like so:
self.view.transform = CGAffineTransformMakeRotation(3.14159/2);
This still works fine in iOS 6.
However, this view controller has some text fields. When the user taps one it shows the keyboard. Since I've only rotated the view (and not actually changed the orientation of the device), the keyboard comes out in portrait mode, which is no good.
In previous versions of iOS, I set the orientation of the status bar to be landscape, and as a byproduct, this would set the keyboard to be landscape as well, like this:
[[UIApplication sharedApplication] setStatusBarOrientation: UIInterfaceOrientationLandscapeRight animated:NO];
However, this has stopped working for iOS 6.
I have read a million stack overflows trying to get this to work, but still have no luck.
Changing the keyboard orientation and transform is an difficult part and not a good solution(especially when it changes status bar orientations).
Better solutions is to allow application to support all orientations.
Implement the Orientation delegates inside your ViewControllers asper the rotation support.
For Supporting only Landscape
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return interfaceOrientation == UIInterfaceOrientationLandscapeLeft
|| interfaceOrientation == UIInterfaceOrientationLandscapeRight ;
}
- (BOOL)shouldAutorotate
{
return NO;
}
For Supporting only Portrait
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
Which is the best way to change the UI when orientation is changed?
For example, use two different UIView one portrait and a landscape and show one of both if orientation is changed, or use one UIView and change the UI control sizes and positions?
Any other ideas?
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
NSLog(#"Change to custom UI for landscape");
}
else if (toInterfaceOrientation == UIInterfaceOrientationPortrait ||
toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
NSLog(#"Change to custom UI for portrait");
}
}
I always recommend using the autoresizingmask for all the subviews in the view controller view. With this being set correctly all the views will resize automatically from the orientation and you don't need the extra rotation specific subviews (one portrait view and one landscape view).
In my App, I have a tabbarController and 5 view controllers managed by it.
I only want one UIView rotate to landscape and make the tabbar invisible when rotate to landscape. In current condition, all my views autorotate themselves which is not what I expect.
The code of UIView which I dont want it rotate is:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if (UIInterfaceOrientationLandscapeLeft) {
// stop the rotation, keep it as portrait orientation
// from app document, it says for UITabbarController, rootview which is uitabbarcontroller and views managed by it should all agree on the same orientation otherwise they wont rotate.
// how can i do this?
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}