类似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的效果可以通过赋值的图片控制。