123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- //
- // BottomBasePopupController.m
- // mask
- //
- // Created by 翟玉磊 on 2018/12/24.
- // Copyright © 2018 翟玉磊. All rights reserved.
- //
- #import "BottomBasePopupController.h"
- #define RowHeight 50.0f
- @interface BottomBasePopupController ()<UITableViewDelegate, UITableViewDataSource>
- {
- CGRect _showRect;
- CGRect _hiddenRect;
- }
- @property (nonatomic, readwrite, strong) UIView *bgView;
- @property (nonatomic, readwrite, strong) UIView *titleView;
- @property (nonatomic, readwrite, strong) UILabel *titleLabel;
- @property (nonatomic, readwrite, strong) UIView *titleLineView;
- @property (nonatomic, readwrite, strong) UITableView *tableView;
- @end
- static NSString * const cellId = @"UITableViewCellId";
- @implementation BottomBasePopupController
- - (instancetype)initWithTitle:(NSString *)title categoryTitles:(NSArray *)titles target:(UIViewController *)target selctedBlock:(void(^)(NSInteger index))block {
-
- if (self = [super init]) {
-
- self.titleString = [title copy];
- self.dataArray = [NSArray arrayWithArray:titles];
- self.parentController = target;
- self.selectedDidBlock = block;
-
- self.view.frame = [UIScreen mainScreen].bounds;
- self.view.backgroundColor = Color_Clear;
- }
- return self;
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- /// 设置
- [self _setup];
-
- /// 设置导航栏
- [self _setupNavigationItem];
-
- /// 设置子控件
- [self _setupSubViews];
-
- /// 布局子空间
- [self _makeSubViewsConstraints];
- }
- - (void)show {
-
- UIViewController *appRootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
- UIViewController *topVC = appRootVC;
- if (topVC.presentedViewController) {
- topVC = topVC.presentedViewController;
- if ([topVC isKindOfClass:[UINavigationController class]]) {
- UINavigationController *nav = (UINavigationController *)topVC;
- [nav.topViewController addChildViewController:self];
- [nav.topViewController.view addSubview:self.view];
- }else {
- [topVC addChildViewController:self];
- [topVC .view addSubview:self.view];
- }
- }else {
- [appRootVC addChildViewController:self];
- [appRootVC.view addSubview:self.view];
- }
-
- [UIView animateWithDuration:.3 animations:^{
- self.tableView.frame = self->_showRect;
- } completion:^(BOOL finished) {
-
- }];
- }
- - (void)showWithController:(UIViewController *)controller {
-
- [controller addChildViewController:self];
- [controller.view addSubview:self.view];
-
- [UIView animateWithDuration:.3 animations:^{
- self.tableView.frame = self->_showRect;
- } completion:^(BOOL finished) {
-
- }];
- }
- - (void)dismiss {
-
- [UIView animateWithDuration:.3 animations:^{
-
- self.tableView.frame = self->_hiddenRect;
- } completion:^(BOOL finished) {
- [self.view removeFromSuperview];
- [self removeFromParentViewController];
- }];
- }
- #pragma mark - 事件处理Or辅助方法
- - (void)bgViewAction:(id)sender {
-
- if (self.cancelActionBlock) {
- self.cancelActionBlock();
- }
- [self dismiss];
- }
- #pragma mark - UITableViewDelegate
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- return _dataArray.count;
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
- return RowHeight;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
-
- if (cell == nil) {
- cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
- }
- cell.contentView.backgroundColor = Color_White;
- cell.textLabel.font = Font(14);
- cell.textLabel.textColor = Color_TextFont;
- cell.textLabel.text = _dataArray[indexPath.row];
- return cell;
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
- [tableView deselectRowAtIndexPath:indexPath animated:YES];
-
- if (self.selectedDidBlock) {
- self.selectedDidBlock(indexPath.row);
- }
- /// dismiss
- [self dismiss];
- }
- /// 处理系统cell分割线顶到头
- - (void)tableView:(UITableView*)tableView willDisplayCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexPath {
- [cell setSeparatorInset:UIEdgeInsetsZero];
- [cell setLayoutMargins:UIEdgeInsetsZero];
- cell.preservesSuperviewLayoutMargins = NO;
-
- }
- #pragma mark - 初始化
- - (void)_setup{
- }
- #pragma mark - 设置导航栏
- - (void)_setupNavigationItem{
-
- }
- #pragma mark - 设置子控件
- - (void)_setupSubViews{
-
- [self.view addSubview:self.bgView];
- [self.view addSubview:self.tableView];
-
- if (StringIsNotEmpty(_titleString)) {
- self.tableView.tableHeaderView = self.titleView;
- [self.titleView addSubview:self.titleLabel];
- [self.titleView addSubview:self.titleLineView];
-
- self.titleLabel.text = self.titleString;
- }
-
- UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bgViewAction:)];
- [self.bgView addGestureRecognizer:tap];
- }
- #pragma mark - 布局子控件
- - (void)_makeSubViewsConstraints{
-
- self.bgView.frame = self.view.bounds;
-
- CGFloat tableViewHeight = self.dataArray.count * RowHeight;
- if (StringIsNotEmpty(_titleString)) {
- tableViewHeight = tableViewHeight + 50.0f;
- }
- _hiddenRect = CGRectMake(0, self.view.f_heigh, self.view.f_width, tableViewHeight);
- _showRect = CGRectMake(0, self.view.f_heigh - tableViewHeight - HOME_INDICATOR_HEIGHT, self.view.f_width, tableViewHeight);
- self.tableView.frame = _hiddenRect;
-
- self.titleView.frame= CGRectMake(0, 0, self.tableView.f_width, 50.0f);
- self.titleLabel.frame = CGRectMake(15.0f, 15.0f, self.titleView.f_width - 30.0f, 20.0f);
- self.titleLineView.frame =CGRectMake(0, self.titleView.f_heigh - 0.5f, self.titleView.f_width, 0.5f);
- }
- #pragma mark - Setter & Getter
- - (UIView *)bgView {
- if (_bgView == nil) {
- _bgView = [[UIView alloc] init];
- _bgView.backgroundColor = Color_Black;
- _bgView.alpha = 0.4f;
- _bgView.userInteractionEnabled = YES;
- }
- return _bgView;
- }
- - (UIView *)titleView {
- if (_titleView == nil) {
- _titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50.0f)];
- _titleView.backgroundColor = Color_White;
- }
- return _titleView;
- }
- - (UILabel *)titleLabel {
- if (_titleLabel == nil) {
- _titleLabel = [UILabel new];
- _titleLabel.font = Font(15);
- _titleLabel.textColor = Color_TextFont;
- _titleLabel.textAlignment = NSTextAlignmentCenter;
- }
- return _titleLabel;
- }
- - (UIView *)titleLineView {
- if (_titleLineView == nil) {
- _titleLineView = [[UIView alloc] init];
- _titleLineView.backgroundColor = Color_line;
- }
- return _titleLineView;
- }
- - (UITableView *)tableView {
- if (_tableView == nil) {
- _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.f_width, 150.0f) style:UITableViewStylePlain];
- _tableView.delegate = self;
- _tableView.dataSource = self;
- _tableView.backgroundColor = Color_White;
- _tableView.bounces = NO;
- [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellId];
- }
- return _tableView;
- }
- @end
|