iOS 模态窗口居中弹出背景半透明
来源:互联网 发布:淘宝旺铺只能装修手机 编辑:程序博客网 时间:2024/06/15 18:18
实现功能:点击UIImageView弹出一个模态窗口并居中显示,背景为灰色半透明
App中有很多地方用到点击用户头像并弹出一个模态窗体,显示该用户的一些信息,考虑到很多地方使用,就写了一个UIImageView的类别,将点击手势封装到这个类别中,已达到共用代码的效果。github上有很多模态窗体库
实现思路:先将当前视图控制器的视图设置为透明[self.view.backgroundColor clearColor],然后将一个灰色半透明UIView(grayAlphaView)添加到self.view
, [self.view addSubview:grayAlphaView]; 再将一个容器视图containerView(UIView) 添加到灰色半透明视图上grayAlphaView
[grayAlphaView addSubview:containerView];
, [self.view addSubview:grayAlphaView]; 再将一个容器视图containerView(UIView) 添加到灰色半透明视图上grayAlphaView
[grayAlphaView addSubview:containerView];
1.UIImageView+UserHeadTap
@interface UIImageView (UserHeadTap)- (id) initWithTap;@end
#import "UIImageView+UserHeadTap.h"#import "NameCardViewController.h"#import "UIViewController+Utils.h"@implementation UIImageView (UserHeadTap)- (id) initWithTap { if (self = [super init]) { self.userInteractionEnabled = YES; UITapGestureRecognizer * tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(userHeaderTapAction:)]; [self addGestureRecognizer:tapGestureRecognizer]; } return self;}- (void) userHeaderTapAction:(UITapGestureRecognizer *)tapGestureRecognizer { UIViewController * parentViewController = [UIViewController currentViewController]; NameCardViewController * controller = [[NameCardViewController alloc] init]; controller.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) { controller.providesPresentationContextTransitionStyle = YES; controller.definesPresentationContext = YES; controller.modalPresentationStyle = UIModalPresentationOverCurrentContext; [parentViewController presentViewController:controller animated:YES completion:nil]; } else { parentViewController.view.window.rootViewController.modalPresentationStyle = UIModalPresentationCurrentContext; [parentViewController presentViewController:controller animated:NO completion:nil]; parentViewController.view.window.rootViewController.modalPresentationStyle = UIModalPresentationFullScreen; }}@end
2.ViewController
#import "UIViewController+Utils.h"@implement ViewController - (void)viewDidLoad { UIImageView * userHeadImageView = [UIImageView initWithTap]; userHeadImageView.frmae = CGRectMake(50, 50, 70, 70); userHeadImageView.image = [UIImage imageNamed:@"icon"]; [self.view addSubview:userHeadImageView];}@end
3. NameCardViewController(名片)
#import "NameCardViewController.h"@interface NameCardViewController ()@end
@implementation NameCardViewController- (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor clearColor]; UIView *grayAlphaView = [[UIView alloc] initWithFrame:self.view.frame]; grayAlphaView.backgroundColor = [UIColor grayColor]; grayAlphaView.alpha = 0.8; [self.view addSubview:grayAlphaView]; UIView *containerView = [[UIView alloc] init]; containerView.size = CGSizeMake(self.view.size.width - 40 * 2, 350); containerView.center = self.view.center; containerView.backgroundColor = [UIColor redColor]; [grayAlphaView addSubview:containerView];}@end
4. 实现效果如果
该效果还没有实现全屏
0 0
- iOS 模态窗口居中弹出背景半透明
- CSS弹出背景半透明窗口
- 弹出窗口,页面背景逐渐变为半透明
- ios 模态半透明弹出框
- 模态与push导致弹出半透明背景不同
- JavaScript 弹出登陆窗口背景半透明可拖动效果
- 模态背景半透明
- popuwindow弹出时,背景半透明
- popupwindow 底部弹出+背景半透明
- 弹出窗口悬浮,背景页面变灰,类似网易邮箱的半透明信息提示效果
- 弹出窗口悬浮,背景页面变灰,类似网易邮箱的半透明信息提示效果
- 窗口弹出居中显示
- 弹出窗口居中
- 弹出窗口居中显示
- js弹出窗口居中
- 【js】弹出窗口居中
- 弹出窗口居中
- js 弹出窗口居中
- IOS-事务在数据库中的用处
- Linux下线程的调度策略与优先级
- php函数
- OpenCV中利用cvConvertScale()对图像数据作线性变换~
- js,ajax验证编号是否存在,input只能输入数字,form表单验证
- iOS 模态窗口居中弹出背景半透明
- jsessionid 如何去掉
- 利用spring-websocket包搭建websocket服务
- MySQL修改root密码的多种方法
- BZOJ 2728: [HNOI2012]与非
- 保留两位小数
- uart stdio的移植3
- 前端工程化:云构建
- C++设计模式<二>:面向对象设计原则