【问题标题】:Update UITabBar width on screen rotation在屏幕旋转时更新 UITabBar 宽度
【发布时间】:2013-02-15 13:57:40
【问题描述】:

我正在以编程方式创建一个 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 的位置 它以任何旋转方式运行的屏幕,它总是看起来固定在 屏幕底部?

另一个问题是宽度。当第一次创建 UITabBar 实例时,我已经使用这条线 bounds.size.width 设置它。

  1. 如何在屏幕旋转时更新它以使其扩展填充 整个屏幕的宽度?

【问题讨论】:

    标签: ios objective-c width uitabbar screen-rotation


    【解决方案1】:

    使用两个变量来代表 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。

    【讨论】:

    • 嗨!非常感谢您的回复。当方向改变时,它在定位 TabBar 方面效果很好。但是我遇到了一个小问题。说,我将设备旋转到横向模式。 TabBar 位置很好。然后,当我将其旋转回纵向视图时,之前在横向模式下创建的 TabBar 仍然显示为 this。我怎样才能摆脱它?再次感谢。 :)
    • 我不得不使用UITabBar 而不是UITabBarController,因为我正在实现自定义UITabBar
    • 由于每次旋转都会在self.view中添加一个新的subview,所以在添加新的tbbar之前需要清除之前添加的tbbar。代码可能是:for (UIView *view in self.view.subviews) { [view removeFromSuperview]; }
    猜你喜欢
    • 2011-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多