viewdidload でプログラムによる方向の設定が機能しない
-
23-12-2019 - |
質問
ホーム画面が縦モードでのみ表示されるiPad用のアプリを作成しました。そこで私はviewdidloadで以下のコードを使用しました。
[[UIDevice currentDevice] setOrientation:UIDeviceOrientationPortrait];
しかし機能しません。私はiOS 7とXcode 5を使用しています。アプリを横向きモードで開くと、自動的に縦向きモードに切り替わるはずです。しかし、私は次のようになります:
しかし、それは次のようになるはずです:
誰かこの問題を解決するのを手伝ってくれませんか。前もって感謝します。
解決
[[UIDevice currentDevice] setOrientation:UIDeviceOrientationPortrait];
.
この方法は推奨されていません。この方法はもう使用できません。
https://stackoverflow.com/a/12813644/1405008
上のリンクは、UIViewController
の縦軸のみのモードを提供する方法について詳しく説明します。
NavigationViewController
にプッシュした場合は、これを試してください。
他のヒント
次のコードは、PortraitからLandscapeへのナビゲーションコントラルビューをプログラムで変換しようとしています。
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
self.navigationController.view.transform = CGAffineTransformIdentity;
self.navigationController.view.transform = CGAffineTransformMakeRotation(M_PI*(90)/180.0);
self.navigationController.view.bounds = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width);
[UIView commitAnimations];
self.view.frame = self.view.frame; //This is necessary
self.wantsFullScreenLayout = YES;
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscapeRight;
}
.
あなたに他のアイデアを提供したいと思っています。
ViewDidLoad
にこのコードをコピーするUIInterfaceOrientation orientation = (UIInterfaceOrientation)[[UIDevice currentDevice] orientation];
if(orientation == UIInterfaceOrientationPortrait)
{
isLandscapeMode = NO;
inLandscapeRight = NO;
inLandscapeLeft = NO;
}
else if(orientation == UIInterfaceOrientationLandscapeRight)
{
isLandscapeMode = YES;
inLandscapeRight = YES;
inLandscapeLeft = NO;
} else if(orientation == UIInterfaceOrientationLandscapeLeft)
{
isLandscapeMode = YES;
inLandscapeRight = NO;
inLandscapeLeft = YES;
}
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft)
{
isLandscapeMode = YES;
inLandscapeLeft = YES;
inLandscapeRight = NO;
frameForProfile = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
}
else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
isLandscapeMode = YES;
inLandscapeLeft = NO;
inLandscapeRight = YES;
frameForProfile = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
}
else
{
isLandscapeMode = NO;
inLandscapeLeft = NO;
inLandscapeRight = NO;
}
}
.
あなたの要求に応じてboolを設定する
あなたからのスクリーンショット私はあなたのアプリがタブベースであることを見ることができます。私は同じ問題に直面しました。私はそれを解決しました。私はあなたとほぼ同じ質問をしました。それから数時間の読み取りの後、私は問題を解決しました。my href="https://stackoverflow.com/questions/21747851 / fixed-orientation-for-some-view-controllers">質問と回答
。SO には同様の質問と回答がたくさんありますが、許可する必要があったため、どういうわけかどれも私にとっては役に立ちませんでした iPhoneではポートレートモードのみ, 、 そして iPadでは横向きモードのみ, ので、私の解決策を共有します。
デバイスの向きに関連するすべてのメソッドを削除します (
shouldAutoRotate, supportedInterfaceOrientations, preferredInterfaceOrientations
, 、など)互いに競合している可能性があるためプロジェクト設定で 4 つのモードをすべて有効にします。縦向き、逆さま、横向き左、横向き右
デバイスの種類 (iPhone または iPad) を識別する方法を選択します。使っています
UIScreen.mainScreen().bounds.height which
理想的ではありませんが、私のニーズには合っていますAppDelegateに以下を配置します
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask { if UIScreen.mainScreen().bounds.height < 760 { //iPhone UIApplication.sharedApplication().setStatusBarOrientation(.Portrait, animated: false); return UIInterfaceOrientationMask.Portrait; } else { UIApplication.sharedApplication().setStatusBarOrientation(.LandscapeLeft, animated: false); return UIInterfaceOrientationMask.Landscape; } }
iOS9.1を実行しているすべてのiPhoneおよびiPadシミュレータでXcode 7.1.1でテスト済み