ios - 强制方向改变有时不起作用

标签 ios objective-c ios8 uiinterfaceorientation

当在我的应用程序中按下某个按钮时, View 应该将方向从纵向更改为 景观。当用户回来时, View Controller 应该变回纵向。但 有时方向不会改变或使用了错误的 View 框架。

这是我的代码

-(void)btnSignClicked:(CustomSignButton *)btn {
    isSignButtonClicked = true;
    if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_0) {
        NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
        [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
    }
    else
    {
        [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:YES];
    }
    selectedWaiverId = btn.customTag;

    SignatureView *obj = [[SignatureView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) delegate:self]; // Most of time got size (568,320) but some time i got (320,568), Don't know why
    [self.view addSubview:obj];
}

#pragma mark - SIGNATUREVIEW DELEGATE
-(void)removeSignatureView:(SignatureView *)signatureView {
    isSignButtonClicked = false;

    if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_0)
    {
        NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
        [[UIDevice currentDevice] setValue:value forKey:@"orientation"]; // Some time not changed the orientation are view remaining in landscape 
    }
    else
    {
        [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];
    }

    [signatureView removeFromSuperview];
    signatureView = nil;

}
#pragma mark
#pragma mark - Rotate screen
-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    if (isSignButtonClicked == true)
    {
        return UIInterfaceOrientationMaskLandscapeRight|UIInterfaceOrientationMaskLandscape;
    }
    else
    {
        return UIInterfaceOrientationMaskPortrait;
    }
}
- (BOOL)shouldAutorotate
{
    return YES;
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if (isSignButtonClicked == true)
    {
        return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
    }
    else
    {
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }

}

更新

有时 viewWillTransitionToSize 方法没有被调用所以我也集成了这个通知

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

但有时这也行不通。

最佳答案

在 AppDelegate.m 文件或任何基础 Controller 文件中添加

 @implementation UINavigationController (Orientation)


- (UIInterfaceOrientationMask) supportedInterfaceOrientations
{

    return [(UIViewController*)[[self viewControllers] lastObject] supportedInterfaceOrientations];

}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [(UIViewController*)[[self viewControllers] lastObject] preferredInterfaceOrientationForPresentation];
}

- (BOOL) shouldAutorotate
{
    return [(UIViewController*)[[self viewControllers] lastObject] shouldAutorotate];
}

@end

现在将您的 ViewController 对象放入 UINavigationController 对象并推送 View Controller 。

例如。

UINavigationController *obj=[[UINavigationController  alloc] initWithRootViewController:_yourViewCtrlObj];

[self presentViewController:obj.....]; 

or
[self.navigationController pushViewController:obj animated:YES];

在所有 View Controller 中设置您想要的方向。

关于ios - 强制方向改变有时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35794712/

相关文章:

javascript - iPhone native 应用程序与 webkit 和 PhoneGap 之间的区别?

ios - 自 IOS 8 使用自动布局和 ScrollView 以来额外的顶部空白

objective-c - ios8 中无法更改 cell.textLabel 的字体

ios - 缓慢的 iOS 共享扩展

iphone - NIPhotoAlbumScrollView : unable to adjust image after device rotation

ios - 使用 Monotouch 和 iOS 从服务中提取数据 (JSON) 的理想方式?

objective-c - 使用 CAShapeLayer 对 CGMutablePathRef 进行动画处理

ios - 无法在 iOS8 上的 iTunes Store 中搜索

objective-c - 如何在不使用任何外部库的情况下压缩文件?

ios - 我可以将我的 NSURLConnection 与 UIProgressView 一起使用吗?