先看下效果图:
直接上代码:
#import "FilterViewController.h"
#define WEAK_SELF(weakSelf) __weak __typeof(&*self)weakSelf = self;
#define ScreenBounds [[UIScreen mainScreen] bounds] //屏幕frame
#define ScreenFullWidth [[UIScreen mainScreen] bounds].size.width //屏幕宽度
@interface ViewController (){
UILabel *showLabel;
}
@property (nonatomic, weak) UIView *upView;
@property (nonatomic, strong) UIWindow *window;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"主页";
[self createBarRightWithImage:@"" :@"筛选"];
showLabel= [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 200, 50)];
showLabel.text = @"主页";
showLabel.textColor = [UIColor blackColor];
[self.view addSubview:showLabel];
}
//点击筛选按钮,出现筛选页面
- (void)showRight:(UIButton *)sender{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenFullWidth, ScreenBounds.size.height)];
view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
[view addGestureRecognizer:tap];
[[self mainWindow] addSubview:view];
self.upView = view;
UIWindow *window = [[UIWindow alloc] initWithFrame:CGRectMake(100, 0, ScreenFullWidth-100, ScreenBounds.size.height)];
window.backgroundColor = [[UIColor clearColor] colorWithAlphaComponent:0.7];
window.windowLevel = UIWindowLevelNormal;
window.hidden = NO;
[window makeKeyAndVisible];
self.window = window;
WEAK_SELF(weakSelf)
FilterViewController *up = [[FilterViewController alloc] init];
up.tap = ^{
weakSelf.window.hidden = YES;//少这句话的就会有问题
[weakSelf.window resignKeyWindow];
[weakSelf.upView removeFromSuperview];
weakSelf.window = nil;
weakSelf.upView = nil;
};
up.clickCell = ^(NSString *chooseStr, NSInteger index){
showLabel.text = [NSString stringWithFormat:@"%@,%ld",chooseStr,(long)index];
};
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:up];
up.view.frame = window.bounds;
window.rootViewController = nav;
}
- (void)tapAction{
[self.window resignKeyWindow];
[self.upView removeFromSuperview];
self.window = nil;
self.upView = nil;
}
看下筛选页面的代码:
//.h文件
#import <UIKit/UIKit.h>
#import "BaseViewController.h"
@interface FilterViewController : BaseViewController<UITableViewDelegate,UITableViewDataSource>
//在灰色部分点击返回上个页面的block
@property(nonatomic,copy)void(^tap)();
//选中cell后在上个页面显示带回去的值的block
@property(nonatomic,copy)void(^clickCell)(NSString *chooseStr, NSInteger index);
@end
//.m文件
#import "FilterViewController.h"
@interface FilterViewController (){
NSArray *arrList;
}
@end
@implementation FilterViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"筛选";
[self createBarLeftWithImage:@"backBtn.png" :@"nil"];
arrList = [NSArray arrayWithObjects:@"大家好",@"你们好",@"他们好",@"哈哈",@"嘻嘻",@"嘎嘎", nil];
[self createTable];
}
- (void)createTable{
UITableView *myTable = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
myTable.delegate = self;
myTable.dataSource = self;
myTable.rowHeight = 44;
[self.view addSubview:myTable];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return arrList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [NSString stringWithFormat:@"%@",arrList[indexPath.row]];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
self.clickCell(arrList[indexPath.row], indexPath.row);
self.tap();
}
//返回按钮
-(void)showLeft:(UIButton *)sender{
self.tap();
}