iOS學習筆記--PresentedVC自定義彈窗

zybing發表於2021-09-09

一 、封裝自定義彈窗有一下幾種:

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 *)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];

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/855/viewspace-2799745/,如需轉載,請註明出處,否則將追究法律責任。

相關文章