// // YMBaseViewController.m // MSYOUPAI // // Created by YoMi on 2024/2/4. // Copyright © 2024 MS. All rights reserved. // #import "YMBaseViewController.h" #import "YYKit.h" @interface YMBaseViewController () @property (nonatomic, strong, readwrite) YMBaseViewModel *viewModel; @end @implementation YMBaseViewController + (instancetype)allocWithZone:(struct _NSZone *)zone { YMBaseViewController *viewController = [super allocWithZone:zone]; @weakify(viewController) [[viewController rac_signalForSelector:@selector(viewDidLoad)] subscribeNext:^(id x) { @strongify(viewController) [viewController ym_setupViews]; [viewController ym_bindViewModel]; }]; return viewController; } - (instancetype)initWithViewModel:(YMBaseViewModel*)viewModel{ if (self = [super init]) { self.viewModel = viewModel; } return self; } - (void)viewDidLoad { [super viewDidLoad]; self.extendedLayoutIncludesOpaqueBars = NO; self.navigationController.navigationBar.hidden = YES; self.view.backgroundColor = UIColor.whiteColor; if(@available(iOS 15.0,*)){ [UITableView appearance].sectionHeaderTopPadding = 0; } RAC(self.ym_customNavView.ym_customNavItem, title) = RACObserve(self.viewModel, customNavTitle); [self.view addSubview:self.ym_customNavView]; [self.ym_customNavView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.view); make.left.equalTo(self.view); make.right.equalTo(self.view); make.height.mas_equalTo(kYMNavHeight); }]; /* 非根视图,添加返回按钮 */ if (self.navigationController.viewControllers.count > 1 && !self.isHideLeftItem) { @weakify(self) [self setLeftBarButtonWithImage:@"ym_navigation_black_icon" block:^(id _Nonnull sender) { @strongify(self) [self.navigationController popViewControllerAnimated:YES]; }]; } } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; self.navigationController.navigationBar.hidden = YES; } - (void)setYm_navigationStyle:(YMBaseNavigationStyle)ym_navigationStyle{ _ym_navigationStyle = ym_navigationStyle; switch (ym_navigationStyle) { case YMBaseNavigationStyleClearBgWhiteBackArrow: { NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init]; paragraphStyle.alignment = NSTextAlignmentCenter; paragraphStyle.lineSpacing = 1; NSDictionary *attributes= @{ NSForegroundColorAttributeName:UIColor.whiteColor, NSParagraphStyleAttributeName:paragraphStyle }; if (@available(iOS 15.0, *)) { UINavigationBarAppearance *barApp = [UINavigationBarAppearance new]; barApp.backgroundColor = UIColor.clearColor; barApp.backgroundEffect = nil; barApp.titleTextAttributes = attributes; barApp.shadowColor = nil; [self.ym_customNavView.ym_customNavBar setScrollEdgeAppearance:barApp]; [self.ym_customNavView.ym_customNavBar setStandardAppearance:barApp]; }else{ [self.ym_customNavView.ym_customNavBar setShadowImage:[UIImage new]]; [self.ym_customNavView.ym_customNavBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault]; [self.ym_customNavView.ym_customNavBar setTitleTextAttributes:attributes]; } self.ym_customNavView.backgroundColor = UIColor.clearColor; self.ym_customNavView.ym_customNavBar.backgroundColor = UIColor.clearColor; self.ym_customNavView.ym_customNavBar.translucent = YES; if (self.navigationController.viewControllers.count > 1 && !self.isHideLeftItem) { @weakify(self) [self setLeftBarButtonWithImage:@"ym_navigation_white_icon" block:^(id _Nonnull sender) { @strongify(self) [self.navigationController popViewControllerAnimated:YES]; }]; } self.ym_customNavView.ym_customNavBarLine.hidden = YES; } break; case YMBaseNavigationStyleClearBgBlackBackArrow: { NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init]; paragraphStyle.alignment = NSTextAlignmentCenter; paragraphStyle.lineSpacing = 1; NSDictionary *attributes= @{ NSForegroundColorAttributeName:UIColor.blackColor, NSParagraphStyleAttributeName:paragraphStyle }; if (@available(iOS 15.0, *)) { UINavigationBarAppearance *barApp = [UINavigationBarAppearance new]; barApp.backgroundColor = UIColor.clearColor; barApp.backgroundEffect = nil; barApp.titleTextAttributes = attributes; barApp.shadowColor = nil; [self.ym_customNavView.ym_customNavBar setScrollEdgeAppearance:barApp]; [self.ym_customNavView.ym_customNavBar setStandardAppearance:barApp]; }else{ [self.ym_customNavView.ym_customNavBar setShadowImage:[UIImage new]]; [self.ym_customNavView.ym_customNavBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault]; [self.ym_customNavView.ym_customNavBar setTitleTextAttributes:attributes]; } self.ym_customNavView.backgroundColor = UIColor.clearColor; self.ym_customNavView.ym_customNavBar.backgroundColor = UIColor.clearColor; self.ym_customNavView.ym_customNavBar.translucent = YES; if (self.navigationController.viewControllers.count > 1 && !self.isHideLeftItem) { @weakify(self) [self setLeftBarButtonWithImage:@"ym_navigation_black_icon" block:^(id _Nonnull sender) { @strongify(self) [self.navigationController popViewControllerAnimated:YES]; }]; } self.ym_customNavView.ym_customNavBarLine.hidden = YES; } break; default: { self.ym_customNavView.ym_customNavBar.barTintColor = UIColor.whiteColor; self.ym_customNavView.backgroundColor = UIColor.whiteColor; NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init]; paragraphStyle.alignment = NSTextAlignmentCenter; paragraphStyle.lineSpacing = 1; NSDictionary *attributes= @{ NSForegroundColorAttributeName:UIColor.blackColor, NSParagraphStyleAttributeName:paragraphStyle }; [self.ym_customNavView.ym_customNavBar setTitleTextAttributes:attributes]; if (self.navigationController.viewControllers.count > 1 && !self.isHideLeftItem) { @weakify(self) [self setLeftBarButtonWithImage:@"ym_navigation_black_icon" block:^(id _Nonnull sender) { @strongify(self) [self.navigationController popViewControllerAnimated:YES]; }]; } self.ym_customNavView.ym_customNavBarLine.hidden = YES; } break; } } - (void)setNavHidden:(BOOL)hidden { [self.ym_customNavView setHidden:hidden]; } - (void)setTitle:(NSString *)title { self.ym_customNavView.ym_customNavItem.title = title; } - (NSString *)title { return self.ym_customNavView.ym_customNavItem.title; } /** 添加控件 最好最后加上[self.view setNeedsUpdateConstraints]和[self.view updateConstraintsIfNeeded],以防不调用updateViewConstraints*/ - (void)ym_setupViews { } /** 绑定VM*/ - (void)ym_bindViewModel { } - (void)headerRefreshing{ } - (void)footerRefreshing{ } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ [self.view endEditing:YES]; } - (YMBaseCustomNavView *)ym_customNavView { if (!_ym_customNavView) { _ym_customNavView = [[YMBaseCustomNavView alloc] init]; _ym_customNavView.backgroundColor = UIColor.whiteColor; } return _ym_customNavView; } - (MJRefreshNormalHeader *)refreshHeader{ if(!_refreshHeader){ // 设置回调(一旦进入刷新状态,就调用target的action,也就是调用self的headerRefreshing方法) _refreshHeader = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefreshing)]; } return _refreshHeader; } - (MJRefreshAutoNormalFooter *)loadMoreFooter{ if(!_loadMoreFooter){ // 设置回调(一旦进入刷新状态,就调用target的action,也就是调用self的footerRefreshing方法) _loadMoreFooter = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefreshing)]; _loadMoreFooter.triggerAutomaticallyRefreshPercent = 0.1; } return _loadMoreFooter; } - (MJRefreshAutoNormalFooter *)noLoadMoreFooter{ if(!_noLoadMoreFooter){ _noLoadMoreFooter = [[MJRefreshAutoNormalFooter alloc]initWithFrame:CGRectZero]; _noLoadMoreFooter.backgroundColor = [UIColor clearColor]; _noLoadMoreFooter.state = MJRefreshStateIdle; _noLoadMoreFooter.mj_h = 0;//重新设置高度,不让看到它,并且当设置为table.footer时,table会自动调整自己的高度 _noLoadMoreFooter.clipsToBounds = YES;//不让看不到菊花 } return _noLoadMoreFooter; } @end @implementation YMBaseViewController (YMNavBar) /** 左Bar按钮+标题*/ - (void)setLeftBarButtonWithTitle:(NSString *)title block:(void (^)(id sender))block { self.ym_customNavView.ym_customNavItem.leftBarButtonItem = [self barButtonWithPosition:YES title:title block:block]; } /** 右Bar按钮+标题*/ - (void)setRightBarButtonWithTitle:(NSString *)title block:(void (^)(id sender))block { self.ym_customNavView.ym_customNavItem.rightBarButtonItem = [self barButtonWithPosition:NO title:title block:block]; } /** 左Bar按钮+图片*/ - (void)setLeftBarButtonWithImage:(NSString *)image block:(void (^)(id sender))block { [self setLeftBarButtonWithNormalImage:image highlightedImage:nil block:block]; } /** 右Bar按钮+图片*/ - (void)setRightBarButtonWithImage:(NSString *)image block:(void (^)(id sender))block { [self setRightBarButtonWithNormalImage:image highlightedImage:nil block:block]; } /** 左Bar按钮+选中图片+未选中图片*/ - (void)setLeftBarButtonWithNormalImage:(NSString *)normal highlightedImage:(NSString *)highlighted block:(void (^)(id sender))block { self.ym_customNavView.ym_customNavItem.leftBarButtonItem = [self barButtonWithPosition:YES normalImage:normal highlightedImage:highlighted block:block]; } /** 右Bar按钮+选中图片+未选中图片*/ - (void)setRightBarButtonWithNormalImage:(NSString *)normal highlightedImage:(NSString *)highlighted block:(void (^)(id sender))block { self.ym_customNavView.ym_customNavItem.rightBarButtonItem = [self barButtonWithPosition:NO normalImage:normal highlightedImage:highlighted block:block]; } /** 左Bar多个按钮*/ - (void)setLeftBarButtonWithButtons:(NSArray *)buttons { self.ym_customNavView.ym_customNavItem.leftBarButtonItems = [self barItemsWithPosition:YES buttons:buttons]; } /** 右Bar多个按钮*/ - (void)setRightBarButtonWithButtons:(NSArray *)buttons { self.ym_customNavView.ym_customNavItem.rightBarButtonItems = [self barItemsWithPosition:NO buttons:buttons]; } /** 左Bar自定义View*/ - (void)setLeftBarButtonWithCustomView:(UIView *)customView { self.ym_customNavView.ym_customNavItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:customView]; } /** 右Bar自定义View*/ - (void)setRightBarButtonWithCustomView:(UIView *)customView { self.ym_customNavView.ym_customNavItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:customView]; } #pragma mark - private method - (UIBarButtonItem *)barButtonWithPosition:(BOOL)onLeft title:(NSString *)title block:(void (^)(id sender))block { UIButton *navBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [navBtn setBounds:CGRectMake(0, 0, 70, 44)]; [navBtn setTitle:title forState:UIControlStateNormal]; [navBtn setTitleColor:HexColorFromRGB(0x616161) forState:UIControlStateNormal]; [navBtn setTitleColor:HexColorFromRGB(0x616161) forState:UIControlStateHighlighted]; navBtn.titleLabel.font = [UIFont systemFontOfSize:16]; navBtn.backgroundColor = [UIColor clearColor]; if (onLeft) { navBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; } else { navBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight; } if (block) { [navBtn setBlockForControlEvents:UIControlEventTouchUpInside block:block]; } return [[UIBarButtonItem alloc] initWithCustomView:navBtn]; } - (UIBarButtonItem *)barButtonWithPosition:(BOOL)onLeft image:(UIImage *)image block:(void (^)(id sender))block { UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setBounds:CGRectMake(0, 0, 44, 44)]; [button setImage:image forState:UIControlStateNormal]; button.backgroundColor = [UIColor clearColor]; if (onLeft) { button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; } else { button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight; } if (block) { [button setBlockForControlEvents:UIControlEventTouchUpInside block:block]; } return [[UIBarButtonItem alloc] initWithCustomView:button]; } - (UIBarButtonItem *)barButtonWithPosition:(BOOL)onLeft normalImage:(NSString *)normal highlightedImage:(NSString *)highlighted block:(void (^)(id sender))block { UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setBounds:CGRectMake(0, 0, 44, 44)]; [button setImage:[UIImage imageNamed:normal] forState:UIControlStateNormal]; [button setImage:[UIImage imageNamed:highlighted ?: normal] forState:UIControlStateHighlighted]; button.backgroundColor = [UIColor clearColor]; if (onLeft) { button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; } else { button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight; } if (block) { [button setBlockForControlEvents:UIControlEventTouchUpInside block:block]; } return [[UIBarButtonItem alloc] initWithCustomView:button]; } - (NSArray *)barItemsWithPosition:(BOOL)onLeft buttons:(NSArray *)buttons { NSMutableArray *barItems = [NSMutableArray array]; for (UIView *tmpView in buttons) { UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:tmpView]; UIBarButtonItem *negativeSeperator = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; negativeSeperator.width = 14; [barItems addObject:negativeSeperator]; [barItems addObject:item]; } return [barItems copy]; } @end