tabBar的自定义
- 完全自定义(自己写一个)
- 更改TabBar的内部子控件
我们今天说的是 先来说更改TabBar的内部子控件,然后在iOS 11遇到一些问题。
更改TabBar的内部子控件
首先是frame的问题在PhoneX
PhoneX的frame问题.jpeg其次是整个tabbar的控件尺寸问题
tabbar控件的问题.gif**********上述的2个问题可以根据安全区域的概念解决****************8
PhoneX上TabBar向上移动的问题
tabbar向上移动.gif***********可以在导航控制器push的时候 拦截 重写 设置tabBar的frame的y值
打印的结果如下:
//向上便宜的y值
y:的值695.000000-----整屏幕的高度:812.000000
++++++{{0, 695}, {375, 83}}
//修改后的
y的值:729.000000-----整个屏幕的高度:812.000000
从上面的 结果可以看出 695+83 != 812,而且y只在push的时候发生变化了
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
//不是跟视图控制器,需要返回按钮
if (self.childViewControllers.count > 0 ) {
viewController.hidesBottomBarWhenPushed = YES;
}
//真正的跳转
[super pushViewController:viewController animated:animated];
CGRect frame = self.tabBarController.tabBar.frame;
NSLog(@"%f-----%f",frame.origin.y,[UIScreen mainScreen].bounds.size.height);
NSLog(@"++++++%@",NSStringFromCGRect(frame));
frame.origin.y = [UIScreen mainScreen].bounds.size.height - frame.size.height;
NSLog(@"%f-----%f",frame.origin.y,[UIScreen mainScreen].bounds.size.height);
self.tabBarController.tabBar.frame = frame;
//访问到父控制器
// for (UIView* next = [self.view superview]; next; next = next.superview) {
// UIResponder *nextResponder = [next nextResponder];
// if ([nextResponder isKindOfClass:[CCTabBarVC class]]) {
//
// CGRect frame = self.tabBarController.tabBar.frame;
// frame.origin.y = [UIScreen mainScreen].bounds.size.height - frame.size.height;
// self.tabBarController.tabBar.frame = frame;
//
// }
// }
}
遇到这些问题的主要原因是iOS11苹果推出的 安全区域
[iOS11适配的网址] : https://github.com/2877025939/iOS11
下面是关于上面2个问题的代码和解决
方法
#import "CCTabBarVC.h"
#import "CCTabBar.h"
@interface CCTabBarVC ()
@end
@implementation CCTabBarVC
- (void)viewDidLoad {
[super viewDidLoad];
[self setupChildVCContent];
[self setupTabBarContent];
}
//设置tabBar
- (void)setupTabBarContent {
__weak typeof(self) weakSelf = self;
CCTabBar *tabBar = [[CCTabBar alloc] init];
//保存代码
tabBar.block = ^{
CCFindVC *findVC = [[CCFindVC alloc] init];
[weakSelf presentViewController:findVC animated:YES completion:nil];
};
[self setValue:tabBar forKeyPath:@"tabBar"];
}
**************TabBar**********************
。h文件
#import <UIKit/UIKit.h>
typedef void(^tabBarBlock)(void);
@protocol CCTabBar <NSObject>
- (void)ccTabBarDidClicked:(UIButton *)button;
@end
@interface CCTabBar : UITabBar
/** block */
@property (nonatomic, strong) tabBarBlock block;
@end
***************。m文件*******************
#import "CCTabBar.h"
@interface CCTabBar ()
/** 加好按钮 */
@property (nonatomic, weak) UIButton *paluseButton;
@end
@implementation CCTabBar
- (UIButton *)paluseButton {
if (!_paluseButton) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateHighlighted];
[button addTarget:self action:@selector(handleDidClick:) forControlEvents:UIControlEventTouchUpInside];
[button sizeToFit];
[self addSubview:button];
_paluseButton = button;
}
return _paluseButton;
}
- (void)handleDidClick:(UIButton *)button {
//调用block
if (_block) {
_block();
}
}
///布局子控件
- (void)layoutSubviews {
[super layoutSubviews];
NSInteger count = self.items.count;
CGFloat btnW = self.bounds.size.width / (count + 1);
CGFloat btnH = self.bounds.size.height;
self.paluseButton.center = CGPointMake(self.cc_width*0.5, self.cc_height *0.5);
//方法一
/*
//简单粗暴的方法,直接设置tabbar的高度是49
self.paluseButton.cc_height = 49;
self.paluseButton.cc_y = 0;
*/
//方法二 根据安全区域 动态的更改
UIWindow *window = [UIApplication sharedApplication].keyWindow;
if (@available(iOS 11.0, *)) {
btnH = self.bounds.size.height - window.safeAreaInsets.bottom;
self.paluseButton.cc_centerY = (self.cc_height - window.safeAreaInsets.bottom) *0.5;
}
CGFloat x = 0;
int i = 0;
///遍历子控件
for (UIView *tabBarButton in self.subviews) {
if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
if (i == 2) {
i += 1;
}
x = i * btnW;
//简单粗暴的方法 btnH = 49
tabBarButton.frame = CGRectMake(x, 0, btnW, btnH);
i ++;
}
}
}
网友评论