当前位置:   article > 正文

完美封装 tabBar 自定义加号按钮_vue 加号图标

vue 加号图标

1.自定义TabBarController

  1. 1.自定义继承于UITabBarController
  2. 2.利用KVC 替换系统的tabBar
  3. 3. 利用appearance 全局统一设置UITabBarItem
[self setValue:[[LCTabBar alloc] init] forKeyPath:@"tabBar"];
  1. NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
  2. normalAttrs[NSForegroundColorAttributeName] = [UIColor grayColor]; normalAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:12];
  3. NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
  4. selectedAttrs[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
  5. UITabBarItem *item = [UITabBarItem appearance];
  6. [item setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
  7. [item setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];

2.自定义tabBar

  1. 1.自定义继承于UITabBar
  2. 2.初始化
  3. 3.布局

2.1中间有线条的tabBar

如图1


1.png

解决方案: 布局子控件地方, 将中间按钮 bringSubviewToFront

  1. - (void)layoutSubviews
  2. {
  3. [super layoutSubviews];
  4. CGFloat width = self.width;
  5. CGFloat height = self.height;
  6. self.plusButton.center = CGPointMake(width * 0.5, height * 0.5);
  7. self.plusButton.y = -20;
  8. int index = 0;
  9. CGFloat tabBarButtonW = width / 5;
  10. CGFloat tabBarButtonH = height;
  11. CGFloat tabBarButtonY = 0;
  12. for (UIView *tabBarButton in self.subviews) {
  13. if (![NSStringFromClass(tabBarButton.class) isEqualToString:@"UITabBarButton"]) continue;
  14. CGFloat tabBarButtonX = index * tabBarButtonW;
  15. if (index >= 2) {
  16. tabBarButtonX += tabBarButtonW;
  17. }
  18. tabBarButton.frame = CGRectMake(tabBarButtonX, tabBarButtonY, tabBarButtonW, tabBarButtonH);
  19. index++;
  20. }
  21. [self bringSubviewToFront:self.plusButton];
  22. }

2.2中间按钮 超出tabBar的范围不能响应点击事件

如图 2


2.png

解决方案: 重写系统的hitTest方法, 不了解的看官可以去查查事件传递和响应者链条

  1. - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
  2. if (self.isHidden == NO) { // 当前界面 tabBar显示
  3. CGPoint newPoint = [self convertPoint:point toView:self.plusButton];
  4. if ( [self.plusButton pointInside:newPoint withEvent:event]) { // 点 属于按钮范围
  5. return self.plusButton;
  6. }else{
  7. return [super hitTest:point withEvent:event];
  8. }
  9. }
  10. else {
  11. return [super hitTest:point withEvent:event];
  12. }
  13. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/245839
推荐阅读
  

闽ICP备14008679号