如何在tabBar的中间添加自定义按钮
1.在UITabBarController中间添加一个占位控制器
2.把UITabBarController的tabBar的中间的按钮设置为不可点击
-(void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
for (UIView *child in self.tabBar.subviews) {
if ([child isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
NSString * title = [child valueForKeyPath:@"label.text"];
if (title && [title isKindOfClass:[NSString class]] && [title isEqualToString:@""]) {
child.userInteractionEnabled = NO;
return;
}
}
}
}
3.在添加完成所有子控制器后,在UITabBarController的tabBar的中间添加自定义按钮
-(void)addMidButton
{
dispatch_async(dispatch_get_main_queue(), ^{
CGFloat height = self.tabBar.frame.size.height - [UIApplication sharedApplication].keyWindow.safeAreaInsets.bottom;
UIButton * shot = [[UIButton alloc] initWithFrame:CGRectMake((self.tabBar.frame.size.width - height)*0.5, 0, height, height)];
[shot addTarget:self action:@selector(showMidController) forControlEvents:UIControlEventTouchUpInside];
[shot setBackgroundImage:[UIImage imageNamed:@"mid"] forState:UIControlStateNormal];
shot.layer.cornerRadius = height * 0.5;
[self.tabBar addSubview:shot];
});
}
网友评论