TabBar中间按钮设置

类似iOS简书中间发布文章用的圆形按钮
1、在《框架搭建_纯代码》的基础上稍作修改
2、自定义几个页面,用button控制切换

中间按钮效果图

比较:第1种法式适合平行切换,缺点就是按钮超出TabBar范围的点击无响应。第2种设置上可能相对更麻烦,适合model页面,如简书的弹出页面,而不是平行切换。

1、在《框架搭建_纯代码》的基础上稍作修改

添加按钮的主要代码如下:

    //缺点:1、按钮超过bottom的部分点击无响应    2、点击除按钮外的中间部分时也展示中间item的页面
    //3个item是可能范围太大,如果调整item数量,缺点2应该影响不大。
    UIButton *btn = [[UIButton alloc] init];
    btn.backgroundColor = [UIColor redColor];
    [btn setFrame:CGRectMake(130, -12, 60, 60)];
    btn.clipsToBounds = YES;
    btn.layer.cornerRadius = 30;
    [btn addTarget:self action:@selector(clickCenterButton) forControlEvents:UIControlEventTouchUpInside];
    
    [tabBarControl.tabBar addSubview:btn];

2、自定义几个页面,用button控制切换

整体思路:自定义三个按钮,中间按钮设置较大,并且切成圆角,在点击事件中设置隐藏或展示或弹出页面即可。代码如下:

AppDelegate中代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    ViewController *vc = [[ViewController alloc] init];
    
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];

    //指定应用的跟视图控制器
    self.window.rootViewController = nav;
    
    return YES;
}

ViewController中主要代码

#import "ViewController.h"
#import "ModelViewController.h"

#define ScreenHeight [UIScreen mainScreen].bounds.size.height
#define ScreenWidth  [UIScreen mainScreen].bounds.size.width

@interface ViewController ()
{
    UIView *view1;
    UIView *view2;
    UIView *view3;
    
    ModelViewController *modelVC;
}
@end

@implementation ViewController

- (instancetype)init
{
    self = [super init];
    
    if (self)
    {
        [self createUI];
    }
    
    return self;
}

- (void)createUI
{
    view1 = [[UIView alloc] initWithFrame:self.view.frame];
    view1.backgroundColor = [UIColor greenColor];
    [self.view addSubview:view1];
        
    view3 = [[UIView alloc] initWithFrame:self.view.frame];
    view3.backgroundColor = [UIColor lightGrayColor];
    view3.hidden = YES;
    [self.view addSubview:view3];
    
    UIButton *btn1 = [[UIButton alloc] init];
    [btn1 setFrame:CGRectMake(20, ScreenHeight-44, 44, 44)];
    btn1.backgroundColor = [UIColor greenColor];
    [btn1 addTarget:self action:@selector(tapFirstButton) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn1];
    
    UIButton *btn2 = [[UIButton alloc] init];
    [btn2 setFrame:CGRectMake(ScreenWidth/2-30, ScreenHeight-44-16, 60, 60)];
    btn2.clipsToBounds = YES;
    btn2.layer.cornerRadius = 30;
    btn2.backgroundColor = [UIColor redColor];
    [btn2 addTarget:self action:@selector(tapSecondButton) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn2];
    
    UIButton *btn3 = [[UIButton alloc] init];
    btn3.backgroundColor = [UIColor grayColor];
    [btn3 setFrame:CGRectMake(ScreenWidth-44-20, ScreenHeight-44, 44, 44)];
    [btn3 addTarget:self action:@selector(tapThirdButton) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn3];
}

- (void)tapFirstButton
{
    view1.hidden = NO;
    view2.hidden = YES;
    view3.hidden = YES;
    self.title = @"first";
}

- (void)tapSecondButton
{
    if (!modelVC)
    {
        modelVC = [[ModelViewController alloc] init];
    }
    
    [self presentViewController:modelVC animated:YES completion:nil];
}

- (void)tapThirdButton
{
    view1.hidden = YES;
    view2.hidden = YES;
    view3.hidden = NO;
    
    self.title = @"third";
}

以上基本完成了项目需求,button的效果可以通过赋值的图片控制。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,314评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,679评论 4 61
  • 忍耐是有限度的,耐心是会用光的。 我知道说这句话,就证明自己修炼还不够。 今天没控制住,怒而吼!因为我真的很耐心的...
    玫瑰冰糖阅读 1,346评论 0 0
  • 精进 任何华丽的语言都抵不过发自内心的改变 感受 三天的课程结束,从工作角度我们完成了一个项目;从个人角度,通过这...
    若兰ZHOU阅读 1,576评论 0 1
  • 今天1尝试自己上色,零模仿!总结出几处经验… 好几天没有绘画了,对色彩的搭配选择还是不到位,所以导致下面这种结局 ...
    Qincius阅读 1,553评论 0 0

友情链接更多精彩内容