美文网首页日常收录
iOS仿脉脉tabbar

iOS仿脉脉tabbar

作者: 海是天空蓝 | 来源:发表于2022-07-27 10:16 被阅读0次
Simulator Screen Shot - iPhone 13 Pro - 2022-07-27 at 10.14.56.png

//
// TabbarViewController.m
// TabbarDemo
//
// Created by Chocolate on 2022/7/27.
//

import "TabbarViewController.h"

import "MainViewController.h"

import "FindViewController.h"

import "StudyViewController.h"

import "MineViewController.h"

define RCColorWithValue(v) [UIColor colorWithRed:(((v) >> 16) & 0xff)/255.0f green:(((v) >> 8) & 0xff)/255.0f blue:((v) & 0xff)/255.0f alpha:1]

define kColorTitle RCColorWithValue(0x333333) // App字体颜色

define kColorTitleHighlight RCColorWithValue(0xFF5C00)

@interface TabbarViewController ()

@end

@implementation TabbarViewController

  • (void)viewDidLoad {
    [super viewDidLoad];

    UIColor *backgroundColor = RCColorWithValue(0xfafafa);
    self.tabBar.barTintColor = backgroundColor;
    self.tabBar.backgroundColor = backgroundColor;
    [self.tabBar setBackgroundImage:[UIImage new]];
    [self.tabBar setShadowImage:[UIImage new]];
    //
    // self.tabBar.layer.shadowColor = [UIColor lightGrayColor].CGColor;
    // self.tabBar.layer.shadowOffset = CGSizeMake(0, -5);
    // self.tabBar.layer.shadowOpacity = 0.3;

    // 五十音
    MainViewController *mainVC = [[MainViewController alloc]init];
    UINavigationController *mainNav = [[UINavigationController alloc]initWithRootViewController:mainVC];

    [self setBaseInfo:mainVC title:@"五十音" normalImage:@"main" selectImage:@"main_select" normalColor:kColorTitle selectColor:kColorTitleHighlight ];

    // 发现
    FindViewController *findVC = [[FindViewController alloc]init];
    findVC.title = @"发现";
    UINavigationController *findNav = [[UINavigationController alloc]initWithRootViewController:findVC];

    [self setBaseInfo:findVC title:@"发现" normalImage:@"find" selectImage:@"find_select" normalColor:kColorTitle selectColor:kColorTitleHighlight ];

    // 跟老师学
    StudyViewController *learnVC = [[StudyViewController alloc]init];
    UINavigationController *learnNav = [[UINavigationController alloc]initWithRootViewController:learnVC];

    [self setBaseInfo:learnVC title:@"跟老师学" normalImage:@"study" selectImage:@"study_select" normalColor:kColorTitle selectColor:kColorTitleHighlight ];

    // 我的
    MineViewController *mineVC = [[MineViewController alloc]init];
    UINavigationController *mineNav = [[UINavigationController alloc]initWithRootViewController:mineVC];

    [self setBaseInfo:mineVC title:@"我的" normalImage:@"mine" selectImage:@"mine_select" normalColor:kColorTitle selectColor:kColorTitleHighlight ];

    self.viewControllers = @[mainNav, findNav, learnNav, mineNav];
    }

  • (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

    NSInteger marginLeft = 50;
    NSInteger tabHeight = 44;
    NSInteger marginBottom = 40;
    self.tabBar.layer.cornerRadius = tabHeight/2;
    self.tabBar.frame = CGRectMake(marginLeft, self.view.bounds.size.height - tabHeight - marginBottom, self.view.bounds.size.width - 2*marginLeft, tabHeight);
    }

/// 设置tab基本信息
/// @param vc controller
/// @param title 标题
/// @param normalImage 默认图
/// @param selectImage 选中图
/// @param normalColor 默认字体颜色
/// @param selectColor 选中字体颜色

  • (void)setBaseInfo:(UIViewController *)vc
    title:(NSString *)title
    normalImage:(NSString *)normalImage
    selectImage:(NSString *)selectImage
    normalColor:(UIColor *)normalColor
    selectColor:(UIColor *)selectColor {
    vc.title = title;

    UIImage *mainNormal = [UIImage imageNamed:normalImage];
    UIImage *mainSelected = [UIImage imageNamed:selectImage];

    [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : normalColor} forState:UIControlStateNormal];
    if (@available(iOS 13.0, *)) {
    self.tabBar.tintColor = selectColor;
    }
    [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : selectColor} forState:UIControlStateSelected];

    vc.tabBarItem.selectedImage = [mainSelected imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    vc.tabBarItem.image = [mainNormal imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }

@end

相关文章

网友评论

    本文标题:iOS仿脉脉tabbar

    本文链接:https://www.haomeiwen.com/subject/uvxzirtx.html