UITabBarController(二)自定义UITabBar方式一
来源:互联网 发布:手机淘宝能发布食品吗? 编辑:程序博客网 时间:2024/05/23 00:53
转载自:(http://www.jianshu.com/p/fcf0f6933ffe)
自定义UITabBar代码如下:
// 中间凸起部分的按钮-(UIButton *)centerBtn{ if (_centerBtn == nil) { _centerBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 90, 90)]; // 禁止按钮在高亮下改变颜色 _centerBtn.adjustsImageWhenHighlighted = NO; [_centerBtn setImage:[UIImage imageNamed:@"tab_launch"] forState:UIControlStateNormal]; [_centerBtn addTarget:self action:@selector(clickCenterBtn:) forControlEvents:UIControlEventTouchUpInside]; } return _centerBtn;}- (void)clickCenterBtn:(UIButton *)sender{ NSLog(@"点击中间按钮事件");}// 重写layoutSubviews-(void)layoutSubviews{ [super layoutSubviews]; self.backgroundColor = [UIColor whiteColor]; // 把 tabBarButton 取出来 NSMutableArray *tabBarBtnArr = [NSMutableArray array]; for (UIView *view in self.subviews) { if ([view isKindOfClass:NSClassFromString(@"UITabBarButton")]) { [tabBarBtnArr addObject:view]; } } CGFloat barWidth = self.bounds.size.width; CGFloat barHeight = self.bounds.size.height; CGFloat centerBtnWidth = CGRectGetWidth(self.centerBtn.frame); CGFloat centerBtnHeight = CGRectGetHeight(self.centerBtn.frame); // 设置中间按钮的位置,居中,部分凸起 self.centerBtn.center = CGPointMake(barWidth/2, barHeight - centerBtnWidth/2 + 10); [self addSubview:self.centerBtn]; // 重新布局其他 tabBarItem // 平均分配其他 tabBarItem 的宽度 CGFloat barItemWidth = (barWidth - centerBtnWidth) / tabBarBtnArr.count; // 逐个布局 tabBarItem, 修改 UITabBarButton 的 frame [tabBarBtnArr enumerateObjectsUsingBlock:^(UIView * _Nonnull view, NSUInteger idx, BOOL * _Nonnull stop) { CGRect frame = view.frame; if (idx >= tabBarBtnArr.count / 2) { // 重新设置 x 坐标, 如果排在中间按钮的右边需要加上中间按钮的宽度 frame.origin.x = idx * barItemWidth + centerBtnWidth; } else { frame.origin.x = idx * barItemWidth; } // 重新设置宽度 frame.size.width = barItemWidth; view.frame = frame; }]; // 把中间按钮带到视图最前面 [self bringSubviewToFront:self.centerBtn];}// 凸起button原本是接收不到点击事件的,需要重写 hitTest:withEvent: 方法- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{ if (self.clipsToBounds || self.hidden || (self.alpha == 0.f)) { return nil; } UIView *result = [super hitTest:point withEvent:event]; // 如果事件发生在tabbar里面直接返回 if (result) { return result; } // 这里遍历那些超出的部分就可以了,不过这么写比较通用。 for (UIView *subview in self.subviews) { // 吧这个坐标从tabbar的坐标系转为 subview 的坐标系 CGPoint subPoint = [subview convertPoint:point fromView:self]; result = [subview hitTest:subPoint withEvent:event]; // 如果事件发生在 subview 里就返回 if (result) { return result; } } return nil;}
OK !最后需要在自定义的UITabBarController的viewDidLoad方法中添加一行代码:
// 利用KVO来使用自定义的tabBar [self setValue:[[CustomerBar alloc] init] forKey:@"tabBar"];
最终效果如下:
阅读全文
0 0
- UITabBarController(二)自定义UITabBar方式一
- UITabBarController(三)自定义UITabBar方式二
- UITabBarController中自定义UITabBar
- UITabBarController中自定义UITabBar
- 自定义实现UITabBarController和UITabBar
- iOS UITabBarController 的 UITabBar 自定义
- 自定义UITabBarController、UITabBar和UIButton
- UITabBar自定义方式二(容器类控制器)
- 自定义UITabBarController以及UITabBar的分析
- iOS 自定义UITabBarController以及UITabBar的分析
- UITabBarController中自定义UITabBar方法2
- iOS中 UITabBarController中自定义UITabBar
- UItabbarController自定义 常用架构 UITabBar UITabBaritem
- UITabBarController、UITabBar
- 自定义UITabbarController及向UITabBar中添加自定义按钮
- UITabBarController自定义(一)
- UITabBar自定义方式三(中间有按钮)
- UITabBarController with hidden UITabBar
- mybatis中查询有复杂关联对象的结果集
- 新项目开启,unity-lua起步
- Java 线程 2
- Java设计模式之策略模式
- 在jsp里面写java代码
- UITabBarController(二)自定义UITabBar方式一
- CF 262 C. Maxim and Discounts 贪心(水)
- Angular 2的管道Pipe
- 重磅丨AI公共政策成全球热点,美国ITI发布《人工智能政策原则》
- 算法设计:最多约数问题
- 几本安卓方面的学习书籍分享给大家,有新的就会更新
- Android开发集成科大讯飞语音识别+语音合成功能
- 人物丨深度学习大神Hinton推翻自己30年的学术成果另造新世界
- EventBus源码解析(史上最全的源码解析)