【问题标题】:How to subclass a UITabBarController and replace its UITabBar view?如何继承 UITabBarController 并替换其 UITabBar 视图?
【发布时间】:2014-11-13 04:45:04
【问题描述】:

我需要将UITabBarController 子类化,这样我就可以用我希望在界面生成器中生成的自定义视图完全替换 UITabBar 视图。我试过了,但没有成功。

首先,我创建了一个UITabBarController 的子类和一个xib。我删除了 xib 中的默认视图,并用一个只有 60 像素高(我的标签栏大小)的新视图替换了它。我将必要的按钮拖到它上面,并像这样配置 .h 文件:

@interface ToolbarViewController : UITabBarController

@property (strong, nonatomic) IBOutlet UIView *tabBarView;

@property (strong, nonatomic) IBOutlet UIButton* firstButton;
@property (strong, nonatomic) IBOutlet UIButton* secondButton;

@end

我的 xib 看起来像这样:

当我启动应用程序时,我看到底部有一个空白区域用于标签栏,但我没有看到实际的标签栏:

更新:我意识到我实际上并没有在 .m 文件中启动 xib 文件。任何人都知道我该如何正确地做到这一点?

【问题讨论】:

  • 您是否为视图和按钮添加了适当的约束?
  • 这不是我的问题。我刚刚意识到我需要以某种方式在我的 viewDidLoad 方法中添加 xib 视图。我只是不确定如何正确执行此操作。

标签: ios objective-c iphone


【解决方案1】:

有多种不同的解决方案可以将一组自定义按钮添加到自定义标签栏控制器子类。几年前我按照本指南完成了它:http://idevrecipes.com/2010/12/16/raised-center-tab-bar-button/

这个想法是在您的 UITabBarController 子类的标签栏上添加一个自定义 UIView。 CustomTabBarController 类不必有 xib。相反,我有一个 UIView 的子类,它可以以编程方式布局,也可以使用 xib 为 UIView 创建。这是我的 CustomTabBarView 类的头文件:

@interface CustomTabBarView : UIView
{
    CALayer *opaqueBackground;
    UIImageView *tabBG;

    IBOutlet UIButton *button0;
    IBOutlet UIButton *button1;
    IBOutlet UIButton *button2;
    NSArray *tabButtons;

    int lastTab;
}
@property (nonatomic, weak) id delegate;

-(IBAction)didClickButton:(id)sender;

您可以将所需的按钮连接到 xib 文件中的 button0、button1、button2 等,或者在视图的 init 上以编程方式进行。请注意,这是 UIView 子类。

在 CustomTabBarView.m 中:

-(IBAction)didClickButton:(id)sender {
    int pos = ((UIButton *)sender).tag;
    // or some other way to figure out which tab button was pressed

    [self.delegate setSelectedIndex:pos]; // switch to the correct view
}

然后在您的 CustomTabBarController 类中:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    tabView = [[CustomTabBarView alloc] init];
    tabView.delegate = self;
    tabView.frame = CGRectMake(0, self.view.frame.size.height-60, 320, 60);
    [self.view addSubview:tabView];
}

当在 CustomTabBarView 中单击按钮时,它将调用其委托函数,在本例中为 CustomTabBarController。该调用与单击实际选项卡栏中的选项卡按钮的功能相同,因此如果您像普通的 UITabBarController 一样正确设置了 CustomTabBarController,它将跳转到选项卡。

哦,稍微单独说明一下,添加自定义 xib 作为 UIView 子类的接口的正确方法:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil];
        UIView *mainView = [subviewArray objectAtIndex:0];

        //Just in case the size is different (you may or may not want this)
        mainView.frame = self.bounds;

        [self addSubview:mainView];
    }
    return self;
}

在 xib 文件中,确保 File's Owner 将其自定义类设置为 CustomTabBarView。

【讨论】:

    猜你喜欢
    • 2018-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-18
    • 2014-02-22
    相关资源
    最近更新 更多