如图:创建中间按钮
代码如下:
// 撰写按钮
fileprivate lazy var composeButton:UIButton = UIButton.cz_imageButton("tab_btn_add", backgroundImageName: "tab_btn_add")
/// @implementation UIButton (CZAddition)
+ (instancetype)cz_imageButton:(NSString *)imageName backgroundImageName:(NSString *)backgroundImageName {
UIButton *button = [[self alloc] init];
[button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
NSString *imageNameHL = [imageName stringByAppendingString:@"_highlighted"];
[button setImage:[UIImage imageNamed:imageNameHL] forState:UIControlStateHighlighted];
[button setBackgroundImage:[UIImage imageNamed:backgroundImageName] forState:UIControlStateNormal];
NSString *backgroundImageNameHL = [backgroundImageName stringByAppendingString:@"_highlighted"];
[button setBackgroundImage:[UIImage imageNamed:backgroundImageNameHL] forState:UIControlStateHighlighted];
[button sizeToFit];
return button;
}
// 设置撰写按钮
fileprivate func setupComposeButton(){
// 撰写按钮
tabBar.addSubview(composeButton)
// 计算按钮的宽度
let count = CGFloat(childViewControllers.count)
// 将向内缩进的宽度减少,能够让按钮的宽度变大,盖住容错点,防止穿帮!
let aWidth:CGFloat = tabBar.bounds.width / count - 1
// CGRecInset 正数向内缩进,负数向外扩展 67 - 49 = 18
composeButton.frame = tabBar.bounds.insetBy(dx: aWidth, dy: -12)
print("撰写按钮宽度 \(composeButton.bounds.width)")
// 按钮监听方法
composeButton.addTarget(self, action: #selector(composeStatus), for: .touchUpInside)
}
// MARK:- 中间按钮
extension LJMainController {
// fileprivate 文件内私有属性和方法,仅在当前文件中可以访问,包括同一个文件中不同的类
// private: 私有属性和方法,仅在当前类中可以访问,不包括分类。
@objc fileprivate func composeStatus(){
}
}
需求是,当是任务管理Item,点击加号按钮时,弹出框是 任务创建页面。
当是OA办公Item时,点击加号按钮,弹出的是 创建工单管理
如何判断点击了哪个选项,请看下篇文章