uiviewautoresizingnone:回転後にサイズを変更します
-
24-10-2019 - |
質問
iPad用のシンプルな構造があり、viewcontrollerからのビューを含むAppDelegateを備えています。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
ClockVC *clockVC = [[ClockVC alloc]init];
clockVC.view.frame = CGRectMake(100, 100, clockVC.view.bounds.size.width, clockVC.view.bounds.size.height);
[self.window addSubview:clockVC.view];
[self.window makeKeyAndVisible];
return YES;
}
ClockVCには、このコードで定義された視聴者があります。
- (void)viewDidLoad {
[super viewDidLoad];
self.view.autoresizingMask = UIViewAutoresizingNone;
self.view.autoresizesSubviews = UIViewAutoresizingNone;
}
CLOCKVCの境界はIBによって定義され、アプリケーションでオーバーライドします。
CLOCKVCは、ワンステップの自動回転のための方法を実装しています: /
/ Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
-(void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
[UIView animateWithDuration:0.5
animations:^{self.view.alpha = 0;}
];
}
-(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
[UIView animateWithDuration:1
animations:^{self.view.alpha = 1;}
];
}
最初のロードでは、ページが正しく表示され、ClockVCビューが所定の位置にあります。 ClockVCビュー(Autoresizemaskがuiviewautoresizingnonに設定されている)を回転させると、画面全体を取得するためにサイズが変更されます。
どうして ??初期サイズをマンテインしたいのですが。
ここで私の問題のスクリーンショット。回転前:
回転後:
解決
このためにルートビューを使用しないでください。代わりに、ビューコントローラーのビューにサブビューを追加してください。 UIViewControllers
通常、コンテナのスペース全体を埋めるように設計されています(この場合のウィンドウ)。
たとえば、aを追加する場合 UIViewController
に UINavigationController
, 、ナビゲーションコントローラーは、ナビゲーションコントローラーのレイアウト(ナビゲーションバーが見えるかどうかなど)に従って、ビューコントローラーのビューをサイズする責任を引き継ぎます。 Uiwindowがどのように動作するかはわかりません(これはそれほど明確に文書化されていないと思います)が、SDKの多くの場所では、ビューコントローラーの親が子供の位置とサイズを担当します(UIPopoverController
, UITabBarController
et.al.)。