Search code examples
iphoneobjective-ciosuiinterfaceorientation

Fix uiview position in iphone sdk


In my app, I want to set the UIView orientation according to home button orientation of iPhone. I done it using following way:

/* I handled view orientation in shouldAutorotate method*/
    -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
            viewForBarButtons.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
        else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight)
            viewForBarButtons.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
        else if (interfaceOrientation == UIInterfaceOrientationPortrait)
            viewForBarButtons.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin;
        else if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
            viewForBarButtons.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }  

where viewForBarButtons is a UIView inside a viewController.

But if I set return Yes instead of return (interfaceOrientation == UIInterfaceOrientationPortrait); then it does not work.

How to resolve this issue. If anyone knows it, then please help me. Similar functionality is implemented in LifeCards app. Thanks in advance.


Solution

  • The above method is to decide whether to rotate your view to a particular orientation, when device orientation change.

    return (interfaceOrientation == UIInterfaceOrientationPortrait);
    

    specifies to support only Portrait orientation.

    return YES;
    

    would support all orientation.

    But if you are putting your viewcontroller in a TabBarController then it will rotate only if all viewcontrollers support that specific orientation.

    Instead of putting the above code to autoresize the object in willRotateToInterfaceOrientation.

    But you need know one thing, by specifing

    UIViewAutoresizingFlexibleLeftMargin

    You are just specifying that view can put as much as space want between object and left margin. But it will keep previous positionson other sides during orientation change, so you might need to phsically change the origin of the object (viewForBarButtons)

    Ok. I guess what you are saying is you want the viewForBarButtons beside the homeButton, and need to position/rotate its subviews accordingly.

    First register for devie rotaion or use didRotateInterfaceOrientation, to initate rotating subviews of viewForBarButtons.

    #define degreesToRadians(x) (M_PI * x / 180.0)
    

    Rotate subviews: replace self with subview object

    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    
        if (animated)
        {
            [UIView beginAnimations:nil context:NULL];
            [UIView setAnimationDuration:0.3];
        }
    
        if (orientation == UIInterfaceOrientationPortraitUpsideDown)
            self.transform = CGAffineTransformRotate(CGAffineTransformIdentity, degreesToRadians(180)); 
    
        else if (orientation == UIInterfaceOrientationLandscapeRight)
            self.transform = CGAffineTransformRotate(CGAffineTransformIdentity, degreesToRadians(90));  
    
        else if (orientation == UIInterfaceOrientationLandscapeLeft)
            self.transform = CGAffineTransformRotate(CGAffineTransformIdentity, degreesToRadians(-90));
        else 
            self.transform=CGAffineTransformIdentity;
    
        if (animated)
            [UIView commitAnimations];