2016-03-04 7 views
6

アプリで特定のボタンが押されると、ビューの向きが縦から横に に変更されます。ユーザーが戻ったとき、ビューコントローラーはポートレートに戻る必要があります。しかし、 方向が変わらない、または間違ったビューフレームが使用されることがあります。私も強制的な向きの変更が動作しない場合があります

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

この通知を統合しかし、時にはこれも動作しませんので

は、ここに私のコード

-(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); 
    } 

} 

UPDATE

は時々viewWillTransitionToSizeメソッドが呼び出されませんされています。

+1

オリエンテーションを変更する方法は文書化されているのか、それともハックですか? – Sulthan

+1

これに反対することは、確かに「信用できる、公的な情報源」が推奨します。 – beyowulf

+0

文書にあります。 –

答えて

0

このlinkは、特に、私は彼らが

例えばアップルの推奨事項を満たしていることを、あなたはあなたのビューコントローラの状態を確認するべきだと思います参照してください。一番上のフルスクリーンビューコントローラのsupportedInterfaceOrientations方法をご確認

+0

私はすでにすべての条件を入れました。 –

0

てみてください。このブロック

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

内のすべてのローテーション変更コードを追加するあなたは必要shouldAutorotateshouldAutorotateToInterfaceOrientationを使用していません。

は、ビューのフレームが間違って取得するためとして、あなたはそれぞれの制約を自動レイアウトを使用する必要があり、すべてはあなたがprogrammativally

+0

非常に古いコードなので、autolayoutは実装されていません。私はすでにコードでディスパッチを使用しましたが、成功を得ていません。 –

+0

私も古いコードを持っていましたが、idをautolayoutに変更してフレームを正しく取得する必要がありました。あまり痛みはありません。コード内でフレームを設定する場所をプログラムで自動レイアウトすることができます。また、info.plistのViewControllerBasedOrientationをYESに追加する必要があります – shinoys222

+0

info.plistの "ViewControllerBasedOrientation"のオプションはありません –

2

AppDelegate.mファイルまたは任意のベースコントローラに追加のビューを作成している場合でも、あなたがこのViewControllerをで使用しているビューファイル

@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を押します。

EX。

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

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

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

すべてのビューコントローラで希望の方向を設定します。

+0

私はすでにこれをすべて行いましたが、私は新しいVCを使っていないのでVCを却下しますビュー –

1

あなたのアプリは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。メートル

- (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のプロパティであり、目的の向きでfalseの場合

ViewControllerにデフォルトのプレゼンテーションを使用している場合は

あなたはストーリーボードを使用している場合は、アイデンティティインスペクターカスタムクラスのセクション

customNav そして、それは、上記の手順を実行した後に直接CustomNavViewControllerを追加

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

を閉じたときと同じ

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]; 

。うまくいきたいです

+0

私は既にVCを使用していないので、私はVCのみを表示します –

1

「ユーザーが戻ったときにビューコントローラをポートレートに戻す」と言うと、ユーザーがナビゲーションコントローラの戻るボタンを押しているということですか?もしそうなら、私はこの問題を以前に見て、別のSOの投稿で私のために働く解決策を投稿しました:A: Locking device orientation on a view fails when going back in NavBar。私は移行が荒いことを覚えていますが、効果はありました

また、私はblog postを書いていましたが、これは、ビューコントローラの向きを固定するロッキングについて、他のいくつかの状況を見ています。

関連する問題