iphone - 在没有私有(private) API 的情况下实现自定义 UITabBarController

标签 iphone ios objective-c cocoa-touch uitabbarcontroller

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

There are several problems with using the standard UITabBarController including:

It is too tall, especially in landscape mode

The height doesn't match the UIToolbar

It cannot be customized without using private APIs

尽管如此,我发现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。谢谢!

最佳答案

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

在以前版本的 iOS SDK 中,这有点冒险,因为您必须自己管理 viewWill/viewDid 方法的转发。

看看UIViewController类引用,实现容器 View Controller 部分,你会在那里找到你需要的一切:UIViewController Class Reference

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

希望这会有所帮助,

关于iphone - 在没有私有(private) API 的情况下实现自定义 UITabBarController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15201870/

相关文章:

ios - 需要一些帮助来配置我的项目架构

iphone - 如何在不影响字体大小和姓氏的情况下为标签设置粗体?

iphone - stretchableImageWithLeftCapWidth 使图像模糊

ios - 使用 ARKit 中的 ARCamera 信息定位/旋转平面节点

ios - 错误消息 : The Bundle ID you entered has already been used

ios - 尝试保存核心数据对象时出错

ios - 如果要支持iPhone 5,带有armv7的iOS 4.3是最低版本吗?

iphone - coredata 如何将默认 NSSet 更改为 NSMutableArray

iphone - 如何在多纹理对象之后绘制具有漫反射纹理的对象

ios - 应用程序关闭时 3D Touch UIapplicationShortcut 不起作用