实现类似系统自带的提示框覆盖的效果

本文详细介绍了iOS开发中如何通过按钮点击事件触发视图弹出与隐藏的操作,包括创建视图、设置视图属性、添加视图到父视图以及使用动画效果实现平滑过渡。此外,还分享了一个遇到的问题及其解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    

    UIButton * bt = [UIButton buttonWithType:UIButtonTypeContactAdd];

    bt.frame = CGRectMake(100, 100, 100, 100);

    [self.view addSubview:bt];

    [bt addTarget:self action:@selector(btTTTClic:) forControlEvents:UIControlEventTouchUpInside];

}

- (void)btTTTClic:(UIButton * )bt

{

    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

    view.tag = 200;

    view.backgroundColor = [UIColor grayColor];

    view.alpha = 0.8;


    

    [[UIApplication sharedApplication].delegate.window.rootViewController.view addSubview:view];

    

    UIButton * btt = [UIButton buttonWithType:UIButtonTypeContactAdd];

    btt.frame = CGRectMake(100, 100, 100, 100);

    [view addSubview:btt];

    [btt addTarget:self action:@selector(btTTTCli:) forControlEvents:UIControlEventTouchUpInside];

    

}

- (void)btTTTCli:(UIButton *)bt

{

    UIView * view = [[UIApplication sharedApplication].delegate.window.rootViewController.view viewWithTag:200];

[UIView animateWithDuration:0.2 animations:^{

    [view setFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width, 0)];

} completion:^(BOOL finished) {

    [view removeFromSuperview];

}];

}


核心代码 其实就一句:[[UIApplication sharedApplication].delegate.window.rootViewController.viewaddSubview:view];

和大家分享一下,同时请教大家一个问题 我敲代码的时候 部分代码不知道为什么没有提示 希望大家能留言帮忙解决一下!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值