類似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的效果可以通過賦值的圖片控制。