Wednesday 30 July 2014

How to set view for particular orientation in any view forcefully?



Here using this bellow code you can set any orientation forcefully to any view...


[[UIDevice currentDevice] performSelector:NSSelectorFromString(@"setOrientation:")

                                                                      withObject:(__bridge id)((void*)UIInterfaceOrientationPortrait)];

after put this code you get warning that "PerformSelector may cause a leak because its selector is unknown"

for remove this code you can use this above code with bellow solution...

First define the bellow code in your .m file

#define SuppressPerformSelectorLeakWarning(Stuff) \
do { \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Warc-performSelector-leaks\"") \
Stuff; \
_Pragma("clang diagnostic pop") \
} while (0)

after use that above code with bellow method...

SuppressPerformSelectorLeakWarning(
         [[UIDevice currentDevice] performSelector:NSSelectorFromString(@"setOrientation:")
                                                                      withObject:(__bridge id)((void*)UIInterfaceOrientationPortrait)];
    );

you can use this code in viewDidLoad method or anywhere in which you want to display whole view in particular orientation forcefully

Friday 25 July 2014

Set All Orientation for only one UIViewController in your whole application.

In AppDelegate class just use this bellow method which is available in iOS 6 and later....

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    // Get topmost/visible view controller
    UIViewController *currentViewController = [self topViewController];// this topViewController is my custom method which i post bellow...

    if ([currentViewController isKindOfClass:[YourClassName class]]) {
            // Unlock landscape view orientations for this view controller
            return UIInterfaceOrientationMaskAllButUpsideDown;
    }
    
    // Only allow portrait (standard behaviour)
    return UIInterfaceOrientationMaskPortrait;
}

This is custom methods which is used to find out top viewController

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

}

Also For do some changes in your that class in which you want to available different orientation then in that class's use bellow method...

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
        NSLog(@"Landscape Orientation Called");
    }
    else{
        NSLog(@"Potrait Orientation Called");
    }
}  

If Any suggestion then please post comment...

Wednesday 9 July 2014

Set Landscape Oriantation forcefull with bellow code.

    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
    
    float   angle = M_PI/2//rotate 180°, or 1 π radians

    self.view.layer.transform = CATransform3DMakeRotation(angle, 0, 0.0, 1.0);