0

プログラムで 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 インチのデバイスで実行すると、タブ バーが画面の下部に表示されることです。デバイスを回転すると(画面サイズにもかかわらず)、横向きモードではタブバーが押し下げられるため、画面にまったく表示されません。

  1. UITabBar の位置を動的に設定して、どの画面をどの回転で実行しても、常に画面の下部に固定して表示されるようにするにはどうすればよいですか?

もう一つの問題は幅です。bounds.size.widthUITabBar インスタンスが初めて作成されるときに、この行を使用して設定しました。

  1. 画面の幅全体に拡大するように、画面の回転時に更新するにはどうすればよいですか?
4

1 に答える 1

1

2つの変数を使用して、yオフセットと幅を表します。フラグを使用して、縦向きと横向きのyオフセットと幅を入れ替えます。回転後にビューをリロードします。

そして、viewDidLoadのスーパービューから古いtbbarを削除します。

...
CGRect bounds = [[UIScreen mainScreen] bounds];
CGFloat tabbarAnchor = bounds.size.height;
CGFloat tabbarWidth = bounds.size.width;
if (_flag == 1) {
    tabbarWidth = bounds.size.height;
    tabbarAnchor = bounds.size.width;
}
UITabBar *tbbar = [[UITabBar alloc] initWithFrame:CGRectMake(0, tabbarAnchor-69, tabbarWidth, 49)];
[tbbar setItems:tabbaritems];
[self.view addSubview:tbbar];
...

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
        //update UITabBar width
        _flag = 1;
        [self viewDidLoad];
    }
    else {
        //update UITabBar width
        _flag = 0;
        [self viewDidLoad];
    }
}

それでも、すでに回転を実装しているUITabBarControllerを使用することをお勧めします。

于 2013-03-02T02:29:09.777 に答える