IOS 定制中间突出UItabBar

2023-05-16

前言:

       公司的项目需要定制一个中间突出的TabBar,在github 上找到一份可以参考的代码(虽然是四年前的,但是还是很有参考价值)。 网址:https://github.com/boctor/idev-recipes/tree/master/RaisedCenterTabBar。作者的readme文档写的很好,这里给出翻译(很不错的思路哦)


先看看效果:


思路:

## Problem:

问题:


Apps like [Instagram][], [DailyBooth][] and [Path™][] have what

looks like a standard UITabBarController, but the center tab bar is

raised or colored. How do we recreate this look?


[Instagram][], [DailyBooth][] and [Path™][] 这些app有一个看起来像标准 tabBarController,但是呢,tabBar的中间是凸起的或者着色的。我们怎样做可以构建这种现实效果呢?


## Solution:

解决方案:


These tab bars look pretty standard with the exception of the

center item, so we’ll start out with a standard UITabBarController

which contains a UITabBar.


这些标签栏除了中间项以外看起来都相当的标准,所以我们会从一个标准的包含一个tabBarUITabBarController开始。


Looking at [the images][] inside each app, it is quickly apparent

that the middle tab bar is simply a custom UIButton.


查看每个应用内的图片,显而易见的是中间的标签是一个简单的自定义button



A UITabBar contains an array of UITabBarItems, which inherit from

UIBarItem. But unlike UIBarButtonItem that also inherits from

UIBarItem, there is no API to create a UITabBarItem with a

customView.


一个UITabBar 包含了一个UITabBaritems的数组,UITabBaritem继承自UIBarItem。但是和同样继承自UIBarItemUIBarButtonItem不同,苹果官方没有提供给UITabBarItem创建自定义视图的api.


So instead of trying to create a custom UITabBarItem, we’ll just

create a regular one and then put the custom UIButton on top of the

UITabBar.


所以呢,大家不用去尝试创建一个自定义的UITabBarItem,我们只需要创建一个标准的UITabBar,然后把自定义的button放在UITabBar上面就可以了。


Our basic recipe is then to create a subclass of UITabBarController

and add a custom UIButton on top of the UITabBar.


我们的基本方案是子类化UITabBarController然后添加一个自定义的buttonUITabBar上面。


If the button is the same height as the UITabBar, then we set the

center of the button to the center of the UITabBar. If the button

is slightly higher, then we do the the same thing except we adjust

the center’s y value to account for the difference in height.


如果buttonUITabBar一样高呢,我们就包buttoncenterUITabBarcenter对齐。如果button稍微高那么一点呢,我们做同样的事情,然后设定centerY值,以对应高度的差。本文原创,转载请注明出处:http://blog.csdn.net/zhenggaoxing/article/details/44623999

UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
    [button setBackgroundImage:buttonImage forState:UIControlStateNormal];
    [button setBackgroundImage:highlightImage forState:UIControlStateHighlighted];
    
    CGFloat heightDifference = buttonImage.size.height - self.tabBar.frame.size.height;
    if (heightDifference < 0)
      button.center = self.tabBar.center;
    else
    {
      CGPoint center = self.tabBar.center;
      center.y = center.y - heightDifference/2.0;
      button.center = center;
    }
    
    [self.view addSubview:button];


代码实现

这里我借鉴了上文作者的代码,针对我的需要进行了封装,下面放代码:

//
//  TabBarViewController.m
//  tabBarViewController
//
//  Created by Bc_Ltf on 15/3/25.
//  Copyright (c) 2015年 Bc_ltf. All rights reserved.
//

#import "TabBarViewController.h"

@interface TabBarViewController ()<UITabBarControllerDelegate>

@end

@implementation TabBarViewController
@synthesize button;
@synthesize myTabBar;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setup];


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark- setup
-(void)setup
{
    //  添加突出按钮
    [self addCenterButtonWithImage:[UIImage imageNamed:@"main"] selectedImage:[UIImage imageNamed:@"mainH"]];
    //  UITabBarControllerDelegate 指定为自己
    self.delegate=self;
    //  指定当前页——中间页
    self.selectedIndex=2;
    //  设点button状态
    button.selected=YES;
    //  设定其他item点击选中颜色
    myTabBar.tintColor= [UIColor colorWithRed:222/255.0 green:78/255.0 blue:22/255.0 alpha:1];
}


#pragma mark - addCenterButton
// Create a custom UIButton and add it to the center of our tab bar
-(void) addCenterButtonWithImage:(UIImage*)buttonImage selectedImage:(UIImage*)selectedImage
{
    button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button addTarget:self action:@selector(pressChange:) forControlEvents:UIControlEventTouchUpInside];
    button.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
    
    //  设定button大小为适应图片
    button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
    [button setImage:buttonImage forState:UIControlStateNormal];
    [button setImage:selectedImage forState:UIControlStateSelected];
    
    //  这个比较恶心  去掉选中button时候的阴影
    button.adjustsImageWhenHighlighted=NO;
    
    
    /*
     *  核心代码:设置button的center 和 tabBar的 center 做对齐操作, 同时做出相对的上浮
     */
    CGFloat heightDifference = buttonImage.size.height - self.tabBar.frame.size.height;
    if (heightDifference < 0)
        button.center = self.tabBar.center;
    else
    {
        CGPoint center = self.tabBar.center;
        center.y = center.y - heightDifference/2.0;
        button.center = center;
    }
    
    [self.view addSubview:button];
}

-(void)pressChange:(id)sender
{
    self.selectedIndex=2;
    button.selected=YES;
}

#pragma mark- TabBar Delegate

//  换页和button的状态关联上

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    if (self.selectedIndex==2) {
        button.selected=YES;
    }else
    {
        button.selected=NO;
    }
}

@end

源代码:

https://git.oschina.net/zhengaoxing/IOS-rase-center-tabbar

本文原创,转载请注明出处:http://blog.csdn.net/zhenggaoxing/article/details/44623999


本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

IOS 定制中间突出UItabBar 的相关文章

  • ubutnu系统维护

    文章目录 1 参考知识点 2 分区3 内核系统版本号查看是uefi还是leagcy启动升级设置时区自动清理手动清理 xff08 推荐 xff09 4 软件修改主机名修改用户名修改用户秘密root密码添加新用户用户组 生成随机密码管理命令温控
  • python之pcl

    pcl是点云可视化软件 xff0c 安装 conda create n py36 python 61 3 6 conda activate py36 方法1 xff0c 推荐 conda install c sirokujira pytho
  • 从零开始学习verilog:1

    在线资料 Verilog 教程 verilog tutorial 推荐书籍 verilog数字系统技术和实例分析 环境搭建 vscode verilog HDL SystemVerilog verilog语言高亮 Verilog Testb
  • verilog之环境记录

    操作系统 xff1a ubuntu18 04 环境安装 参考 span class token function sudo span span class token function apt span span class token f
  • Qt之程序打包发布

    文章目录 linux环境1 QtCreate使用Release版本编译2 使用ldd命令查看和导出需要的库3 编写执行程序的sh文件4 执行程序 Windows环境 qt程序发布打包方法如下 linux环境 原文链接 xff1a https
  • conda常用命令:安装,更新,创建,激活,关闭,查看,卸载,删除,清理,重命名,换源,问题

    文章目录 下载安装升级卸载Anaconda软件conda环境使用基本命令查看指定包可安装版本信息命令更新 xff0c 卸载安装包 xff1a 删除虚拟环境清理 xff08 conda瘦身 xff09 复制 重命名 删除env环境conda自
  • python之os文件路径、文件名、后缀分割

    直接垒代码 import os file path 61 span class hljs string 34 E tt abc py 34 span filepath fullflname 61 os span class hljs pre
  • 目前很火的SD-WAN是什么意思

    SD WAN SD WAN xff0c 即广域软件定义网络 xff0c 是将SDN技术应用到广域网场景中所形成的一种服务 xff0c 这种服务用于连接广阔地理范围的企业网络 数据中心 互联网应用及云服务 这种服务的典型特征是将网络控制能力通
  • cmd-bat 命令延时方法

    参考 xff1a https blog csdn net jk110333 article details 41869053 按照建议使用方法4 命令
  • Qt5中文教程

    1 教程 PyQt5中文教程PyQt5英文教程Qt5中文教程Qt 编程指南 下拉式复选框QComboCheckBox https blog csdn net LJX4ever article details 78039318 http qa
  • train,val,test的区别

    参考 xff1a https www mobibrw com 2017 7966
  • ubuntu18.04下搭建PX4编译环境

    Ubuntu18 04 Development Environment of PX4 Firmware 1安装Ubuntu2开始配置环境2 1下载PX4的固件源码2 2环境配置Bash ScriptsGazebo JMAVSim and N
  • PIX4中CAN调试——学习记录

    PIX4中CAN调试 学习记录 一 先行知识 nuttx驱动二 PX4中CAN驱动设置 固件版本 xff1a V1 13 0 编译版本 xff1a make px4 fmu v3 default 一 先行知识 nuttx驱动 Nuttx驱动
  • 蓝牙 舵狗 openmv通信相关

    总的思路是以openmv 接收蓝牙的指令 xff0c 如果是自动选项 xff0c 就在openmv 运行识别红球进行固定距离跟踪的程序 xff1b 如果是手动选项 xff0c openmv就直接把所得到的数据传给STM xff13 xff1
  • TM4C123G开发板学习记录(八)存储和安全管理(上)

    前言 TM4C123GH6PM有四种类型内存 xff1a FlashSRAMEEPROMROM 芯片设计厂商提供了灵活的操作 xff0c 性能优化 xff0c 和安全控制设计 本章学习目标 四种内存的特点和操作BitBang技术和使用MPU
  • 11-16 Fluent结果查看及后处理功能(做动画、监控点)

    lt
  • 如果你也23岁(2)

    接着上一篇 xff0c 跟大伙分享一下原来的这篇文章 xff1a 23 岁那年你正处在哪个状态 xff1f 现在呢 xff1f 我 xff0c 23岁 xff0c 应届毕业生 生活 xff0c 工作 xff0c 爱情都处于人生的低谷 xff
  • (实测可用)STM32 CubeMx安装教程

    一 STM32CubeMX 简介 xff08 1 xff09 STM32 是Cortex ARM内核架构的芯片 xff0c 中文名称为意法半导体 xff0c 是目前市面上应用自广泛的MCU芯片 STM32CubeMX 是 ST 意法半导体近
  • 25个国内外文献数据库

    1 国家哲学社会科学文献中心 网址 xff1a http www ncpssd org 2 中国国家数字图书馆 网址 xff1a http mylib nlc cn web guest home 3 中国科技论文在线 网址 xff1a ht
  • 巧用 IOPS 提升 Etcd 30% 的写入性能

    Laf 公众号已接入了 AI 聊天机器人 x1f916 xff0c 支持 GPT Claude 以及 Laf 专有模型 xff0c 可通过指令来随意切换模型 欢迎前来调戏 x1f447 本文转自博客园 xff0c 原文 xff1a http

随机推荐