views:

905

answers:

1

I have an application with many views. I want only a couple of the views to be able to rotate to landscape when the device is rotated. I found out that I couldn't use (BOOL)shouldAutorotateToInterfaceOrientation because that would rotate every view in my app. I found a solution to this problem here on Stack Overflow but now I have another issue to deal with.

The view rotates when I turn the device but it still shows the view as if it were still in portrait mode (straight up and down). The top and bottom of the view is cut off. Is there a way to have the view rotate and also adjust its size to fit the new orientation? I also found this but wasn't able to get it to work.

Here's my code for that view:

@implementation businessBank
@synthesize webView, activityIndicator;


- (void)viewDidLoad {

    [super viewDidLoad];
    NSString *urlAddress = @"website_url";

    NSURL *url = [NSURL URLWithString:urlAddress];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

    [webView loadRequest:requestObj];
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(didRotate:)
                                                 name:UIDeviceOrientationDidChangeNotification
                                               object:nil];

}
- (void)didRotate:(NSNotification *)notification {

    UIDeviceOrientation orientation = [[notification object] orientation];

    if (orientation == UIDeviceOrientationLandscapeLeft) {
        [self.view setTransform:CGAffineTransformMakeRotation(M_PI / 2.0)];
    } else if (orientation == UIDeviceOrientationLandscapeRight) {
        [self.view setTransform:CGAffineTransformMakeRotation(M_PI / -2.0)];
    } else if (orientation == UIDeviceOrientationPortraitUpsideDown) {
        [self.view setTransform:CGAffineTransformMakeRotation(M_PI)];
    } else if (orientation == UIDeviceOrientationPortrait) {
        [self.view setTransform:CGAffineTransformMakeRotation(0.0)];
    }
}
+1  A: 

Unless you have a very basic app that stretches to match the orientation, auto-resizing is not going to work well for you anyways - you really need to create two separate views for the two orientations.

For help on autoresizing views, you can check this tutorial:

http://theappleblog.com/2009/04/08/iphone-dev-sessions-how-to-make-an-orientation-aware-clock/

To use the more robust method of switching views, this tutorial should help you out:

http://icodeblog.com/2008/08/03/iphone-programming-tutorial-transitioning-between-views/

Charles Boyung
The app shows the user a bunch of buttons that go to separate views. Each button opens a view that has the UIWebView in it. Seems like a fairly simple app that would be easy to have the webpage rotate, but it's taking longer than I expected. I basically want what Safari does on a couple views. I've looked at the Clock example before but that utilizes the shouldAutorotateToInterfaceOrientation code which rotates every view not just the ones I want.Creating separate views for portrait and landscape sounds like it could work but wouldn't that reset the website the user is currently on?
BCBomb47
You should be able to maintain the state in some way, either with a variable storing which web page is being viewed or by creating the UIWebView dynamically (instead of in the visual designer) and adding it to your views as needed (upon rotation).
Charles Boyung
The webviews are links to a page where the user can login and look at their bank acct. info. Even if I store the website they are on, it will most likely log them off again or (because the information is provided dynamically not on separate web pages) it won't show up where they were before. I really just need a way to rotate the webview to landscape exactly like shouldAutorotateToInterfaceOriention does... just not on every single view. Frustrating stuff.
BCBomb47
Then you should be able to create the UIWebView in your code and bind it to your views dynamically on the rotate event.
Charles Boyung