• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 如何继承 UITabBarController 并替换其 UITabBar View ?

[复制链接]
菜鸟教程小白 发表于 2022-12-13 09:02:28 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

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

@interface ToolbarViewController : UITabBarController

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

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

@end

我的 xib 看起来像这样:

Xcode screenshot of the tabbar xib

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

App screenshot of the tabbar at runtime

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



Best Answer-推荐答案


有多种不同的解决方案可以将一组自定义按钮添加到自定义标签栏 Controller 子类。几年前我按照本指南完成了它: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)didClickButtonid)sender;

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

在 CustomTabBarView.m 中:

-(IBAction)didClickButtonid)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 中单击按钮时,它将调用其委托(delegate)函数,在本例中为 CustomTabBarController。该调用与单击实际选项卡栏中的选项卡按钮的功能相同,因此如果您像普通的 UITabBarController 一样正确设置了 CustomTabBarController,它将跳转到选项卡。

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

- (id)initWithFrameCGRect)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。

关于ios - 如何继承 UITabBarController 并替换其 UITabBar View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25919509/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap