UIView的方向不正确

时间:2012-03-23 19:58:38

标签: objective-c ios cocoa-touch uiview cocos2d-iphone

我正在研究这款仅支持横向定位的cocos2D应用。这个UIView正在垂直显示,就像设备处于纵向方向一样。我有两个问题:

  • 为什么它垂直显示?
  • 是否可以在不手动旋转视图的情况下正确显示?

以下是视图控制器中的代码:

// this method is called from init
-(void) addLoadingView {    
        CGSize size = [[CCDirector sharedDirector] winSize];

    _blockerView = [[[UIView alloc] 
        initWithFrame: CGRectMake(0, 0, 280, 60)] autorelease];
    _blockerView.backgroundColor = [UIColor colorWithWhite: 0.0 alpha: 0.8];
    _blockerView.center = CGPointMake(size.width/2, size.height/2);
    _blockerView.alpha = 0.0;
    _blockerView.clipsToBounds = YES;
    if ([_blockerView.layer respondsToSelector: @selector(setCornerRadius:)])
        [(id) _blockerView.layer setCornerRadius: 10];

    _blockerLabel = [[[UILabel alloc] initWithFrame: CGRectMake(0, 5, _blockerView.bounds.size.width, 15)] autorelease];
    _blockerLabel.text = NSLocalizedString(@"Please Wait...", nil);
    _blockerLabel.backgroundColor = [UIColor clearColor];
    _blockerLabel.textColor = [UIColor whiteColor];
    _blockerLabel.textAlignment = UITextAlignmentCenter;
    _blockerLabel.font = [UIFont boldSystemFontOfSize: 15];
    [_blockerView addSubview: _blockerLabel];

    UIActivityIndicatorView  *spinner = [[[UIActivityIndicatorView alloc]
        initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhite] autorelease];

    spinner.center = CGPointMake(_blockerView.bounds.size.width / 2, _blockerView.bounds.size.height / 2 + 10);
    [_blockerView addSubview: spinner];
    [self.view addSubview: _blockerView];
    [spinner startAnimating];
}

-(void) showLoadingGraphics:(NSString*)textInView{
    _blockerView.alpha = 1.0;
    _blockerLabel.text = NSLocalizedString(textInView, nil);
}

更新

我发现如果将它添加到openGLView那就没关系了。

[[[CCDirector sharedDirector] openGLView] addSubview: _blockerView];

而不是

[self.view addSubview: _blockerView];

作品

我怀疑它当前的视图是以某种方式旋转的。

2 个答案:

答案 0 :(得分:1)

请检查视图控制器以获取名为shouldAutorotateToInterfaceOrientation的方法:你的回答应该是这样的:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
            interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

他们的标签栏控制器是否存在?如果是这样,那就是一个潜在的问题:标签栏的所有视图控制器都需要就方向达成一致。

答案 1 :(得分:0)

您是否在项目属性列表纵向方向上打开了?

<key>UISupportedInterfaceOrientations</key>
<array>
    <string>UIInterfaceOrientationPortrait</string>
    <string>UIInterfaceOrientationLandscapeRight</string>
    <string>UIInterfaceOrientationLandscapeLeft</string>
</array>