在没有私有API的情况下实现自定义UITabBarController [英] Implementing custom UITabBarController without private APIs

查看:72
本文介绍了在没有私有API的情况下实现自定义UITabBarController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

搜索了关于StackOverflow的几个问题后,我发现只有一个用于创建自定义UITabBar的主要项目称为 BCTabBarController .描述如下:

After searching through several question on StackOverflow I've found out that there is only 1 major project for creating custom UITabBar called BCTabBarController. The description to it says:

使用标准UITabBarController存在几个问题包括:

There are several problems with using the standard UITabBarController including:

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

It is too tall, especially in landscape mode

高度与UIToolbar不匹配

The height doesn't match the UIToolbar

不使用私有API便无法自定义

尽管如此,我还是找到了 GitHub上的这个奇怪项目,其中包含

Nevertheless, I've found this strange project on GitHub with the tutorial here that uses standard UITabBarController in its implementation with UIButtons for each tab and it's working (strangely enough, but it does).

我想知道,如果使用 UIButtons 而不是制表符来创建自定义 UITabBarController 是否错误,会导致什么结果?的实现看起来像这样:

I was wondering, if this is wrong to create your custom UITabBarController with UIButtons instead of tabs and what would it result into? The implementation of this looks like this:

- (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.谢谢!

In my project I will be using iOS 5.1 and up and no Storyboards or XIBs. Thanks!

推荐答案

从iOS 5.0开始,使用一行 UIButtons UITabBarController 不再是问题.>在屏幕底部.

Since iOS 5.0, it is no longer a problem to create your own UITabBarController using a line of UIButtons at the bottom of the screen.

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

In previous versions of the iOS SDK, it was a bit risky as you had to manage the forwarding of the viewWill/viewDidmethods by yourself.

查看 UIViewController 类参考的第实现容器视图控制器部分,您将找到所需的全部内容:

Have a look at the UIViewController Class Reference, section Implementing a Container View Controller, you will find all you need there : UIViewController Class Reference

还有一篇特色文章,确切地解释了您的需求:创建自定义容器视图控制器

There is also a featured article explaining exactly what you need : Creating Custom Container View Controllers

希望这会有所帮助,

这篇关于在没有私有API的情况下实现自定义UITabBarController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆