TabBar中間按鈕設(shè)置

類似iOS簡書中間發(fā)布文章用的圓形按鈕
1、在《框架搭建_純代碼》的基礎(chǔ)上稍作修改
2、自定義幾個(gè)頁面,用button控制切換

中間按鈕效果圖

比較:第1種法式適合平行切換,缺點(diǎn)就是按鈕超出TabBar范圍的點(diǎn)擊無響應(yīng)。第2種設(shè)置上可能相對更麻煩,適合model頁面,如簡書的彈出頁面,而不是平行切換。

1、在《框架搭建_純代碼》的基礎(chǔ)上稍作修改

添加按鈕的主要代碼如下:

    //缺點(diǎn):1、按鈕超過bottom的部分點(diǎn)擊無響應(yīng)    2、點(diǎn)擊除按鈕外的中間部分時(shí)也展示中間item的頁面
    //3個(gè)item是可能范圍太大,如果調(diào)整item數(shù)量,缺點(diǎn)2應(yīng)該影響不大。
    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、自定義幾個(gè)頁面,用button控制切換

整體思路:自定義三個(gè)按鈕,中間按鈕設(shè)置較大,并且切成圓角,在點(diǎn)擊事件中設(shè)置隱藏或展示或彈出頁面即可。代碼如下:

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];

    //指定應(yīng)用的跟視圖控制器
    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";
}

以上基本完成了項(xiàng)目需求,button的效果可以通過賦值的圖片控制。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,355評論 25 708
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,704評論 4 61
  • 忍耐是有限度的,耐心是會用光的。 我知道說這句話,就證明自己修煉還不夠。 今天沒控制住,怒而吼!因?yàn)槲艺娴暮苣托牡?..
    玫瑰冰糖閱讀 190評論 0 0
  • 精進(jìn) 任何華麗的語言都抵不過發(fā)自內(nèi)心的改變 感受 三天的課程結(jié)束,從工作角度我們完成了一個(gè)項(xiàng)目;從個(gè)人角度,通過這...
    若蘭ZHOU閱讀 250評論 0 1
  • 今天1嘗試自己上色,零模仿!總結(jié)出幾處經(jīng)驗(yàn)… 好幾天沒有繪畫了,對色彩的搭配選擇還是不到位,所以導(dǎo)致下面這種結(jié)局 ...
    Qincius閱讀 241評論 0 0

友情鏈接更多精彩內(nèi)容