BottomBasePopupController.m 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. //
  2. // BottomBasePopupController.m
  3. // mask
  4. //
  5. // Created by 翟玉磊 on 2018/12/24.
  6. // Copyright © 2018 翟玉磊. All rights reserved.
  7. //
  8. #import "BottomBasePopupController.h"
  9. #define RowHeight 50.0f
  10. @interface BottomBasePopupController ()<UITableViewDelegate, UITableViewDataSource>
  11. {
  12. CGRect _showRect;
  13. CGRect _hiddenRect;
  14. }
  15. @property (nonatomic, readwrite, strong) UIView *bgView;
  16. @property (nonatomic, readwrite, strong) UIView *titleView;
  17. @property (nonatomic, readwrite, strong) UILabel *titleLabel;
  18. @property (nonatomic, readwrite, strong) UIView *titleLineView;
  19. @property (nonatomic, readwrite, strong) UITableView *tableView;
  20. @end
  21. static NSString * const cellId = @"UITableViewCellId";
  22. @implementation BottomBasePopupController
  23. - (instancetype)initWithTitle:(NSString *)title categoryTitles:(NSArray *)titles target:(UIViewController *)target selctedBlock:(void(^)(NSInteger index))block {
  24. if (self = [super init]) {
  25. self.titleString = [title copy];
  26. self.dataArray = [NSArray arrayWithArray:titles];
  27. self.parentController = target;
  28. self.selectedDidBlock = block;
  29. self.view.frame = [UIScreen mainScreen].bounds;
  30. self.view.backgroundColor = Color_Clear;
  31. }
  32. return self;
  33. }
  34. - (void)viewDidLoad {
  35. [super viewDidLoad];
  36. /// 设置
  37. [self _setup];
  38. /// 设置导航栏
  39. [self _setupNavigationItem];
  40. /// 设置子控件
  41. [self _setupSubViews];
  42. /// 布局子空间
  43. [self _makeSubViewsConstraints];
  44. }
  45. - (void)show {
  46. UIViewController *appRootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
  47. UIViewController *topVC = appRootVC;
  48. if (topVC.presentedViewController) {
  49. topVC = topVC.presentedViewController;
  50. if ([topVC isKindOfClass:[UINavigationController class]]) {
  51. UINavigationController *nav = (UINavigationController *)topVC;
  52. [nav.topViewController addChildViewController:self];
  53. [nav.topViewController.view addSubview:self.view];
  54. }else {
  55. [topVC addChildViewController:self];
  56. [topVC .view addSubview:self.view];
  57. }
  58. }else {
  59. [appRootVC addChildViewController:self];
  60. [appRootVC.view addSubview:self.view];
  61. }
  62. [UIView animateWithDuration:.3 animations:^{
  63. self.tableView.frame = self->_showRect;
  64. } completion:^(BOOL finished) {
  65. }];
  66. }
  67. - (void)showWithController:(UIViewController *)controller {
  68. [controller addChildViewController:self];
  69. [controller.view addSubview:self.view];
  70. [UIView animateWithDuration:.3 animations:^{
  71. self.tableView.frame = self->_showRect;
  72. } completion:^(BOOL finished) {
  73. }];
  74. }
  75. - (void)dismiss {
  76. [UIView animateWithDuration:.3 animations:^{
  77. self.tableView.frame = self->_hiddenRect;
  78. } completion:^(BOOL finished) {
  79. [self.view removeFromSuperview];
  80. [self removeFromParentViewController];
  81. }];
  82. }
  83. #pragma mark - 事件处理Or辅助方法
  84. - (void)bgViewAction:(id)sender {
  85. if (self.cancelActionBlock) {
  86. self.cancelActionBlock();
  87. }
  88. [self dismiss];
  89. }
  90. #pragma mark - UITableViewDelegate
  91. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  92. return _dataArray.count;
  93. }
  94. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  95. return RowHeight;
  96. }
  97. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  98. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
  99. if (cell == nil) {
  100. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
  101. }
  102. cell.contentView.backgroundColor = Color_White;
  103. cell.textLabel.font = Font(14);
  104. cell.textLabel.textColor = Color_TextFont;
  105. cell.textLabel.text = _dataArray[indexPath.row];
  106. return cell;
  107. }
  108. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  109. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  110. if (self.selectedDidBlock) {
  111. self.selectedDidBlock(indexPath.row);
  112. }
  113. /// dismiss
  114. [self dismiss];
  115. }
  116. /// 处理系统cell分割线顶到头
  117. - (void)tableView:(UITableView*)tableView willDisplayCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexPath {
  118. [cell setSeparatorInset:UIEdgeInsetsZero];
  119. [cell setLayoutMargins:UIEdgeInsetsZero];
  120. cell.preservesSuperviewLayoutMargins = NO;
  121. }
  122. #pragma mark - 初始化
  123. - (void)_setup{
  124. }
  125. #pragma mark - 设置导航栏
  126. - (void)_setupNavigationItem{
  127. }
  128. #pragma mark - 设置子控件
  129. - (void)_setupSubViews{
  130. [self.view addSubview:self.bgView];
  131. [self.view addSubview:self.tableView];
  132. if (StringIsNotEmpty(_titleString)) {
  133. self.tableView.tableHeaderView = self.titleView;
  134. [self.titleView addSubview:self.titleLabel];
  135. [self.titleView addSubview:self.titleLineView];
  136. self.titleLabel.text = self.titleString;
  137. }
  138. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bgViewAction:)];
  139. [self.bgView addGestureRecognizer:tap];
  140. }
  141. #pragma mark - 布局子控件
  142. - (void)_makeSubViewsConstraints{
  143. self.bgView.frame = self.view.bounds;
  144. CGFloat tableViewHeight = self.dataArray.count * RowHeight;
  145. if (StringIsNotEmpty(_titleString)) {
  146. tableViewHeight = tableViewHeight + 50.0f;
  147. }
  148. _hiddenRect = CGRectMake(0, self.view.f_heigh, self.view.f_width, tableViewHeight);
  149. _showRect = CGRectMake(0, self.view.f_heigh - tableViewHeight - HOME_INDICATOR_HEIGHT, self.view.f_width, tableViewHeight);
  150. self.tableView.frame = _hiddenRect;
  151. self.titleView.frame= CGRectMake(0, 0, self.tableView.f_width, 50.0f);
  152. self.titleLabel.frame = CGRectMake(15.0f, 15.0f, self.titleView.f_width - 30.0f, 20.0f);
  153. self.titleLineView.frame =CGRectMake(0, self.titleView.f_heigh - 0.5f, self.titleView.f_width, 0.5f);
  154. }
  155. #pragma mark - Setter & Getter
  156. - (UIView *)bgView {
  157. if (_bgView == nil) {
  158. _bgView = [[UIView alloc] init];
  159. _bgView.backgroundColor = Color_Black;
  160. _bgView.alpha = 0.4f;
  161. _bgView.userInteractionEnabled = YES;
  162. }
  163. return _bgView;
  164. }
  165. - (UIView *)titleView {
  166. if (_titleView == nil) {
  167. _titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50.0f)];
  168. _titleView.backgroundColor = Color_White;
  169. }
  170. return _titleView;
  171. }
  172. - (UILabel *)titleLabel {
  173. if (_titleLabel == nil) {
  174. _titleLabel = [UILabel new];
  175. _titleLabel.font = Font(15);
  176. _titleLabel.textColor = Color_TextFont;
  177. _titleLabel.textAlignment = NSTextAlignmentCenter;
  178. }
  179. return _titleLabel;
  180. }
  181. - (UIView *)titleLineView {
  182. if (_titleLineView == nil) {
  183. _titleLineView = [[UIView alloc] init];
  184. _titleLineView.backgroundColor = Color_line;
  185. }
  186. return _titleLineView;
  187. }
  188. - (UITableView *)tableView {
  189. if (_tableView == nil) {
  190. _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.f_width, 150.0f) style:UITableViewStylePlain];
  191. _tableView.delegate = self;
  192. _tableView.dataSource = self;
  193. _tableView.backgroundColor = Color_White;
  194. _tableView.bounces = NO;
  195. [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellId];
  196. }
  197. return _tableView;
  198. }
  199. @end