【问题标题】:"[[UIApplication sharedApplication] statusBarOrientation]; gives wrong value in the first call"“[[UIApplication sharedApplication] statusBarOrientation]; 在第一次调用中给出错误的值”
【发布时间】:2013-02-12 17:08:53
【问题描述】:

我正在开发一个 iPad 应用程序,该应用程序从初始屏幕开始,然后移动到登录屏幕。我的应用程序应该支持所有方向,以及 iOS 4.3 及更高版本。为此,我在 plist 中添加了四个方向,并在应用程序委托中添加了以下代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    [self.window makeKeyAndVisible]; 
    // Override point for customization after application launch
    SplashViewController *aController = [[SplashViewController alloc] initWithNibName:@"SplashView" bundle:nil];
    self.mainViewController = aController;
    [aController release];

    mainViewController.view.frame = [UIScreen mainScreen].bounds;// no effect
    [window setFrame:[[UIScreen mainScreen] bounds]]; //no effect
    //[window addSubview:[mainViewController view]];
    [window setRootViewController:mainViewController];

    return YES;
}

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

    return UIInterfaceOrientationMaskAll;
}

在初始屏幕中

- (void) loadView {
    [super loadView];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged) name:@"UIDeviceOrientationDidChangeNotification" object:nil];

}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        return YES;
}
- (BOOL)shouldAutorotate{
    return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}
- (void) orientationChanged
{
    UIInterfaceOrientation interfaceOrientation =[[UIApplication sharedApplication] statusBarOrientation];
    if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
            NSLog(@"Portrait");
    else
            NSLog(@"Landscape");

}

在这里我得到了正确的方向,但是当旋转时,我得到一个倒置的结果,我得到横向的纵向和纵向的横向。我试图像这样转换代码:

- (void) orientationChanged
{
    UIInterfaceOrientation interfaceOrientation =[[UIApplication sharedApplication] statusBarOrientation];
    if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
            NSLog(@"Landscape");
    else
            NSLog(@"Portrait");

}

我得到的第一个结果是错误的,之后结果是正确的。你能帮我吗? 请注意,我已经放置了一些 uielement 进行测试,并且测试结果相同。 谢谢

【问题讨论】:

    标签: ipad orientation


    【解决方案1】:

    经过两天的搜索和测试,我设法解决了这个问题,论坛上没有一个完整的答案,我是这样解决的:

    //this for the orientation
    - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
        [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
        [self orientationChanged];
    }
    
    // here is the tricky thing, when startup you should have a special function for the orientation other than the orientationchanged method, and must be called here in viewDidAppear, otherwise it won't work
    -(void)viewDidAppear:(BOOL)animated {
        [super viewDidAppear:animated];
        [self StartUpOrientation];
    }
    #pragma mark -
    #pragma mark Orientation Methods
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        return YES;
    }
    
    - (BOOL)shouldAutorotate {
        return YES;
    }
    
    -(NSUInteger)supportedInterfaceOrientations {
        return UIInterfaceOrientationMaskAll;
    
    }
    
    - (void) orientationChanged {
        UIInterfaceOrientation interfaceOrientation =[[UIApplication sharedApplication] statusBarOrientation];
        if (interfaceOrientation != UIInterfaceOrientationLandscapeLeft && interfaceOrientation != UIInterfaceOrientationLandscapeRight) {
            [self.view setBounds:CGRectMake(0, 0, 1024, 748)];
            // your code here
        }
        else {
            [self.view setBounds:CGRectMake(0, 0,768,1004)];
            //your code here
        }
    }
    
    - (void) StartUpOrientation {
        UIInterfaceOrientation interfaceOrientation =[[UIApplication sharedApplication] statusBarOrientation];
    if (interfaceOrientation != UIInterfaceOrientationLandscapeLeft && interfaceOrientation != UIInterfaceOrientationLandscapeRight) {
            [self.view setBounds:CGRectMake(0, 0,768,1004)];
            // your code here
        }
    else {
            [self.view setBounds:CGRectMake(0, 0, 1024, 748)];
            // your code here
        }
    }
    

    希望有一天能对某人有所帮助

    【讨论】:

    • 但从一个视图导航到另一个视图时仍然存在同样的问题
    猜你喜欢
    • 1970-01-01
    • 2023-03-23
    • 2011-03-22
    • 1970-01-01
    • 1970-01-01
    • 2011-05-15
    • 1970-01-01
    • 2014-05-28
    • 1970-01-01
    相关资源
    最近更新 更多