【问题标题】:Implementing custom UITabBarController without private APIs在没有私有 API 的情况下实现自定义 UITabBarController
【发布时间】:2013-02-18 13:25:45
【问题描述】:

在 StackOverflow 上搜索了几个问题后,我发现只有一个名为 BCTabBarController 的用于创建自定义 UITabBar 的主要项目。它的描述是:

使用标准的 UITabBarController 有几个问题 包括:

它太高了,尤其是在横向模式下

高度与 UIToolbar 不匹配

不能在不使用私有 API 的情况下进行自定义

尽管如此,我发现this strange project on GitHubtutorial here 在其实现中使用标准UITabBarController,每个选项卡都使用UIButtons,它正在工作(很奇怪,但确实如此)。

我想知道,如果使用UIButtons 而不是制表符创建自定义UITabBarController 是错误的,它会导致什么结果?其实现如下所示:

- (void)viewDidAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [self hideTabBar];
    [self addCustomElements];
}

- (void)hideTabBar
{
    for(UIView *view in self.view.subviews)
    {
        if([view isKindOfClass:[UITabBar class]])
        {
            view.hidden = YES;
            break;
        }
    }
}

-(void)addCustomElements
{
    // Initialise our two images
    UIImage *btnImage = [UIImage imageNamed:@"NavBar_01.png"];
    UIImage *btnImageSelected = [UIImage imageNamed:@"NavBar_01_s.png"];

    self.btn1 = [UIButton buttonWithType:UIButtonTypeCustom]; //Setup the button
    btn1.frame = CGRectMake(0, 430, 80, 50); // Set the frame (size and position) of the button)
    [btn1 setBackgroundImage:btnImage forState:UIControlStateNormal]; // Set the image for the normal state of the button
    [btn1 setBackgroundImage:btnImageSelected forState:UIControlStateSelected]; // Set the image for the selected state of the button
    btn1.backgroundColor = [UIColor yellowColor];
    [btn1 setTag:0]; // Assign the button a "tag" so when our "click" event is called we know which button was pressed.
    [btn1 setSelected:true]; // Set this button as selected (we will select the others to false as we only want Tab 1 to be selected initially

在我的项目中,我将使用 iOS 5.1 及更高版本,并且不使用 Storyboard 或 XIB。谢谢!

【问题讨论】:

    标签: iphone ios objective-c cocoa-touch uitabbarcontroller


    【解决方案1】:

    从 iOS 5.0 开始,使用屏幕底部的一行 UIButtons 创建自己的 UITabBarController 不再是问题。

    在之前的 iOS SDK 版本中,viewWill/viewDidmethods 的转发必须自己管理,有点冒险。

    查看UIViewController 类参考,实现容器视图控制器部分,您会在那里找到所需的一切:UIViewController Class Reference

    还有一篇专题文章准确解释了您的需求:Creating Custom Container View Controllers

    希望这会有所帮助,

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-27
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多