继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

iOS学习笔记--PresentedVC自定义弹窗

ZKReadStone
关注TA
已关注
手记 52
粉丝 32
获赞 322

一 、封装自定义弹窗有一下几种:

1 直接在当前视图控制器上放view(简直6翻了)

2 present到一个新的半透明视图控制器(类似UIAlertViewController,也就是说咱们要用的就是个控制器而不是个View了)

3 使用一个windowLevel更高的UIWindow(UIAlertView就是这种)

4 放在keyWindow上(使用这种方式有隐患,点击查看详情

5 放在[UIApplication sharedApplication] delegate] window]上

二 看看效果

图片描述

我选择这种方法的原因是简单方便,有复杂交互的也可以,那就是两个控制器之间的传值了。

这里上代码:


#import "BCAlertViewController.h"

@interface BCAlertViewController ()
@property (weak, nonatomic) IBOutlet UIView *alertView0;
@property (weak, nonatomic) IBOutlet UILabel *textContent;
@property (weak, nonatomic) IBOutlet UIButton *closeBtn;

@end

@implementation BCAlertViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//标注方法setCornerRadious:4 borderColor:nil borderWidth:0  ①
    [self.alertView0 setCornerRadious:4 borderColor:nil borderWidth:0];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (IBAction)closeBtnClicked:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end

//方法①的注释 自己写Category

#import "UIView+CornerRadious.h"

@implementation UIView (CornerRadious)

//设置圆角
- (void)setCornerRadious:(CGFloat)cornerRadious borderColor:(UIColor *)borderColor borderWidth:(CGFloat)borderWidth
{
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:cornerRadious];
    CAShapeLayer *maskLayer= [[CAShapeLayer alloc]init];
    maskLayer.frame = self.bounds;
    maskLayer.path = path.CGPath;

    CAShapeLayer *borderLayer = [[CAShapeLayer alloc]init];
    borderLayer.lineWidth = borderWidth;
    borderLayer.strokeColor = borderColor.CGColor;
    borderLayer.fillColor = ClearColor.CGColor;
    borderLayer.frame = self.bounds;
    borderLayer.path = path.CGPath;

    [self.layer insertSublayer:borderLayer atIndex:0];
    self.layer.mask = maskLayer;
}

///使用方法

- (IBAction)registerBtnClicked:(id)sender {

    BCAlertViewController *alert = [[BCAlertViewController alloc]init];
    alert.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    alert.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self.navigationController presentViewController:alert animated:YES completion:nil];

}

注意:BCAlertViewController.h 的view的背景色设置

    [[UIColor blackColor]colorWithAlphaComponent:0.2];
打开App,阅读手记
2人推荐
发表评论
随时随地看视频慕课网APP