このコードは、攻撃を意図したものではなく、非常に興味深いものです。あなたが何をしようとしているのかわかりません。どのような問題を解決しようとしていますか?CGAffineTransformをいじってみると、あまり注意しないと、説明したような奇妙な結果が確実に生成される可能性があります。
アプリが横向きと縦向きを正常にサポートしていることを確認したいだけの場合は、ViewControllerに実装できshouldAutorotateToInterfaceOrientation
ます。これを行うと、さまざまなコントロールのすべてがそれに応じて向きを変えます。
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Support all orientations on iPad
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
return YES;
// otherwise, for iPhone, support portrait and landscape left and right
return ((interfaceOrientation == UIInterfaceOrientationPortrait) ||
(interfaceOrientation == UIInterfaceOrientationLandscapeLeft) ||
(interfaceOrientation == UIInterfaceOrientationLandscapeRight));
}
しかし、私があなたがやろうとしていることを誤解している場合、つまり、横向きと縦向きの両方をサポートするだけではなく、より洗練された何かをしようとしている場合は、私に知らせてください。
このコードを最初にどこで入手したか覚えていないのでお詫びします(ただし、ここではSOで参照されています)が、以下を使用して横向きを強制することができます。
まず、shouldAutoRotateToInterfaceOrientationが次のようになっていることを確認します。
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) ||
(interfaceOrientation == UIInterfaceOrientationLandscapeRight))
return YES;
else
return NO;
}
次に、viewDidLoadに次のコードを追加します。
if (UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation]))
{
UIWindow *window = [[UIApplication sharedApplication] keyWindow];
UIView *view = [window.subviews objectAtIndex:0];
[view removeFromSuperview];
[window addSubview:view];
}
何らかの理由で、メインウィンドウからビューを削除してから再度追加すると、shouldAutorotateToInterfaceOrientationにクエリが実行され、方向が正しく設定されます。これがAppleが承認したアプローチではないことを考えると、おそらくそれを使用することを控えるべきですが、それは私にとってはうまくいきます。あなたのマイレージは異なる場合があります。しかし、そのSOの議論は、他の手法にも言及しています。