【问题标题】:Creating a UITabBar With a Normal UIViewController(Not UITabBarController)使用普通 UIViewController(不是 UITabBarController)创建 UITabBar
【发布时间】:2011-07-29 02:35:23
【问题描述】:

我有一个 UIViewController,我希望像下面的屏幕截图那样放置一个简单的 UITabBar:我不想使用 UITabBarController。事实上,我最初的设置使用了一个,而且很容易让它工作。

但是,我发现将 UITabBarControllers 放在导航控制器中是个坏主意,所以我不得不重构层次结构(所以请不要建议使用)。

给定一个 UIViewController,如何以编程方式在底部添加一个 UITabBar?我得到了我的 UITabBarDelegate 所有设置,以在单击其中一个选项卡栏时显示特定视图,但问题是当视图控制器加载时,没有显示选项卡栏!这是我的代码:

- (void) viewDidLoad {
        ...some initialization...
    CGRect frame = CGRectMake(0, 431, 320, 49);


    profileItem = [[UITabBarItem alloc] init];
    profileItem.image = icon;
    profileItem.title = @"Diet";    

    tabBar = [[UITabBar alloc] initWithFrame:frame];
    NSArray* items = [[NSArray alloc] initWithObjects:profileItem, nil];

    tabBar.items = items;
    tabBar.delegate = self;
    [tabBar setSelectedItem:[tabBar.items objectAtIndex:0]];

    [self.view addSubview:tabBar];
    [super viewDidLoad];
}

我在这里错过了什么?

编辑:啊,我发现它与框架有关......嗯......如果我在顶部有一个现有的导航标题,我应该使用什么 y 坐标?

【问题讨论】:

    标签: iphone objective-c


    【解决方案1】:

    您在编辑中提到您已经确定它与框架有关。您的问题可能源于您的视图在viewDidLoad 中的大小不能保证与最终显示时的大小相同。 UIKit 会调整视图的大小以适应在 viewWillAppear: 之前显示它的上下文。

    如果您想在viewDidLoad 中添加标签栏,那么您应该通过做一些数学运算将其放置在视图框架的底部:

    CGRect frame = CGRectMake(0, self.view.bounds.size.height - 49,
                              self.view.bounds.size.width, 49);
    

    然后设置一个适当的 autoresizingMask 以便它保持在底部,即使 UIKit 调整你的视图大小:

    tabBar.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
    

    【讨论】:

      猜你喜欢
      • 2012-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-16
      • 2018-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多