2

我无数次遇到这个问题,无法弄清楚如何解决它。我在一个 Xcode 项目中工作(空项目 - 没有 XIB 的!)。我将方向设置为横向:

在此处输入图像描述

但这一直在发生:

在此处输入图像描述

视图正在被切断。无论我做什么,它似乎都没有设置为正确的大小。由于某种原因,它使用纵向边界以横向显示视图。有谁知道如何解决这一问题?我还想将方向限制为仅横向。

更新 如果我将 1024 硬编码为宽度并将 768 硬编码为高度,则视图不会被截断。这显然是一个糟糕的解决方案,但我无法弄清楚。有没有人知道解决方案?

4

2 回答 2

1

检查您application:didFinishLaunchingWithOptions:在应用委托类中设置的 rooViewController。确保您在此视图控制器类中返回正确的允许方向,您将其对象设置为 rootViewController:

- (NSUInteger) supportedInterfaceOrientations{
return UIInterfaceOrientationMaskLandscape|UIInterfaceOrientationLandscapeRight;
}

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
return UIInterfaceOrientationIsLandscape(toInterfaceOrientation);
}

在您的应用程序委托中添加此功能:

- (NSUInteger) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{ 

 return UIInterfaceOrientationMaskLandscape|UIInterfaceOrientationLandscapeRight; 
 }
于 2013-05-27T01:55:20.843 回答
0

我有答案!我的一位朋友帮我解决了这个问题。

视图在出现之前不会定向,因此,如果您要向视图添加任何组件并希望它们遵循默认方向以外的方向,我怀疑这是纵向,您必须将这些组件添加到-(void)viewDidAppear:(BOOL)animated. 我正在调用从方法中向视图添加多个组件的viewDidLoad方法,但是,当调用该方法时,视图尚未出现,并且未设置方向。将我的初始化代码移动到该viewDidAppear方法中可以解决我的问题。

这是一个例子:

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidLoad];

    //Probably dont want to draw stuff in here, but if you did, it would adhere to the
    //correct orientation!
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.backgroundColor = [UIColor blueColor].CGColor;
    layer.anchorPoint = CGPointMake(0, 0);
    layer.bounds = CGRectMake(0, 0, self.view.bounds.size.width, 300);
    [self.view.layer addSublayer:layer];

    //Call methods from here
    [self initializeScrollView];
    [self addItemToScrollView];
    [self addGraphToView];
}  
于 2013-05-27T05:44:13.187 回答