プログラムで UITabBar を作成しています。ここにコードがあります、
UITabBarItem *item1 = [[UITabBarItem alloc] initWithTitle:@"Item 1" image:[UIImage imageNamed:@"dashboard"] tag:1];
UITabBarItem *item2 = [[UITabBarItem alloc] initWithTitle:@"Item 2" image:[UIImage imageNamed:@"documents"] tag:2];
UITabBarItem *item3 = [[UITabBarItem alloc] initWithTitle:@"Item 3" image:[UIImage imageNamed:@"mail"] tag:3];
UITabBarItem *item4 = [[UITabBarItem alloc] initWithTitle:@"Item 4" image:[UIImage imageNamed:@"packages"] tag:4];
NSArray *tabbaritems = [[NSArray alloc] initWithObjects:item1, item2, item3, item4, nil];
CGRect bounds = [[UIScreen mainScreen] bounds];
UITabBar *tbbar = [[UITabBar alloc] initWithFrame:CGRectMake(0, 411, bounds.size.width, 49)];
[tbbar setItems:tabbaritems];
[self.view addSubview:tbbar];
- (BOOL)shouldAutorotate
{
return YES;
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
//update UITabBar width
}
else {
//update UITabBar width
}
}
2 つの質問があります。ご覧のとおり、CGFloat y
値を 411 にハード コードしました。これは、ポートレート モードの 3.5 インチ画面では問題なく表示されます。しかし、ご想像のとおり問題は、4 インチのデバイスで実行すると、タブ バーが画面の下部に表示されることです。デバイスを回転すると(画面サイズにもかかわらず)、横向きモードではタブバーが押し下げられるため、画面にまったく表示されません。
- UITabBar の位置を動的に設定して、どの画面をどの回転で実行しても、常に画面の下部に固定して表示されるようにするにはどうすればよいですか?
もう一つの問題は幅です。bounds.size.width
UITabBar インスタンスが初めて作成されるときに、この行を使用して設定しました。
- 画面の幅全体に拡大するように、画面の回転時に更新するにはどうすればよいですか?