iOS自定义弹出窗口

先看示例
最终效果图.gif
首先搭建好基础界面
- (void)viewDidLoad {
    [super viewDidLoad];
    //设置背景色
    self.view.backgroundColor = [UIColor yellowColor];
    //弹出按钮
    UIButton *alertBt = [UIButton buttonWithType:UIButtonTypeSystem];
    alertBt.frame = CGRectMake(100, 100, 100, 100);
    [alertBt setTitle:@"弹出" forState:UIControlStateNormal];
    [alertBt addTarget:self action:@selector(alertAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:alertBt];
    
}
//弹出按钮点击事件
- (void)alertAction{
//这里我创建了一个AlertViewController  继承UIViewController  这是我们最基本的弹出方式,效果你们也可以想象,我就不放图了.
    [self presentViewController:[AlertViewController new] animated:YES completion:nil];

}
然后我们就要在AlertViewController里面做事情了

ps:大部分是布局代码,看看前面几句就可以跳过看下一步

- (void)viewDidLoad {
    [super viewDidLoad];
    //为了弹出框效果,我们当然要把背景透明
    self.view.backgroundColor = [UIColor clearColor];
    //这里开始都是布局代码  可以根据界面效果阅读,不读也没什么影响  我这里定义了两个宏一个bigWid 一个bigHei 分别是屏幕的宽和高
    UIView *myView = [UIView new];
    myView.frame = CGRectMake(15, bigHei-175, bigWid-30, 160);
    myView.backgroundColor = [UIColor whiteColor];
    myView.layer.cornerRadius = 5;
    [self.view addSubview:myView];
    
    //分享微信 和朋友圈按钮
    UIButton *bt1 = [UIButton buttonWithType:UIButtonTypeSystem];
    [bt1 setBackgroundImage:[UIImage imageNamed:@"weChat"] forState:UIControlStateNormal];
    [bt1 setTitle:@"分享给好友" forState:UIControlStateNormal];
    bt1.titleLabel.font = [UIFont systemFontOfSize:10];
    [bt1 setTitleEdgeInsets:UIEdgeInsetsMake(40, 0, -40, 0)];
    bt1.tintColor = [UIColor grayColor];
    bt1.frame = CGRectMake(40, 30, 50, 50);
    [bt1 sizeToFit];
    [myView addSubview:bt1];
    
    UIButton *bt2 = [UIButton buttonWithType:UIButtonTypeSystem];
    [bt2 setBackgroundImage:[UIImage imageNamed:@"share"] forState:UIControlStateNormal];
    [bt2 setTitle:@"朋友圈分享" forState:UIControlStateNormal];
    bt2.titleLabel.font = [UIFont systemFontOfSize:10];
    [bt2 setTitleEdgeInsets:UIEdgeInsetsMake(40, 0, -40, 0)];
    bt2.tintColor = [UIColor grayColor];
    bt2.frame = CGRectMake(bigWid/2-35, 30, 50, 50);
    [bt2 sizeToFit];
    [myView addSubview:bt2];
    
    
    
    UIButton *bt3 = [UIButton buttonWithType:UIButtonTypeSystem];
    [bt3 setBackgroundImage:[UIImage imageNamed:@"box"] forState:UIControlStateNormal];

    [bt3 setTitle:@"收藏" forState:UIControlStateNormal];
    
    bt3.titleLabel.font = [UIFont systemFontOfSize:10];
    [bt3 setTitleEdgeInsets:UIEdgeInsetsMake(40, 0, -40, 0)];
    bt3.tintColor = [UIColor grayColor];
    bt3.frame = CGRectMake(bigWid - 110, 30, 50, 50);
    [myView addSubview:bt3];
   
    
    
    //分割线
    UIView *deView = [[UIView alloc]initWithFrame:CGRectMake(0, 120, bigWid - 30, 1)];
    deView.backgroundColor = [UIColor colorWithRed:225/255.0 green:225/255.0 blue:225/255.0 alpha:1.0];
    [myView addSubview:deView];
    
    //取消按钮
    UIButton *cancellBt = [UIButton buttonWithType:UIButtonTypeSystem];
    [cancellBt setTitle:@"取消" forState:UIControlStateNormal];
    cancellBt.frame = CGRectMake(15, 120, bigWid - 60, 40);
    [myView addSubview:cancellBt];
    [cancellBt addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    
}
//返回按钮
- (void)backAction{
    [self dismissViewControllerAnimated:YES completion:nil];
}

然后效果嘛,当然是

gif.gif

因为默认的present效果并不是覆盖,是切换.显示的黑色背景是Window的背景颜色.

所以下一步我们就要修改modalPresentStyle

先回到ViewController

//在弹出方法中设置跳转模式
- (void)alertAction{
    AlertViewController *vc = [AlertViewController new];
    //这里设置目的视图控制器的模式 不是self  
    vc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    [self presentViewController:vc animated:YES completion:nil];
}

//为了逼真,再加一层阴影

//这个可以写在viewDidLoad里面
self.shadowView = [[UIView alloc] initWithFrame:self.view.bounds];
    self.shadowView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:self.shadowView];
---------
//在present的之前,先
self.shadowView .alpha = 0.5;
//然后再返回时再讲它的alpha设回0,这里不能用在viewWillApear里面设置,因为他的视图一直可见,所以回来不会触发这个方法.这里可以用block或者代理来实现返回时shadowVIew.alpha = 0;

最终效果图

![Uploading 最终效果图_165272.gif . . .]
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,045评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,114评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,120评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,902评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,828评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,132评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,590评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,258评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,408评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,335评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,385评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,068评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,660评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,747评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,967评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,406评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,970评论 2 341

推荐阅读更多精彩内容