【问题标题】:ViewController orientation changeViewController 方向改变
【发布时间】:2011-11-08 11:03:38
【问题描述】:

当 iPhone 将方向更改为横向时,我正在推送 ViewController,并且在更改 ViewController 的方向时遇到问题。

我使用了那个代码:

    - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
Storage *strg = [Storage sharedStorage];

if ([strg.orient intValue] == 2) 
{
    [[UIApplication sharedApplication] setStatusBarOrientation:
     UIInterfaceOrientationLandscapeRight];

    UIScreen *screen = [UIScreen mainScreen];
    CGFloat screenWidth = screen.bounds.size.width;
    CGFloat screenHeight = screen.bounds.size.height;
    UIView *navView = [[self navigationController] view];
    navView.bounds = CGRectMake(0, 0, screenHeight, screenWidth);
    navView.transform = CGAffineTransformIdentity;
    navView.transform = CGAffineTransformMakeRotation(1.57079633);
    navView.center = CGPointMake(screenWidth/2.0, screenHeight/2.0);
    [UIView commitAnimations];
}
if ([strg.orient intValue] == 1)
{
    [[UIApplication sharedApplication] setStatusBarOrientation:
     UIInterfaceOrientationLandscapeLeft];

    UIScreen *screen = [UIScreen mainScreen];
    CGFloat screenWidth = screen.bounds.size.width;
    CGFloat screenHeight = screen.bounds.size.height;
    UIView *navView = [[self navigationController] view];
    navView.bounds = CGRectMake(0, 0, screenHeight, screenWidth);
    navView.transform = CGAffineTransformIdentity;
    navView.transform = CGAffineTransformMakeRotation(4.71238898);
    navView.center = CGPointMake(screenWidth/2.0, screenHeight/2.0);
    [UIView commitAnimations];
}
}

结果不一致;有时它进入正确的方向,有时它是颠倒的。

当我从 LandcapeRight 到 LandscapeLeft 海峡时(反之亦然),它工作正常,只有当我进入纵向模式时才会出现问题。

任何想法我做错了什么?

【问题讨论】:

    标签: iphone navigation orientation


    【解决方案1】:

    如果您确实在响应设备方向更改,那么您可能不应该使用 setStatusBarOrientation。我认为您最好使用 shouldAutorotateToInterfaceOrientation 和 deviceDidRotateSelector 通知让您的视图控制器旋转到支持的方向。

    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(deviceDidRotateSelector:) name: UIDeviceOrientationDidChangeNotification object: nil];
    
    -(void)deviceDidRotateSelector:(NSNotification*) notification {
    // respond to rotation here
    }
    
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    //Return YES for supported orientations
    return YES;
    }
    

    【讨论】:

    • 我试过这个。行不通。不知道为什么,我看到很多其他人在结合方向更改和导航时遇到了这个问题。
    • 嗯,这对我有用。我倾向于遇到的唯一问题是确保我响应 UIDeviceOrientationUnknown 以及其他方向。但是,我不使用笔尖。我编写所有代码,这可能就是我没有问题的原因。我仍然认为你应该避免使用 setStatusBarOrientation 因为这实际上并没有改变设备方向 - 如果你问设备它的设备方向是什么,它仍然会说它的真实设备方向是什么,即使你已经将状态栏方向设置为别的东西。
    • 我不知道我之前尝试过这个时错过了什么,但现在它可以工作了!谢谢!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多