1

I have created an application to support all orientations.

Also I have created a table and search bar in viewDidLoad method (without using xib). I have also written following code to adjust the positioning of table view and search bar.

It works properly when I launch my application in portrait view and when I rotate to landscape view, it adjust the views perfectly.

But when I launch my application in landscape mode, views are not adjusted, instead they take positioning of portrait view. But again when I rotate in portrait and then landscape it works all right.

Also to ensure that correct orientation is captured, I have written NSLog inside this method, and it shows me the correct value.

Also to ensure that this method (adjustViewsForOrientation) is called explicitly, I have called the method in viewDidLoad as well.

[self adjustViewsForOrientation:self.interfaceOrientation];

Here is a rest of code snippet:

- (void) adjustViewsForOrientation:(UIInterfaceOrientation)orientation {
    if (orientation == UIInterfaceOrientationLandscapeLeft || 
            orientation == UIInterfaceOrientationLandscapeRight) {
        aTableView.frame = CGRectMake(705, 220, 320, 280);
        sBar.frame=CGRectMake(805, 0, 220, 40);
        NSLog(@"inside landscape");        
    }
    else if (orientation == UIInterfaceOrientationPortrait || 
                 orientation == UIInterfaceOrientationPortraitUpsideDown) {
        aTableView.frame = CGRectMake(450, 220, 320, 280);
        sBar.frame=CGRectMake(550,3,220,40);
    }
}

- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
                                 duration:(NSTimeInterval)duration {
    // [self adjustViewsForOrientation]
    [self adjustViewsForOrientation:toInterfaceOrientation];
    NSLog(@"landscap");
}
iHunter
  • 6,205
  • 3
  • 38
  • 56
user1051673
  • 91
  • 1
  • 1
  • 4
  • Can you print out NSLog(@"To Orientation: %d", toInterfaceOrientation); and show us the results? – Philippe Sabourin Jan 10 '12 at 15:08
  • A duplicate of - http://stackoverflow.com/questions/8775911/landscapeorientation-on-start-of-didload-method-in-objective-c/8775924#8775924 – rishi Jan 10 '12 at 16:02

1 Answers1

0

Try this approach, it may help. Register for UIDeviceOrientationDidChange in viewDidLoad method:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(adjustViewsForOrientation:) name:UIDeviceOrientationDidChangeNotification object:nil];

and also change -(void) adjustViewsForOrientation:(UIInterfaceOrientation)orientation

to -(void) adjustViewsForOrientation:(NSNotification *)notification

and obtain the new orientation by getting the orientation value

e.g. UIDeviceOrientation newDeviceOrientation = [notification orientation];

Sierra Alpha
  • 3,707
  • 4
  • 23
  • 36