系统UITabbarController

新建MainTabbarController类

在MainTabbarController.m中

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    //创建viewContronllers
    [self createViewControllers];
    //创建tabBarItems
    [self createItems];
}

#pragma mark 创建viewControllers

- (void)createViewControllers
{
    UINavigationController * nc1 = [[UINavigationController alloc] initWithRootViewController:[[viewController1 alloc] init]];
    UINavigationController * nc2 = [[UINavigationController alloc] initWithRootViewController:[[viewController2 alloc] init]];
    UINavigationController * nc3 = [[UINavigationController alloc] initWithRootViewController:[[viewController3 alloc] init]];

    self.viewControllers = @[nc1,nc2,nc3];
}

- (void)createItems
{
    NSArray *titleArray = @[@"1", @"2", @"3"];
    NSArray *selectImageName = @[@"selected1.png", @"selected2.png", @"selected3.png"];
    NSArray *unselectImageName = @[@"normal1.png", @"normal2.png", @"normal3.png"];
    for (int i = 0; i < titleArray.count; i++) {
        //需要对图片进行单独处理
        UIImage *selectImage = [UIImage imageNamed:selectImageName[i]];
        selectImage = [selectImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        UIImage *unselectImage = [UIImage imageNamed:unselectImageName[i]];
        unselectImage = [unselectImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        //以上是防止在bar上显示为阴影而不是图片
        UITabBarItem *item = self.tabBar.items[i];
        item.selectedImage = selectImage;
        item.image = unselectImage;
        item.title = titleArray[i];
    }
    [self.tabBar setBackgroundImage:[UIImage imageNamed:@"img_tabBar_bg_iOS8.png"]];
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor orangeColor]} forState:UIControlStateSelected];
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor darkGrayColor]} forState:UIControlStateNormal];
}

特别鸣谢 詹Q同学

### 3.1 使用自定义 `UITabBarController` 子类调整 TabBar 高度 系统原生的 `UITabBar` 高度是固定的,无法直接通过公开 API 进行修改。要实现自定义 TabBar 高度,通常需要继承 `UITabBarController` 并重写其 `viewDidLoad` 方法,通过修改 `tabBar.frame` 来实现高度调整。但需要注意的是,直接修改 `tabBar.frame` 可能会导致子视图控制器的布局错位,因此还需要对子视图控制器的 `bottom` 约束或 `safeAreaInsets` 进行适配。 示例代码如下: ```objective-c - (void)viewDidLoad { [super viewDidLoad]; // 设置自定义高度 CGFloat customTabBarHeight = 80.0; // 获取当前 TabBar 的 frame CGRect tabBarFrame = self.tabBar.frame; tabBarFrame.size.height = customTabBarHeight; tabBarFrame.origin.y = self.view.bounds.size.height - customTabBarHeight; // 更新 TabBar 的 frame self.tabBar.frame = tabBarFrame; // 强制更新子视图控制器的 view 的布局 [self.view setNeedsLayout]; } ``` 此方法适用于需要在特定界面中调整 TabBar 高度的场景,但需注意系统不会自动调整子视图控制器的布局区域,需手动处理 `safeAreaInsets` 或 `edgesForExtendedLayout` [^1]。 ### 3.2 使用 `UIEdgeInsets` 适配子视图控制器的布局 由于修改 `UITabBar` 高度后,子视图控制器的 `safeAreaInsets.bottom` 仍然使用系统默认值,因此需要在子视图控制器中手动调整底部偏移,以避免内容被 TabBar 遮挡。 示例代码如下: ```objective-c - (void)viewDidLayoutSubviews { [super viewDidLayoutSubviews]; // 获取 TabBar 的实际高度 CGFloat tabBarHeight = self.tabBarController.tabBar.frame.size.height; // 获取系统默认的底部安全区域偏移 CGFloat systemBottomInset = self.view.safeAreaInsets.bottom; // 计算实际需要的底部偏移 CGFloat customBottomInset = tabBarHeight - systemBottomInset; // 调整底部约束 self.contentViewBottomConstraint.constant = customBottomInset; } ``` 此方法可以确保子视图控制器的内容不会被自定义高度的 TabBar 遮挡,同时保持与安全区域的一致性 [^2]。 ### 3.3 使用自定义 TabBar 替代系统 TabBar 如果需要更灵活的布局控制,例如支持动态高度、复杂动画或自定义交互逻辑,可以完全替换系统 `UITabBar`,使用自定义的 `UIView` 作为 TabBar 的容器。此时可以将 `UITabBarController` 的 `tabBar` 隐藏,并在底部添加自定义视图。 示例代码如下: ```objective-c // 隐藏系统 TabBar self.tabBar.hidden = YES; // 创建自定义 TabBar CustomTabBar *customTabBar = [[CustomTabBar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - customHeight, self.view.bounds.size.width, customHeight)]; [self.view addSubview:customTabBar]; ``` 此方法适用于需要完全自定义 TabBar 样式和交互逻辑的场景,例如动态调整高度、支持服务器配置、图片固定范围显示等 [^2]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值