如何present出一个透明导航控制器NavigationController + UIVisualEffectView实现模糊效果、毛玻璃效果

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/qq_15509071/article/details/70799678


在实际需求中使用模糊效果一般是: 原有界面(保持不变)+ 毛玻璃效果 + 需要展示的界面(上方)。


即:



1.新建工程 ,设置ViewController的背景图(淘宝首页截图),添加按钮及其事件

- (void)viewDidLoad {
    
    [super viewDidLoad];

    _imageView.frame = [UIScreen mainScreen].bounds;

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.backgroundColor = [UIColor orangeColor];
    btn.frame = CGRectMake(10, SCREEN_HEIGHT_NEW - 60, SCREEN_WIDTH_NEW - 10 * 2, 40);
    [self.view addSubview:btn];
    [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitle:@"点击展示模糊" forState:UIControlStateNormal];
    
}

- (void)click:(UIButton*)btn{
    
    MyTopViewController *topVC = [[MyTopViewController alloc]init];
    UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:topVC];
    //present出一个透明导航控制器NavigationController
    navi.modalPresentationStyle = UIModalPresentationCustom;
    [self presentViewController:navi animated:YES completion:^{}];
        
}

2在MyTopViewController中添加毛玻璃效果

- (void)viewDidLoad {
    [super viewDidLoad];

    //只有透明才能看到下方界面
    self.view.backgroundColor = [UIColor clearColor];
    
    
    //实现模糊效果
    UIVisualEffectView *visualEffectView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight]];

    visualEffectView.frame = self.view.bounds;

    visualEffectView.alpha = 0.88;

    [self.view addSubview:visualEffectView];
    

    
    UITextField *textFiled = [[UITextField alloc]initWithFrame:CGRectMake(20, 200, SCREEN_WIDTH_NEW - 20*2, 40)];
    [self.view addSubview:textFiled];
    textFiled.placeholder = @"请输入手机号";
    textFiled.tintColor = [UIColor purpleColor];;
    textFiled.layer.borderWidth = 1;
    textFiled.layer.borderColor = [UIColor purpleColor].CGColor;
    
    
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.backgroundColor = [UIColor orangeColor];
    btn.frame = CGRectMake(10, 170 + 100, SCREEN_WIDTH_NEW - 10 * 2, 50);
    [btn setTitle:@"下一步" forState:UIControlStateNormal];
    [self.view addSubview:btn];
    [btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    
    
}

-(void)click{
    UIViewController *vc = [[UIViewController alloc]init];
    vc.view.backgroundColor = [UIColor grayColor];
    [self.navigationController pushViewController:vc animated:YES];
}


下载地址:http://download.csdn.net/detail/qq_15509071/9826691

点击打开链接



展开阅读全文

没有更多推荐了,返回首页