#import <UIKit/UIKit.h>
#import "IMEMainTabBarItemModel.h"
NS_ASSUME_NONNULL_BEGIN
@interface IMEMainController : UIViewController
- (void)addViewControllers:(NSArray <IMEMainTabBarItemModel *>*)viewControllers;
@end
NS_ASSUME_NONNULL_END
//
// IMEMainController.m
// dewu
//
// Created by tqh on 2020/7/8.
// Copyright © 2020 tqh. All rights reserved.
//
#import "IMEMainController.h"
@interface IMEMainController ()<UITabBarDelegate>
@property (nonatomic, strong) UITabBar *tabBar;
@property (strong, nonatomic) NSMutableArray *viewControllers;
@property (nonatomic, strong) UIView *addView;
@end
@implementation IMEMainController
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleDefault;
}
- (BOOL)prefersStatusBarHidden
{
return NO;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self _setupSubviews];
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
view.backgroundColor = [UIColor yellowColor];
[self.view addSubview:view];
NSString *url = [NSString stringWithFormat:@"%@%@",@"http://139.224.117.244:8096/qdxm-api",@"/api/common/mqtt/getTopic"];
[IMENetworkManager requestWithURL:url parameters:nil haveLoading:NO haveToast:YES success:^(id response, id ext) {
} failure:^(NSError *error, id response) {
}];
IMEMainTabBarItemModel *model1 = [IMEMainTabBarItemModel new];
model1.title = @"首页";
model1.controllerName = @"HomeController";
IMEMainTabBarItemModel *model2 = [IMEMainTabBarItemModel new];
model2.title = @"我的";
model2.controllerName = @"ViewController";
NSArray *array = @[model1,model2];
[self addViewControllers:array];
}
- (void)addViewControllers:(NSArray<IMEMainTabBarItemModel *> *)viewControllers {
NSMutableArray *itemArray = [NSMutableArray array];
[viewControllers enumerateObjectsUsingBlock:^(IMEMainTabBarItemModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
UIViewController *controller = [NSClassFromString(obj.controllerName) new];
[self addChildViewController:controller];
[self.viewControllers addObject:controller];
UITabBarItem *item = [self _setupTabBarItemWithTitle:obj.title imgName:obj.normalImage selectedImgName:obj.selectedImage tag:idx];
controller.tabBarItem = item;
[itemArray addObject:item];
}];
[self.tabBar setItems:itemArray];
self.tabBar.selectedItem = itemArray[0];
[self tabBar:self.tabBar didSelectItem:itemArray[0]];
}
#pragma mark - <UITabBarDelegate>
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
NSInteger tag = item.tag;
UIView *tmpView = nil;
UIViewController *controller = self.viewControllers[tag];
tmpView = controller.view;
if (self.addView == tmpView) {
return;
} else {
[self.addView removeFromSuperview];
self.addView = nil;
}
self.addView = tmpView;
if (self.addView) {
[self.view addSubview:self.addView];
[self.addView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.view);
make.left.equalTo(self.view);
make.right.equalTo(self.view);
make.bottom.equalTo(self.tabBar.mas_top);
}];
}
}
#pragma mark - private
- (void)_setupSubviews
{
if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) {
[self setEdgesForExtendedLayout: UIRectEdgeNone];
}
self.view.backgroundColor = [UIColor whiteColor];
self.tabBar = [[UITabBar alloc] init];
self.tabBar.delegate = self;
self.tabBar.translucent = NO;
self.tabBar.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.tabBar];
[self.tabBar mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(self.view.mas_bottom).offset(-EMVIEWBOTTOMMARGIN);
make.left.equalTo(self.view.mas_left);
make.right.equalTo(self.view.mas_right);
make.height.mas_equalTo(50);
}];
UIView *lineView = [[UIView alloc] init];
lineView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0];
[self.tabBar addSubview:lineView];
[lineView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.tabBar.mas_top);
make.left.equalTo(self.tabBar.mas_left);
make.right.equalTo(self.tabBar.mas_right);
make.height.equalTo(@1);
}];
}
- (UITabBarItem *)_setupTabBarItemWithTitle:(NSString *)aTitle
imgName:(NSString *)aImgName
selectedImgName:(NSString *)aSelectedImgName
tag:(NSInteger)aTag
{
UITabBarItem *retItem = [[UITabBarItem alloc] initWithTitle:aTitle image:[UIImage imageNamed:aImgName] selectedImage:[UIImage imageNamed:aSelectedImgName]];
retItem.tag = aTag;
[retItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIFont systemFontOfSize:14], NSFontAttributeName, [UIColor lightGrayColor],NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
[retItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:13], NSFontAttributeName, kColor_Blue, NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];
return retItem;
}
#pragma mark - lazy
- (NSMutableArray *)viewControllers {
if (!_viewControllers) {
_viewControllers = [NSMutableArray array];
}
return _viewControllers;
}
@end
网友评论