Aswathy Bose
Aswathy Bose

Reputation: 4289

how to set orientation for a view which is subviewed to window

enter image description here

I have created a UIView.I want to subview the view to the appdelegate window

  UIView *newView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 1048, 748)];
  AppDelegate *appdelegate = [[UIApplication sharedApplication]delegate];
  [appdelegate.window addSubview:newView];

This helps me to subview a view to the window.But the view is in portrait mode.I need the view to be in landscape mode .How can I set the view to the landscape mode? and I want the Red color view to completely cover the white view.how can I do so?

RedColor is the newView white is present viewController

Upvotes: 0

Views: 480

Answers (1)

Balu
Balu

Reputation: 8460

if we add any object to window and then we want to change the orientation then we must use transform methods.

#define DegreesToRadians(degrees) (degrees *M_PI /180)

add above line

CGAffineTransform newTransform;
UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
switch (orientation)
{
    case UIInterfaceOrientationPortraitUpsideDown:
        newTransform = CGAffineTransformMakeRotation(-DegreesToRadians(180));
        txt.transform = newTransform;
        txt.frame = CGRectMake(0, 0, 320, 480);
                break;
    case UIInterfaceOrientationLandscapeLeft:
        newTransform = CGAffineTransformMakeRotation(DegreesToRadians(-90));
        txt.transform = newTransform;
        txt.frame = CGRectMake(0, 0, 320, 480);

        break;
    case UIInterfaceOrientationLandscapeRight:
        newTransform = CGAffineTransformMakeRotation(DegreesToRadians(90));
        txt.transform = newTransform;
        txt.frame = CGRectMake(0, 0, 320, 480);


        break;
    default: 
        newTransform = CGAffineTransformMakeRotation(-DegreesToRadians(0));
        txt.transform = newTransform;
        txt.frame = CGRectMake(0, 0, 320, 480);

        break;                             
}    

here txt is object name,try in this way.

Upvotes: 2

Related Questions