How to hide the status bar programmatically in iOS 7? - iphone

In ios7, how can I hide the statusbar programmatically? I am using XCode 4.6.1 (ios6.1) and I want to implement this in XCode itself.

in iOS7 you should implement in your viewController
- (BOOL)prefersStatusBarHidden {
return YES;
}

you can hide status bar to set the key value "View controller-based status bar appearance" NO in plist. This is easiest way.
or You can hide in code by using property statusBarHidden of UIApplication class.
[[UIApplication sharedApplication] setStatusBarHidden:YES];
Swift 3.0
Hide status bar for any particular view controller
override var prefersStatusBarHidden: Bool {
get {
return true
}
}
Hide Status bas across the application
UIApplication.shared.isStatusBarHidden = true
and set the key value "View controller-based status bar appearance" NO in info plist of project.

- (void)viewDidLoad
{
[super viewDidLoad];
if ([self respondsToSelector:#selector(setNeedsStatusBarAppearanceUpdate)])
{
[self prefersStatusBarHidden];
[self performSelector:#selector(setNeedsStatusBarAppearanceUpdate)];
}
else
{
// iOS 6
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
}
}
// Add this method
- (BOOL)prefersStatusBarHidden {
return YES;
}

To hide for a specific ViewController (and then turn back on) when View controller-based status bar appearance set to NO:
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
}
-(void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
}

If you need to hide/show it on a given view controller dynamically you can do something like this.
(Although I recommend just using - (BOOL)prefersStatusBarHidden to return your preference if you don't need it to change.)
// view controller header
#interface MyViewController : UIViewController {
BOOL shouldHideStatusBar;
}
#end
#implementation
- (BOOL)prefersStatusBarHidden {
return shouldHideStatusBar; // backed by your instance variable
}
- (void)setPrefersStatusBarHidden:(BOOL)hidden {
shouldHideStatusBar = hidden;
// Don't call this on iOS 6 or it will crash since the
// `setNeedsStatusBarAppearanceUpdate` method doesn't exist
[self performSelector:#selector(setNeedsStatusBarAppearanceUpdate)];
// [self setNeedsStatusBarAppearanceUpdate]; // (if Xcode 5, use this)
}
#end

In case of iOS >= 7.0 use following code :
Syntax:
// Present in UIViewController of UIKit Frameworks
- (BOOL)prefersStatusBarHidden NS_AVAILABLE_IOS(7_0); // Defaults to NO
Usage:
- (BOOL)prefersStatusBarHidden {
return YES;
}
In iOS < 7.0 use following code :
Syntax:
// Present in UIApplication of UIKit Frameworks
- (void)setStatusBarHidden:(BOOL)hidden withAnimation:(UIStatusBarAnimation)animation NS_AVAILABLE_IOS(3_2);
Usage:
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];

Swift 4.1
step1. Set View controller-based status bar appearance in your info.plist to YES
step2. Type some code in your UIViewController, status bar will hide when you present the UIViewController.
private var statusBarIsHidden = false
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
statusBarIsHidden = true
setNeedsStatusBarAppearanceUpdate()
}
override var prefersStatusBarHidden: Bool {
return statusBarStatus
}

Try this
[[UIApplication sharedApplication] setStatusBarHidden:YES];

If anyone wanted the most updated way to do it (Swift 2.1 and latest Xcode 7.2)
Set "View controller based status bar appearance to NO in your info.plist"
UIApplication.sharedApplication().statusBarHidden = true // put inside app delegate somewhere (applicationWill or DidFinishLaunchingWithOptions:

My experience is that you need both the code and the value in the info.plist file in iOS 9 / Xcode 7.3.
Add this to your viewDidLoad method.
[[UIApplication sharedApplication] setStatusBarHidden:YES];
Add this to your info.plist file as a boolean value and set it to NO:
View controller-based status bar appearance

If using iOS 9.0+ and Swift. If you want to have status bar hidden in some view controllers but not all - make sure to have View controller-based status bar appearance value in Info.plist set to YES else same parameters will be used across all view controllers.
And override prefersStatusBarHidden in subclass of UIViewController
override var prefersStatusBarHidden: Bool {
get {
return true
}
}
Similar can be done to change preferredStatusBarStyle
override var preferredStatusBarStyle: UIStatusBarStyle {
get {
return .lightContent
}
}

on swift 4:
UIApplication.shared.isStatusBarHidden = ..true/false

Related

Custom Segue Portrait Mode

I am trying to create a custom segue to change and lock my next view into portrait mode, no matter how the device is rotated. I was able to create my UIStoryBoardSegue files, but now I am stuck with the code to switch from landscape to portrait... this is what I did so far:
-(void) perform
{
self.appDelegate = [[UIApplication sharedApplication] delegate];
UIViewController *source = (UIViewController *) self.sourceViewController;
UIViewController *destination = (UIViewController *) self.destinationViewController;
}
And now I am stuck... help!
I don't know if you are writting for iOS 6 or per iOS 6, but if it is iOS 6, try placing the following code in the view controller that you are trying to set its orientation.
- (BOOL) shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}
Now the method above has all the masks and you can remove the masks you don't need. There is really no need to access the app delegate for rotation setting any more. Hope this helps you achieve what you want.
One way is to overload the supportedDeviceOrientations method of a view controller subclass like this
- (NSUInteger)supportedInterfaceOrientations {
return UIDeviceOrientationPortrait;
}

How to allow only single UIViewController to rotate in both Landscape and Portrait direction?

My app is only for iphone device (both iphone 4 and 5) and built to support only ios 6.
My whole app only supports portrait mode. But there is one view called "ChatView" , which i want to support both landscape and portrait modes.
I have set the required device rotations as follows -
I have also tried following code to support rotation in "ChatView" -
-(BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
But it could not rotate that view.
I have searched a lot for this , but could not be able to find the solution for my issue.
And also in "ChatView" there are some objects like buttons, textfields whose frames are set programmaticaly. So i want to know should i have to set frames of all those objects for landscape mode also?
Please help me.
Thanks.....
Simple but it work very fine. IOS 7.1 and 8
AppDelegate.h
#property () BOOL restrictRotation;
AppDelegate.m
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if(self.restrictRotation)
return UIInterfaceOrientationMaskPortrait;
else
return UIInterfaceOrientationMaskAll;
}
ViewController
-(void) restrictRotation:(BOOL) restriction
{
AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
appDelegate.restrictRotation = restriction;
}
viewDidLoad
[self restrictRotation:YES]; or NO
I think if you want to support just one viewcontroller rotation, it is not possible since application will follow orientations set by you in .plist file. An alternate you can follow is to support your app for both landscape and portrait, freeze all viewcontrollers rotation to portrait except for chat view.
EDIT
To subclass UINavigationController, create a new file with name e.g. CustomNavigationController and make it subclass of UINavigationController.
.h file
#import <UIKit/UIKit.h>
#interface CustomNavigationController : UINavigationController
#end
.m file
#import "CustomNavigationController.h"
#interface CustomNavigationController ()
#end
#implementation CustomNavigationController
-(BOOL)shouldAutorotate
{
return NO;
}
-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}
#end
Set the class of your UINavigationController in your main class xib as CustomNavigationController. Hope it helps ypu..
Your view controller will never rotate to any position that is not supported by the app itself. You should enable all possible rotations and then in view controllers that are not supposed to rotate put the following lines
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
In ChatView, it should be:
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
If you need to change your layout after a rotation you should implement the appropriate changes to your subviews in
- (void)viewWillLayoutSubviews
Use self.view.bounds to check the current size of the view, since self.view.frame doesn't change after rotations.
for the specific viewcontroller.m you want to rotate
add this method:
- (BOOL)canAutoRotate
{
return YES;
}
then inside your AppDelegate.m
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
UIViewController *currentViewController = [self topViewController];
if ([currentViewController respondsToSelector:#selector(canAutoRotate)]) {
NSMethodSignature *signature = [currentViewController methodSignatureForSelector:#selector(canAutoRotate)];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setSelector:#selector(canAutoRotate)];
[invocation setTarget:currentViewController];
[invocation invoke];
BOOL canAutorotate = NO;
[invocation getReturnValue:&canAutorotate];
if (canAutorotate) {
return UIInterfaceOrientationMaskAll;
}
}
return UIInterfaceOrientationMaskPortrait;
}
- (UIViewController *)topViewController
{
return [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}
- (UIViewController *)topViewControllerWithRootViewController:(UIViewController *)rootViewController
{
if ([rootViewController isKindOfClass:[UITabBarController class]]) {
UITabBarController* tabBarController = (UITabBarController*)rootViewController;
return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
} else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
UINavigationController* navigationController = (UINavigationController*)rootViewController;
return [self topViewControllerWithRootViewController:navigationController.visibleViewController];
} else if (rootViewController.presentedViewController) {
UIViewController* presentedViewController = rootViewController.presentedViewController;
return [self topViewControllerWithRootViewController:presentedViewController];
} else {
return rootViewController;
}
}
Ted's answer works well with the issue mentioned by Alexander of Norway.
But I figured that issue is not happening the way Alexander explained,
When ViewController B which currently is in landscape (All
orientations enabled) returns back to ViewController A. (Portrait
only) after the user clicks on the back button,
supportedInterfaceOrientationsForWindow doesn't get called and
ViewController A ends up in landscape
Actually when ViewController B which currently is in landscape (All orientations enabled) returns back to ViewController A (Portrait only) after the user clicks on the back button, Appdelegate
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation;
is getting called. But still root view controller is ViewController B (that rotation enabled view controller), ViewController A is not getting back to portrait orientation, since ViewController B is still returning
-(BOOL)shouldAutorotate{
return YES;
}
So when you press back button return, "shouldAutorotate -> NO" in ViewController B. Then ViewController A will come to Portrait orientation. This is what I did
#property (nonatomic, assign) BOOL canAutoRotate;
#pragma mark - Public methods
- (BOOL)canAutoRotate
{
return _canAutoRotate;
}
#pragma mark - Button actions
- (void)backButtonPressed:(UIButton *)sender {
_canAutoRotate = NO;
(...)
}
#pragma mark - Init
- (id)init{
if(self=[super init]) {
_canAutoRotate = YES;
}
return self;
}
Swift 3 Kosher Version
I've left this here just for a case somebody has the problem.
Apple's documentation for supportedInterfaceOrientations says:
When the user changes the device orientation, the system calls this method on the root view controller or the topmost presented view controller that fills the window. If the view controller supports the new orientation, the window and view controller are rotated to the new orientation. This method is only called if the view controller's shouldAutorotate method returns true.
In few words you have to override supportedInterfaceOrientations in root view controller so that it returns the value for its top child view controller and default value otherwise.
What you should do is checking if app supports all modes (go to Deployment info in targets General settings or Info.plist), find out the class of your root view controller. It can be generic UIViewController, UINavigationController, UITabBarController or some custom class. You can check it out this way:
dump(UIApplication.shared.keyWindow?.rootViewController)
Or any other way you like.
Let it be some CustomNavigationController. So you should override supportedInterfaceOrientations like this:
class CustomNavigationController: UINavigationController {
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return topViewController?.supportedInterfaceOrientations ?? .allButUpsideDown
}
}
In any view controller which should support only portrait orientation for instance override supportedInterfaceOrientations this way:
class ChildViewController: UIViewController {
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .portrait
}
}
Then don't forget to check if shouldAutorotate of you root view controller and topmost presented view controller already returns true. If not, add this to classes definitions:
override var shouldAutorotate: Bool {
return true
}
Otherwise supportedInterfaceOrientations will nor be called at all.
Here you go!
If you need to fix the opposite problem when only one view controller should support a bunch of orientations and others don't, make this changes to every view controller but this one.
Hope this will help.
I know this questions is very old but It needs an updated answer. The easiest and most correct way to achieve this result is to enable Portrait and Landscape in your app settings. Then add this code to your app delegate:
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
if let navigationController = self.window?.rootViewController as? UINavigationController {
if navigationController.visibleViewController is INSERTYOURVIEWCONTROLLERHERE {
return UIInterfaceOrientationMask.All
}
else {
return UIInterfaceOrientationMask.Portrait
}
}
return UIInterfaceOrientationMask.Portrait
}
Dont forget to replace "INSERTYOURVIEWCONTROLLERHERE" with your view controller.
I'm not sure about the history of this issue (now = iOS 10 timeframe), but the easiest solution was missing as I posted this in Oct. 2016.
Assuming you want this:
Supporting iOS 7 and newer only (including iOS 10)
Some view controllers should support all orientations, others should support a subset of orientations. Example of what I mean: one view controller should only support portrait, while all others should support all orientations
All view controllers must auto-rotate if they support rotation (meaning, you don't want the code that fixes this issue in your view controllers)
Support adding UINavigationControllers in XIBs/NIBs/Storyboards without having to do anything to them
... then (IMO) the easiest solution is to make a UINavigationControllerDelegate, NOT sub-class UINavigationController (which violates assumption 4 above).
When I solved this, I decided to make my first ViewController a UINavigationControllerDelegate. This view controller sets itself as the navigation controller's delegate, and returns which orientations are allowed. In my case the default is that all orientations are allowed, with portrait preferred, but in one particular case only portrait is allowed. Code below is from Swift 3 / XCode 8:
class iPhoneStartViewController: UIViewController {
var navInterfaceOrientationMask: UIInterfaceOrientationMask?
var navInterfaceOrientationPreferred: UIInterfaceOrientation! = .portrait
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.delegate = self
}
#IBAction func cameraButtonPressed(_ sender: AnyObject) {
if PermissionsHelper.singleton().photosPermissionGranted() == false {
self.navInterfaceOrientationMask = nil // default is: all orientations supported
self.performSegue(withIdentifier: "segueToPhotoAccess", sender: self)
} else {
self.navInterfaceOrientationMask = .portrait // this stops the next view controller from being to rotate away from portrait
self.performSegue(withIdentifier: "segueToCamera", sender: self)
}
}
}
// lock orientation to portrait in certain cases only. Default is: all orientations supported
extension iPhoneStartViewController : UINavigationControllerDelegate {
public func navigationControllerSupportedInterfaceOrientations(_ navigationController: UINavigationController) -> UIInterfaceOrientationMask {
if let mask = self.navInterfaceOrientationMask {
return mask
} else {
return .all
}
}
public func navigationControllerPreferredInterfaceOrientationForPresentation(_ navigationController: UINavigationController) -> UIInterfaceOrientation {
return self.navInterfaceOrientationPreferred
}
}
Based on #iAnum's answer, I've enabled autorotate and UIViewController class detection.
This is because otherwise transitioning into and out of the "special view controller" won't correct to portrait orientation, and you'll be stuck in an unsupported orientation.
I only had one view supporting landscape, so I just hard-coded it in the custom navigation view controller:
-(BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
//Access the current top object.
UIViewController *viewController = [self.viewControllers lastObject];
//Is it one of the landscape supported ones?
if ([viewController isMemberOfClass:[SpecialViewController class]]) {
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
} else
return UIInterfaceOrientationMaskPortrait;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
//Access the current top object.
UIViewController *viewController = [self.viewControllers lastObject];
//Is it one of the landscape supported ones?
if ([viewController isMemberOfClass:[SpecialViewController class]]) {
return interfaceOrientation;
} else
return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}
There's a problem popping VCs discussed here https://stackoverflow.com/a/15057537/1277350 where pressing back while in landscape won't even call the orientation methods, so you've got to hack it a bit by showing and dismissing a modal view.
And then just remember that if you want willShowViewController to fire, you need to set self.delegate = self and add the UINavigationControllerDelegate to your custom navigation controller along with the code below.
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
UIApplication* application = [UIApplication sharedApplication];
if (application.statusBarOrientation != UIInterfaceOrientationPortrait)
{
UIViewController *c = [[UIViewController alloc]init];
[c.view setBackgroundColor:[UIColor clearColor]];
[navigationController presentViewController:c animated:NO completion:^{
[self dismissViewControllerAnimated:YES completion:^{
}];
}];
}
}
make a subclass of UINavigationController like so:
MyNavigationController.h
#import <UIKit/UIKit.h>
#interface MyNavigationController : UINavigationController
#end
MyNavigationController.m
#import "MyNavigationController.h"
#import "ServicesVC.h"
#implementation MyNavigationController
-(BOOL)shouldAutorotate{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations{
if ([[self.viewControllers lastObject] isKindOfClass:[ServicesVC class]]) {
return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}
return UIInterfaceOrientationMaskAll;
}
#end
assuming that your viewcontroller is named: ServicesVC
Here is the answer of Alexander (https://stackoverflow.com/posts/25507963/revisions) in Swift:
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {
var currentViewController: UIViewController? = self.topViewController()
if currentViewController != nil && currentViewController!.canAutoRotate() {
return Int(UIInterfaceOrientationMask.All.rawValue)
}
return Int(UIInterfaceOrientationMask.Portrait.rawValue)
}
func topViewController() -> UIViewController? {
if UIApplication.sharedApplication().keyWindow != nil
{
return self.topViewControllerWithRootViewController(UIApplication.sharedApplication().keyWindow!.rootViewController!)
}
return nil
}
func topViewControllerWithRootViewController(rootViewController: UIViewController?) -> UIViewController? {
if rootViewController == nil {
return nil
}
if rootViewController!.isKindOfClass(UITabBarController) {
var tabBarController: UITabBarController = (rootViewController as? UITabBarController)!
return self.topViewControllerWithRootViewController(tabBarController.selectedViewController)
}
else {
if rootViewController!.isKindOfClass(UINavigationController) {
var navigationController: UINavigationController = (rootViewController as? UINavigationController)!
return self.topViewControllerWithRootViewController(navigationController.visibleViewController)
}
else {
if (rootViewController!.presentedViewController != nil) {
var presentedViewController: UIViewController = rootViewController!.presentedViewController!
return self.topViewControllerWithRootViewController(presentedViewController)
}
else {
return rootViewController
}
}
}
}
In addition, you will need to add the following snippet in AppDelegate.swift:
extension UIViewController {
func canAutoRotate() -> Bool {
return false
}}
And for ViewControllers for which you want to allow all rotations, add this function:
override func canAutoRotate() -> Bool {
return true
}
// paste this method in app deligate class
- (UIInterfaceOrientationMask)application:(UIApplication )application supportedInterfaceOrientationsForWindow:(UIWindow )window
{
if ([self.window.rootViewController.presentedViewController isKindOfClass: [_moviePlayerController class]])
{
if (self.window.rootViewController.presentedViewController)
return UIInterfaceOrientationMaskAll;
else return UIInterfaceOrientationMaskPortrait;
}
else return UIInterfaceOrientationMaskPortrait;
}
I had the same situation. So I subclassed UINavigationController into CustomNavigationController, and inside this CustomNavigationController, I wrote
#define IOS_OLDER_THAN_6 ( [ [ [ UIDevice currentDevice ] systemVersion ] floatValue ] < 6.0 )
#define IOS_NEWER_OR_EQUAL_TO_6 ( [ [ [ UIDevice currentDevice ] systemVersion ] floatValue ] >= 6.0 )
#pragma mark - Rotation
#ifdef IOS_OLDER_THAN_6
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}
#endif
#ifdef IOS_NEWER_OR_EQUAL_TO_6
-(BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;;
}
#endif
I used this CustomNavigationController instead of existing NavigationController.
Then inside the view controller that you have to display in LandScape Orientation say LandScapeView, I wrote
#pragma mark - Rotation
#ifdef IOS_OLDER_THAN_6
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
return (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight | toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
#endif
#ifdef IOS_NEWER_OR_EQUAL_TO_6
-(BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft;
}
#endif
Inside CustomNavigationController, I presented this view controller, not pushed into Navigation Stack. So the LandScapeView appeared in LandScape Orientation.
LandScapeView *graph = [[LandScapeView alloc]init....];
[self presentViewController:graph animated:YES completion:nil];
I did not change anything in the Supported Interface Orientation in Project Settings.
If the App is supporting from IOS7 to IOS9 use this code for Orientation:
#if __IPHONE_OS_VERSION_MAX_ALLOWED < 90000
- (NSUInteger)supportedInterfaceOrientations
#else
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
#endif
{
if([AppDelegate isPad]) return UIInterfaceOrientationMaskAll;
else return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}

Orientation should change only for the selected viewcontroller

In my app, Four viewcontrollers navigates next by next in potrait mode, but i added coverflow in the final viewcontroller, and when the simulator is rotated it should go landscape.I selected all orientations in plist execpt the Upsidedown orientation, So that coverflow works fine in Landscape, but all the other viewcontrollers also goes to landscape when rotatad, but i need these viewcontrollers to be in potrait even the simulator is rotated.I tried many codes like,
shouldAutorotate and supportedInterfaceOrientations.
But i doesn't get a clear solution towards what iam expecting, if any ideas , will be thankfull.
Add an observer to the viewDidLoad method of the view you want to rotate like this :
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter]
addObserver:self selector:#selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:[UIDevice currentDevice]];
and then set the views according the the landscape view inside the orientationChanged method like this :
- (void) orientationChanged:(NSNotification *)note{
UIDevice * device = [UIDevice currentDevice];
switch(device.orientation)
{
case UIDeviceOrientationPortrait:
break;
case UIDeviceOrientationPortraitUpsideDown:
break;
case UIDeviceOrientationLandscapeLeft:
break;
case UIDeviceOrientationLandscapeRight:
break;
default:
break;
};
}
Add new Objective-C class (subclass of UINavigationController) and add the following code to the .m files
-(NSUInteger)supportedInterfaceOrientations
{
NSLog(#"supportedInterfaceOrientations = %d ", [self.topViewController supportedInterfaceOrientations]);
return [self.topViewController supportedInterfaceOrientations];
}
-(BOOL)shouldAutorotate
{
return self.topViewController.shouldAutorotate;
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// You do not need this method if you are not supporting earlier iOS Versions
return [self.topViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
After you added the new classes go to your ViewController classes and make the following changes
- (BOOL)shouldAutorotate // iOS 6 autorotation fix
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations // iOS 6 autorotation fix
{
return UIInterfaceOrientationMaskAll;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation // iOS 6 autorotation fix
{
return UIInterfaceOrientationPortrait;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return YES;
}
In the shouldAutorotate , shouldAutorotateToInterfaceOrientation: return YES if you want the ViewController to be supporting Multiple orientation else return NO , also in houldAutorotateToInterfaceOrientation: method pass the Orintation you want for that specific ViewController , Repeat the same for all the view controllers .
Reason of doing this:-
1:Although you can change the preferredInterfaceOrientationForPresentation: of any viewController to a specific orientation but since you are using the UINavigationController you also need to override the supportedInterfaceOrientations for your UINavigationController
2:In order the override the supportedInterfaceOrientations for UINavigationController we have subclassed UINavigationController and modified the method related to the UINavigation Orientation.
Hope it will help you !
In iOS 6 and later, the view controllers responsible for rotation are the container Viewcontrollers such as UINavigationController & UITabBarController . What are you using as the rootviewcontroller in your project??
I have navigation controller category written here in which you can handle the supported interface orientations for the individual view controllers.
viewDidLoad or viewWillApper method add following code
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];
set the orientation
#if (__IPHONE_6_0 >= __IPHONE_OS_VERSION_MAX_ALLOWED)
- (NSInteger)supportedInterfaceOrientations {
return (UIInterfaceOrientationMaskPortraitUpsideDown | UIInterfaceOrientationMaskPortrait);
}
#endif
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation==UIInterfaceOrientationPortrait);
}

Trick preferredInterfaceOrientationForPresentation to fire on viewController change

I am using MSNavigationPaneViewController from here and have rotation sort of working.
I've overridden the rotation methods in my root UINavigationController
-(BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
if (appDelegate.topViewController != nil) {
return [appDelegate.topViewController supportedInterfaceOrientations];
} else {
return UIInterfaceOrientationMaskPortrait;
}
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
if (appDelegate.topViewController != nil) {
return [appDelegate.topViewController preferredInterfaceOrientationForPresentation];
} else {
return UIInterfaceOrientationPortrait;
}
}
which I push the MSNavigationPaneViewController with using presentViewController: animated:completion: and I have rotation working so certain views can have different orientations. The problem is, each view that has a different orientation needs the user to tilt the phone to change the orientation at which point it locks on the correct orientation.
I've done tons of reading to get this working and it seems I need preferredInterfaceOrientationForPresentation to fire before each view is loaded, but it's not firing.
I think it's not firing because the MSNavigationPaneViewController isn't changing view controllers using presentViewController.
This is the code used by MSNavigationPaneViewController to change the view
- (void)setPaneViewController:(UIViewController *)paneViewController
{
if (_paneViewController == nil) {
paneViewController.view.frame = _paneView.bounds;
_paneViewController = paneViewController;
[self addChildViewController:_paneViewController];
[_paneView addSubview:_paneViewController.view];
[_paneViewController didMoveToParentViewController:self];
} else if (_paneViewController != paneViewController) {
paneViewController.view.frame = _paneView.bounds;
[_paneViewController willMoveToParentViewController:nil];
[self addChildViewController:paneViewController];
void(^transitionCompletion)(BOOL finished) = ^(BOOL finished) {
[_paneViewController removeFromParentViewController];
[paneViewController didMoveToParentViewController:self];
_paneViewController = paneViewController;
};
[self transitionFromViewController:_paneViewController
toViewController:paneViewController
duration:0
options:UIViewAnimationOptionTransitionNone
animations:nil
completion:transitionCompletion];
}
}
which looks like it's just switching the viewController and therefor not calling preferredInterfaceOrientationForPresentation
Is there any way to force preferredInterfaceOrientationForPresentation to be called, or to trick it into be called maybe by a hidden view?
Thanks
EDIT ----
I totally understand how the new rotation system works in iOS6 and that it's down to the root to manage it. However preferredInterfaceOrientationForPresentation only seems to be called if the next view is being presented with one of the correct methods. Not if the window is just switched.
So I do actually need a way to trick this to be called.
You can trick the window into re-valuating its supported interface orientations by resetting its rootViewController.
UIApplication *app = [UIApplication sharedApplication];
UIWindow *window = [[app windows] objectAtIndex:0];
UIViewController *root = window.rootViewController;
window.rootViewController = nil;
window.rootViewController = root;
You'd need to make sure that when this code executes, -supportedInterfaceOrientations of the root view controller returns the proper mask (corresponding to how you want to force the orientation).
Be aware, this hack can cause a quick flash on the screen and glitch any ongoing animations.
preferredInterfaceOrientationForPresentation will not be called on controllers within a UINavigationController.
You are going to have to subclass UINavigationController and then implement this feature yourself. You'd simply want to delegated to topViewController.
For reference check out: http://code.shabz.co/post/32051014482/ios-6-supportedorientations-with-uinavigationcontroller
I also found this implementation of a UINavigationController subclass: https://gist.github.com/3842178

Get the Identifier of the current tab - iphone

Probably a simple question, but I'm having a hellofa time finding a solution to it.
I need to find the identifier of the current tab in a tabbarcontroller and use it in a conditional to run a method.
how do I find this?
if (self.tabbarcontroller.identifier == #"My identifier") {
// do some method
} else {
// do the default method
}
UIWindow *window = [[UIApplication sharedApplication] keyWindow];
UITabBarController *tabBarController = (UITabBarController*) window.rootViewController;
UIViewController *selectedVC = tabBarController.selectedViewController;
if ([selectedVC.identifier isEqualToString:#"anIdentifier"])
{
// Do something
} else {
// Do something else
}
You can set the identifier of a ViewController in your storyboard
Check out the following code. Also make sure that the delegate for the UITabBar is correct pointing to the view controller, in this case FirstViewController.
**FirstViewController.h****
#interface FirstViewController : UIViewController<UITabBarDelegate>
**FirstViewController.m:**
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
NSLog(#"%#",[item tag]);
}