1

I have a problem with rotating a subview when the device orientation is changed. My situation is:

I have root view controller that is declared in

//in AppDelegate.h
@property MainViewController * myMainViewController;
//in AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.myMainViewController = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
    CGRect initFrame = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height);
    self.window = [[UIWindow alloc] initWithFrame:initFrame];
    // Override point for customization after application launch.
    [self.window makeKeyAndVisible];
    self.window.rootViewController = self.myMainViewController;   
    return YES;
}

In root view controller, I add a subview programmatically; the initial device orientation is portrait, and the size of my subview is set with frame property value (0, 0, 300, 600).

//in MainViewController.h
@property ContentViewController * mySubViewController;
//in MainViewController.m
-(void)viewDidAppear:(BOOL)animated
{   
    self.mySubViewController = [[ContentViewController alloc] initWithNibName:@"ContentViewController" bundle:nil];
    [self.mySubViewController.view setFrame:CGRectMake(0, 0, 300, 600)];
    [self.view addSubview:self.mySubViewController.view];
}

When then device orientation changes, I would like to resize my subview according to the new device orientation. If this is landscape, then I want to set my subview size to (0, 0, 600, 300).

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    if(fromInterfaceOrientation==UIInterfaceOrientationLandscapeLeft || fromInterfaceOrientation==UIInterfaceOrientationLandscapeRight)
    {       
        self.mySubViewController.view.frame = CGRectMake(0, 0, 300, 600);       
    }
    else
    {
        self.mySubViewController.view.frame = CGRectMake(0, 0, 600, 300); 
    }
}

But, the result is not what I expect. The size of my subview is a rectangle with a size of about (0, 0, 300, 300). What is the problem?

Frazz
  • 2,995
  • 2
  • 19
  • 33
phuochuynh
  • 11
  • 2

1 Answers1

-1

Query your interface orientation again, instead of predicting what would be the current orientation based on fromOrientation as below,

-(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    if (UIInterfaceOrientationIsPortrait(orientation))
    {
        //Your portrait
    }
    else
    {
        //Your Landscape.
    }
}
nprd
  • 1,942
  • 1
  • 13
  • 16
  • It’s not a mater about device orientation, I have tried as follow -(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { self.mySubViewController.view.frame = CGRectMake(0, 0, 600, 300); } But, the output result is still not correct. The size of my subview is still about (0, 0, 300, 300) when device orientation change to landscape (my expectation is (0, 0, 600, 300)). Thanks for your reply. – phuochuynh Sep 09 '14 at 15:10
  • are you using auto layout? – nprd Sep 09 '14 at 15:42
  • Dear Naveen Prasad R, I think auto layout does not suit for me in this case. Because I want to change sub view position/size in some circumstances. It is very clear, I have set frame of my sub view, but why my sub view size/position is still not set correctly. I appreciate you very much for any suggestion. – phuochuynh Sep 11 '14 at 13:02