類似iOS簡(jiǎn)書中間發(fā)布文章用的圓形按鈕
1、在《框架搭建_純代碼》的基礎(chǔ)上稍作修改
2赶舆、自定義幾個(gè)頁面哑姚,用button控制切換
中間按鈕效果圖
比較:第1種法式適合平行切換,缺點(diǎn)就是按鈕超出TabBar范圍的點(diǎn)擊無響應(yīng)芜茵。第2種設(shè)置上可能相對(duì)更麻煩叙量,適合model頁面,如簡(jiǎn)書的彈出頁面夕晓,而不是平行切換宛乃。
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的效果可以通過賦值的圖片控制闯第。