0

在此处输入图像描述

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

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

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

RedColor 是 newView 白色是存在 viewController

4

1 回答 1

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 是对象名,这样试试。

于 2012-09-13T06:48:52.060 回答