views:

1422

answers:

4

I am developing an application in iPhone. One view support orientation in portrait and landscape. I have two separate views for both orientation. This view has a UIToolbar at the top.

Problem is when I change view back from landscape to portrait, UIToolbar at the top disappears. I want toolbar to come back in its original position when it is rotated back to portrait.

This is what I am doing in my program.

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)
interfaceOrientation duration:(NSTimeInterval)duration {    
    if (interfaceOrientation == UIInterfaceOrientationPortrait)
    {
        self.view = self.portrait;
        self.view.transform = CGAffineTransformIdentity;
        self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(0));
        self.view.bounds = CGRectMake(0.0, 0.0, 300.0, 480.0);  
    }
    else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
    {
        self.view = self.landscape;
        self.view.transform = CGAffineTransformIdentity;
        self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(-90));
        self.view.bounds = CGRectMake(0.0, 0.0, 460.0, 320.0);
    }
    else if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        self.view = self.portrait;
        self.view.transform = CGAffineTransformIdentity;
        self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(180));
        self.view.bounds = CGRectMake(0.0, 0.0, 300.0, 480.0);
    }
    else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight)
    {
        self.view = self.landscape;
        self.view.transform = CGAffineTransformIdentity;
        self.view.transform = 
        CGAffineTransformMakeRotation(degreesToRadian(90));
        self.view.bounds = CGRectMake(0.0, 0.0, 460.0, 320.0);
    }
}

I don't know what am I missing here? Any help would be really appreciated.

+1  A: 

if you want just to make your view rotatable, is is enough to implement

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
    return YES;
}

in your viewCountroller class

Morion
Plus you'll want to set the anchors/stretch settings on the layout tab in Interface Builder.
Epsilon Prime
I have two views which I want to swap in portrait and landscape mode. Portrait view has a toolbar at the top which appears fine at first launch. But when I change the orientation and set it back to portrait toolbar disappear.
Leo
+1  A: 
self.view.bounds = CGRectMake(0.0, 0.0, 300.0, 480.0);

The problem here is that you are making the heigh 480. But it should be 460. So you are currently having 20px more on the height, which may be sending the toopbar somewhat off the screen.

Roshan Amadoru
20px will not cause disappeariong of the whole toolbar. and iPhone's screen size is 320x480.
Morion
yes. i agree. that's why i said "which may be sending the toopbar somewhat off the screen." . :)
Roshan Amadoru
Hi guys. Thanks for the replies. I tried setting less height but toolbar still invisible. Any idea?
Leo
A: 

Did you find a solution? I have the same issue.

Dave
A: 

In interface builder, select the toolbar, hit command-3; this brings up the ruler / alignment control menu.

Under the Autsizing section, by default the toolbar is only coupled with left-right and center. Not to the TOP. Select the "I" at the top. now the tool bar is docked relative to the top of the view and will not get pushed off during rotation.

I'm sure there is a code equivalent for this as well

zecougar