vishiphone
vishiphone

Reputation: 750

Navigation Bar issue in orientation

I searched every where but not find the solution of this I am New in iphone.In every where I got the set the height of navigation or my view is not rotating in orientation like issue.my view is rotating but my navigation bar is on same position please some one help me if You have solution.Thanks I have show my some code in down which I used for Orientation.when I tap on my tab bar my simulator is automatic rotate and I want tab bar also rotate but using this code only simulator is rotate not tab bar and navigation bar and sorry for my bad english.

CGAffineTransform transform = CGAffineTransformIdentity;

switch ([[UIApplication sharedApplication] statusBarOrientation]) 
{

    case UIInterfaceOrientationPortrait:
        transform = CGAffineTransformMakeRotation(M_PI_2);
        break;

    default:
        break;
}

[[UIApplication sharedApplication]setStatusBarOrientation:UIInterfaceOrientationPortrait];

[UIView animateWithDuration:0.2f animations:^ {

    [self.navigationController.view setTransform:transform];

}];

[self.view setFrame:CGRectMake(0, 0, 320, 480)];
[self.view setNeedsLayout];

Upvotes: 1

Views: 2134

Answers (1)

Rob
Rob

Reputation: 437632

This code is, no offense intended, very curious. I'm not sure what you are trying to do. What problem are you trying to solve? Playing around with CGAffineTransform's can definitely generate strange results like what you describe if you're not very careful.

If you just want to make sure that your app successfully supports landscape and portrait orientations, you can implement shouldAutorotateToInterfaceOrientation in your view controller. When you do this, all of the various controls will reorient themselves accordingly.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Support all orientations on iPad
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
        return YES;

    // otherwise, for iPhone, support portrait and landscape left and right

    return ((interfaceOrientation == UIInterfaceOrientationPortrait) ||
        (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) ||
        (interfaceOrientation == UIInterfaceOrientationLandscapeRight));
}

But if I have misunderstood what you're trying to do, i.e., you're trying to do something more sophisticated than just supporting both landscape and portrait orientation, let me know.


I apologize because I don't remember where I originally got this code (but it's referenced in SO here), but the following can be used to force landscape orientation:

First, make sure that your shouldAutoRotateToInterfaceOrientation should read as follows:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) ||
        (interfaceOrientation == UIInterfaceOrientationLandscapeRight))
        return YES;
    else
        return NO;
}

Second, in viewDidLoad, add the following code:

if (UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation]))
{
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    UIView *view = [window.subviews objectAtIndex:0];
    [view removeFromSuperview];
    [window addSubview:view];
}

For some reason, removing the view from the main window and then re-adding it forces it to query shouldAutorotateToInterfaceOrientation and set the orientation correctly. Given that this isn't an Apple approved approach, maybe one should refrain from using it, but it works for me. Your mileage may vary. But that SO discussion also refers to other techniques, too.

Upvotes: 1

Related Questions