簡體   English   中英

強制改變方向有時不起作用

[英]Forced orientation change does not work sometimes

當我的應用程序中按下某個按鈕時,視圖應將方向從縱向更改為橫向。 當用戶回來時,視圖控制器應改回肖像。 但是有時方向不會改變,或者使用了錯誤的視框。

這是我的代碼

-(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文件或​​任何基本控制器文件

 @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對象中,然后推入視圖控制器。

例如

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

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

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

在所有視圖控制器中設置所需的方向。

如果您的應用程序使用UINavigationViewController,則為UINAvigationController創建一個自定義類,如下所示:

//CustomNavViewController.h

#import <UIKit/UIKit.h>
@interface CustomNavViewController : UINavigationController <UINavigationControllerDelegate>

@end

//CustomNavViewController.m

#import "CustomNavViewController.h"
@interface CustomNavViewController ()
@end

@implementation CustomNavViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)shouldAutorotate {
return [self.visibleViewController shouldAutorotate];
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return [self.visibleViewController supportedInterfaceOrientations];
}
@end

現在在您的AppDelegate中聲明一個屬性,例如:

//AppDelegate.h

 @property (assign, nonatomic) BOOL shouldRotate;

//AppDelegate.m

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if (self.shouldRotate) {
    return  UIInterfaceOrientationMaskLandscapeLeft|UIInterfaceOrientationMaskLandscapeRight;
}
return UIInterfaceOrientationMaskPortrait;
}

現在,您可以向ViewController調用方向方法,該方法需要固定方向,例如:

//YourViewController.m

-(BOOL)shouldAutorotate{
return NO;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
     return UIInterfaceOrientationMaskLandscape;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
return UIInterfaceOrientationLandscapeLeft;
}

現在,這是將技巧設置AppDelegate shouldRotate屬性設置為true和false以獲得所需的方向

如果您對ViewController使用默認表示,則

AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
[appDelegate setShouldRotate:true];// Remember first update the value then present the ViewController
[self presentViewController:yourViewController animated:YES completion:nil];

與解雇時相同

 AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
[appDelegate setShouldRotate:false];
[self dismissViewControllerAnimated:YES completion:nil];

如果您使用的是StoryBoards,則直接在Identity Inspector“自定義類”部分中添加CustomNavViewController

customNav 然后執行上述步驟。 希望它能正常工作

當您說“當用戶回來時,視圖控制器應該變回縱向”,這是否意味着用戶正在點擊導航控制器上的“后退”按鈕? 如果是這樣,我之前就看過這個問題,並在另一篇SO文章中發布了對我有用的解決方案: 答:在NavBar中返回時,在視圖上鎖定設備方向失敗 我記得過渡很艱難,但確實行得通。

不久前,我還寫了一篇博客文章 ,探討了有關視圖控制器方向鎖定的其他情況。

請參閱此鏈接 ,特別是,我認為您應該檢查視圖控制器的條件,以確保它們符合Apple的建議。

例如,檢查最頂部的全屏視圖控制器的supportedInterfaceOrientations方法

嘗試在此塊中添加所有旋轉更改代碼

dispatch_async(dispatch_get_main_queue(), 
{
     //changing orientation code + isSignButtonClicked = true (or false) 
});

您不需要使用shouldAutorotateshouldAutorotateToInterfaceOrientation

對於錯誤的視圖框架,即使您以編程方式創建視圖,也需要為在此viewController中使用的每個視圖使用自動布局約束

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM