Wednesday, May 29, 2013

UIApplication setStatusBarOrientation example in Objective C (iOS).

UIApplication setStatusBarOrientation

Sets the application's status bar to the specified orientation, optionally animating the transition.

- (void)setStatusBarOrientation:(UIInterfaceOrientation)interfaceOrientation animated:(BOOL)animated

A specific orientation of the status bar. See UIInterfaceOrientation for details. The default value is UIInterfaceOrientationPortrait.
YES if the transition to the new orientation should be animated; NO if it should be immediate, without animation.

Discussion of [UIApplication setStatusBarOrientation]
Calling this method changes the value of the statusBarOrientation property and rotates the status bar, animating the transition if animated is YES . If your application has rotatable window content, however, you should not arbitrarily set status-bar orientation using this method. The status-bar orientation set by this method does not change if the device changes orientation.

UIApplication setStatusBarOrientation example.
//will rotate status bar
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];

//will re-rotate view according to statusbar
UIViewController *c = [[UIViewController alloc]init];
[self presentModalViewController:c animated:NO];
[self dismissModalViewControllerAnimated:NO];
[c release];

Example of [UIApplication setStatusBarOrientation].
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];

UIApplication setStatusBarOrientation example.

    CGRect bounds = [[ UIScreen mainScreen ] bounds ];
    CGAffineTransform t;
    CGFloat r = 0;
    switch ( orientation ) {
        case UIDeviceOrientationLandscapeRight:
            r = -(M_PI / 2);
        case UIDeviceOrientationLandscapeLeft:
            r  = M_PI / 2;
    if( r != 0 ){
        CGSize sz = bounds.size;
        bounds.size.width = sz.height;
        bounds.size.height = sz.width;
    t = CGAffineTransformMakeRotation( r );

    UIApplication *application = [ UIApplication sharedApplication ];

    [ UIView beginAnimations:@"InterfaceOrientation" context: nil ];
    [ UIView setAnimationDuration: [ application statusBarOrientationAnimationDuration ] ];
    self.view.transform = t;
    self.view.bounds = bounds;
    [ UIView commitAnimations ];

    [ application setStatusBarOrientation: orientation animated: YES ];    

End of UIApplication setStatusBarOrientation example article.