如何为子视图到窗口的视图设置方向

时间:2012-09-13 06:12:19

标签: objective-c

enter image description here

我创建了一个UIView.I想要将视图子视图到appdelegate窗口

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

这有助于我查看窗口的视图。但是视图处于纵向模式。我需要视图处于横向模式。如何将视图设置为横向模式?我希望红色视图完全覆盖白色视图。我可以这样做吗?

RedColor是newView 白色存在viewController

1 个答案:

答案 0 :(得分:2)

如果我们向窗口添加任何对象然后我们想要改变方向,那么我们必须使用变换方法。

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

添加以上行

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;                             
}    

此处txt是对象名称,请尝试这种方式。