123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- //
- // XMGTabBar.m
- // 备课-百思不得姐
- //
- // Created by MJ Lee on 15/6/15.
- // Copyright © 2015年 小码哥. All rights reserved.
- //
- #import "MJTabBar.h"
- #import "Masonry.h"
- #import "UIView+LK.h"
- @interface MJTabBar()
- @property (nonatomic,strong) NSMutableArray *tabBarButtonArray;
- @property (nonatomic,weak) UIButton *publishButton;
- @end
-
- @implementation MJTabBar
- - (nonnull instancetype)initWithFrame:(CGRect)frame
- {
- if (self = [super initWithFrame:frame]) {
-
- _tabBarButtonArray=[NSMutableArray array];
-
-
-
-
- UIButton *publishButton = [[UIButton alloc] init];
- [publishButton setTitle:@"福利" forState:UIControlStateNormal];
- [publishButton setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
- [publishButton setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
- [self addSubview:publishButton];
- self.publishButton=publishButton;
- publishButton.backgroundColor=[UIColor greenColor];
- [publishButton mas_makeConstraints:^(MASConstraintMaker *make) {
- make.center.equalTo(self);
- make.size.mas_equalTo(CGSizeMake(JX_SCREEN_WIDTH/3, JX_SCREEN_HEIGHT>=812?64:49));
- }];
- [publishButton addTarget:self action:@selector(publishClick) forControlEvents:UIControlEventTouchUpInside];
-
-
-
-
-
-
-
- }
- return self;
- }
- - (void)publishClick
- {
-
- }
- - (void)layoutSubviews
- {
- [super layoutSubviews];
-
- // 把 tabBarButton 取出来(把 tabBar 的 subViews 打印出来就明白了)
- NSMutableArray *tabBarButtonArray = [NSMutableArray array];
- for (UIView *view in self.subviews) {
- if ([view isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
- [tabBarButtonArray addObject:view];
-
- [view removeFromSuperview];
- }
- }
-
- CGFloat barWidth = self.bounds.size.width;
- CGFloat barHeight = self.bounds.size.height;
- CGFloat centerBtnWidth = CGRectGetWidth(self.publishButton.frame);
- CGFloat centerBtnHeight = CGRectGetHeight(self.publishButton.frame);
- // 设置中间按钮的位置,居中,凸起一丢丢
- self.publishButton.center = CGPointMake(barWidth / 2, barHeight - centerBtnHeight/2 - 5);
- // 重新布局其他 tabBarItem
- // 平均分配其他 tabBarItem 的宽度
- CGFloat barItemWidth = (barWidth - centerBtnWidth) / tabBarButtonArray.count;
- // 逐个布局 tabBarItem,修改 UITabBarButton 的 frame
- [tabBarButtonArray enumerateObjectsUsingBlock:^(UIView * _Nonnull view, NSUInteger idx, BOOL * _Nonnull stop) {
-
- CGRect frame = view.frame;
- if (idx >= tabBarButtonArray.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.publishButton];
- }
- @end
|