iOS自定义控件:精简的底部弹框

镇楼图

模仿微信底部弹窗样式。

接口文件

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface WDAlterSheetModel : NSObject
/// 仅显示主标题 黑色 ,16号字体
/// @param title 标题内容
+ (WDAlterSheetModel *)setupWithTitle:(NSString *)title;

/// 主标题(黑色 ,16号字体)和副标题(灰色 ,10号字体)
/// @param title 主标题内容
/// @param subTitle 副标题内容
+ (WDAlterSheetModel *)setupWithTitle:(NSString *)title
                             subTitle:(NSString *)subTitle;


/// 仅显示主标题 16号字体
/// @param title 标题内容
/// @param titleColor 颜色配置
+ (WDAlterSheetModel *)setupWithTitle:(NSString *)title
                           titleColor:(UIColor *)titleColor;


/// 仅显示主标题
/// @param title 主标题内容
/// @param titleColor 颜色配置
/// @param titleFount 字体配置
+ (WDAlterSheetModel *)setupWithTitle:(NSString *)title
                           titleColor:(UIColor *)titleColor
                            titleFont:(UIFont *)titleFount;


/// 主标题和副标题
/// @param title 主标题内容
/// @param titleColor 主标题颜色配置
/// @param titleFont 主标题字体配置
/// @param subTitle 副标题内容
/// @param subTitleColor 副标题字体配置
/// @param subTitleFont 副标题字体配置
+ (WDAlterSheetModel *)setupWithTitle:(NSString *)title
                           titleColor:(UIColor *)titleColor
                            titleFont:(UIFont *)titleFont
                             subTitle:(NSString *)subTitle
                        subTitleColor:(UIColor *)subTitleColor
                         subTitleFont:(UIFont *)subTitleFont;

@end

// 点击回调(取消按钮除外)
typedef void (^DidSelectedBlock)(NSUInteger index);

@interface WDAlterSheetView : UIControl

/// 仅显示按钮 默认样式
/// @param items 按钮文字数组
+ (void)showAlterWithTitleItems:(NSArray<NSString *> *)items
                 didSelectBlock:(DidSelectedBlock)selectBlock;

/// 仅显示按钮 自定义样式按钮
/// @param items 按钮样式模型数组
+ (void)showAlterWithTitleAttItems:(NSArray<WDAlterSheetModel *> *)items
                    didSelectBlock:(DidSelectedBlock)selectBlock;


/// 显示按钮(默认样式) + 取消按钮
/// @param items 按钮文字数组
/// @param cancelText 取消按钮文字
/// @param cancelColor 取消按钮颜色
+ (void)showAlterWithTitleItems:(NSArray<NSString *> *)items
                     cancelText:(NSString *)cancelText
                    cancelColor:(UIColor * _Nullable)cancelColor // 默认颜色红色
                 didSelectBlock:(DidSelectedBlock)selectBlock;

/// 显示按钮(自定义样式) + 取消按钮
/// @param items 按钮样式模型数组
/// @param cancelText 取消按钮文字
/// @param cancelColor 取消按钮颜色
+ (void)showAlterWithTitleAttItems:(NSArray<WDAlterSheetModel *> *)items
                        cancelText:(NSString *)cancelText
                       cancelColor:(UIColor * _Nullable)cancelColor // 默认颜色红色
                    didSelectBlock:(DidSelectedBlock)selectBlock;
@end

NS_ASSUME_NONNULL_END

实现文件

#import "WDAlterSheetView.h"
// 按钮高度
static CGFloat KButtonHeight = 55;
// 取消按钮与其他间距
static CGFloat KSpace = 8;
// 圆角
static CGFloat KCornersSpace = 10;

// 屏幕高度
static CGFloat kScreenHeight() {
    return [[UIScreen mainScreen] bounds].size.height;
}

// 屏幕宽度
static CGFloat kScreenWidth() {
    return [[UIScreen mainScreen] bounds].size.width;
}

// 是否是刘海屏
static BOOL kDevice_iPhoneX() {
    BOOL isPhoneX = NO;
    if (@available(iOS 11.0, *)) {
        isPhoneX = [[UIApplication sharedApplication] delegate].window.safeAreaInsets.bottom > 0.0;
    }
    return isPhoneX;
}

// 主标题颜色
static UIColor * kTitleColor() {
    return [UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:1];
}

// 副标题颜色
static UIColor * kSubTitleColor() {
    return [UIColor colorWithRed:0.90 green:0.90 blue:0.90 alpha:1];
}

// 间隙颜色
static UIColor * kSpaceColor() {
    return [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1];
}

// 分割线颜色
static UIColor * kSpaceLineColor() {
    return [UIColor colorWithRed:0.90 green:0.90 blue:0.90 alpha:1];
}

// 主标题字体
static UIFont * kTitleFont() {
    return [UIFont systemFontOfSize:16];
}

// 副标题字体
static UIFont * kSubTitleFont() {
    return [UIFont systemFontOfSize:12];
}

@interface WDAlterSheetModel ()
// 文字
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subTitle;
// 颜色
@property (nonatomic, strong) UIColor *titleColor;
@property (nonatomic, strong) UIColor *subTitleColor;
// 字体
@property (nonatomic, strong) UIFont *titleFont;
@property (nonatomic, strong) UIFont *subTitleFont;
@end

@implementation WDAlterSheetModel

+ (WDAlterSheetModel *)setupWithTitle:(NSString *)title {
    return [self setupWithTitle:title titleColor:kTitleColor()];
}

+ (WDAlterSheetModel *)setupWithTitle:(NSString *)title
                             subTitle:(NSString *)subTitle {
    
    return [self setupWithTitle:title
                    titleColor:kTitleColor()
                    titleFont:kTitleFont()
                       subTitle:subTitle
                  subTitleColor:kSubTitleColor()
                  subTitleFont:kSubTitleFont()];
}

+ (WDAlterSheetModel *)setupWithTitle:(NSString *)title
                           titleColor:(UIColor *)titleColor {
    return [self setupWithTitle:title titleColor:titleColor titleFont:kTitleFont()];
}

+ (WDAlterSheetModel *)setupWithTitle:(NSString *)title
                           titleColor:(UIColor *)titleColor
                            titleFont:(UIFont *)titleFont {
    
    return [self setupWithTitle:title
                     titleColor:titleColor
                      titleFont:titleFont
                       subTitle:@""
                  subTitleColor:kSubTitleColor()
                   subTitleFont:kSubTitleFont()];
}


+ (WDAlterSheetModel *)setupWithTitle:(NSString *)title
                           titleColor:(UIColor *)titleColor
                            titleFont:(UIFont *)titleFont
                             subTitle:(NSString *)subTitle
                        subTitleColor:(UIColor *)subTitleColor
                         subTitleFont:(UIFont *)subTitleFont {
    WDAlterSheetModel *model = [[WDAlterSheetModel alloc] init];
    model.title = title;
    model.titleColor = titleColor;
    model.titleFont = titleFont;
    model.subTitle = subTitle;
    model.subTitleColor = subTitleColor;
    model.subTitleFont = subTitleFont;
    return model;
}
@end

@interface WDAlterSheetControl : UIControl

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *subTitleLabel;
@property (nonatomic, copy) NSArray<NSLayoutConstraint *> *totalLayoutConstraints;
@property (nonatomic, strong) UIView *bottomLineView;

@end

@implementation WDAlterSheetControl
- (instancetype)init {
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_titleLabel];
        
        _subTitleLabel = [[UILabel alloc] init];
        _subTitleLabel.translatesAutoresizingMaskIntoConstraints = NO;
        _subTitleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_subTitleLabel];
        
        _bottomLineView = [[UIView alloc] init];
        _bottomLineView.backgroundColor = kSpaceLineColor();
        [self addSubview:_bottomLineView];
    
        self.totalLayoutConstraints = @[
            [NSLayoutConstraint constraintWithItem:_titleLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1 constant:0],
            [NSLayoutConstraint constraintWithItem:_titleLabel attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1 constant:-2],
            [NSLayoutConstraint constraintWithItem:_subTitleLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1 constant:0],
            [NSLayoutConstraint constraintWithItem:_subTitleLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1 constant:2],
        ];
        [self addConstraints:self.totalLayoutConstraints];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    _bottomLineView.frame = CGRectMake(0, CGRectGetHeight(self.frame) - 0.5, CGRectGetWidth(self.frame), 0.5);
    
    if (_subTitleLabel.text.length == 0) {
        [self removeConstraints:self.totalLayoutConstraints];
        [self addConstraints:@[
            [NSLayoutConstraint constraintWithItem:_titleLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1 constant:0],
            [NSLayoutConstraint constraintWithItem:_titleLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1 constant:0],
            [NSLayoutConstraint constraintWithItem:_subTitleLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1 constant:0],
            [NSLayoutConstraint constraintWithItem:_subTitleLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1 constant:0],
        ]];
    }
}
@end

@interface WDAlterSheetLastControl : UIControl

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *subTitleLabel;
@property (nonatomic, copy) NSArray<NSLayoutConstraint *> *totalLayoutConstraints;
@property (nonatomic, strong) UIView *bottomView;

@end

@implementation WDAlterSheetLastControl

- (instancetype)init {
    self = [super init];
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];
        
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_titleLabel];
        
        _subTitleLabel = [[UILabel alloc] init];
        _subTitleLabel.translatesAutoresizingMaskIntoConstraints = NO;
        _subTitleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_subTitleLabel];
        
        _bottomView = [[UIView alloc] init];
        _bottomView.translatesAutoresizingMaskIntoConstraints = NO;
        _bottomView.backgroundColor = [UIColor clearColor];
        [self addSubview:_bottomView];
        
        self.totalLayoutConstraints = @[
            [NSLayoutConstraint constraintWithItem:_titleLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1 constant:0],
            [NSLayoutConstraint constraintWithItem:_titleLabel attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1 constant:-2 - (kDevice_iPhoneX() ? 34 : 0)],
            [NSLayoutConstraint constraintWithItem:_subTitleLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1 constant:0],
            [NSLayoutConstraint constraintWithItem:_subTitleLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1 constant:2 - (kDevice_iPhoneX() ? 34 : 0)],
        ];
        
        NSMutableArray *array = [NSMutableArray array];
        [array addObjectsFromArray:@[
            [NSLayoutConstraint constraintWithItem:_bottomView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1 constant:0],
            [NSLayoutConstraint constraintWithItem:_bottomView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1 constant:kDevice_iPhoneX() ? 34 : 0],
            [NSLayoutConstraint constraintWithItem:_bottomView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeading multiplier:1 constant:0],
            [NSLayoutConstraint constraintWithItem:_bottomView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTrailing multiplier:1 constant:0]
        ]];
        [array addObjectsFromArray:self.totalLayoutConstraints];
        [self addConstraints:array];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    if (_subTitleLabel.text.length == 0) {
        [self removeConstraints:self.totalLayoutConstraints];
        [self addConstraints:@[
            [NSLayoutConstraint constraintWithItem:_titleLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1 constant:0],
            [NSLayoutConstraint constraintWithItem:_titleLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1 constant:- (kDevice_iPhoneX() ? 34 : 0) / 2],
            [NSLayoutConstraint constraintWithItem:_subTitleLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1 constant:0],
            [NSLayoutConstraint constraintWithItem:_subTitleLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1 constant:- (kDevice_iPhoneX() ? 34 : 0) / 2],
        ]];
    }
}
@end


@interface WDAlterSheetView ()
@property (nonatomic, strong) UIView *backView;
@property (nonatomic, assign) CGFloat totalHeight;
@property (nonatomic, copy) DidSelectedBlock selectedBlock;

@end

@implementation WDAlterSheetView

+ (instancetype)shared {
    static dispatch_once_t onceToken;
    static WDAlterSheetView *view = nil;
    dispatch_once(&onceToken, ^{
        view = [[WDAlterSheetView alloc] init];
        view.frame = [[UIScreen mainScreen] bounds];
    });
    return view;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        [self addTarget:self action:@selector(hidenView) forControlEvents: UIControlEventTouchUpInside];
    }
    return self;
}

+ (void)showAlterWithTitleItems:(NSArray<NSString *> *)items
                 didSelectBlock:(DidSelectedBlock)selectBlock {
    [self showAlterWithTitleItems:items cancelText:@"" cancelColor:nil didSelectBlock:selectBlock];
}

+ (void)showAlterWithTitleItems:(NSArray<NSString *> *)items
                     cancelText:(NSString *)cancelText
                    cancelColor:(UIColor *)cancelColor
                 didSelectBlock:(DidSelectedBlock)selectBlock {
    
    NSMutableArray<WDAlterSheetModel *> *array = [NSMutableArray arrayWithCapacity:items.count];
    for (NSString *title in items) {
        WDAlterSheetModel *model = [WDAlterSheetModel setupWithTitle:title];
        [array addObject:model];
    }
    [self showAlterWithTitleAttItems:array cancelText:cancelText cancelColor:cancelColor didSelectBlock:selectBlock];
}

+ (void)showAlterWithTitleAttItems:(NSArray<WDAlterSheetModel *> *)items
                    didSelectBlock:(DidSelectedBlock)selectBlock {
    [self showAlterWithTitleAttItems:items cancelText:@"" cancelColor:nil didSelectBlock:selectBlock];
}

+ (void)showAlterWithTitleAttItems:(NSArray<WDAlterSheetModel *> *)items
                        cancelText:(NSString *)cancelText
                       cancelColor:(UIColor *)cancelColor
                    didSelectBlock:(DidSelectedBlock)selectBlock {

    NSAssert(items.count > 0, @"Thead Reason ===> [WDAlterSheetView · itmes`count not be null]");
       
    WDAlterSheetView *view = [WDAlterSheetView shared];
    view.selectedBlock = selectBlock;
    [view addSubViews];
    
    CGFloat cornersSpace = KCornersSpace;
    if (items.firstObject.subTitle.length == 0) {
        cornersSpace = 0;
    }
    
    NSMutableArray *array = [NSMutableArray arrayWithArray:items];
    if (cancelText.length != 0) {
        WDAlterSheetModel *cancelModel = [WDAlterSheetModel setupWithTitle:cancelText titleColor:cancelColor];
        if (cancelColor == nil) {
            cancelModel = [WDAlterSheetModel setupWithTitle:cancelText titleColor:[UIColor redColor]];
        }
        [array addObject:cancelModel];
    }
    
    [array enumerateObjectsUsingBlock:^(WDAlterSheetModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx == array.count - 1) {
            WDAlterSheetLastControl *button = [view createLastButtonWithModel:obj tag:idx];
            if (cancelText.length == 0) {
                if (kDevice_iPhoneX()) {
                    button.frame = CGRectMake(0, KButtonHeight * (items.count - 1) + cornersSpace, kScreenWidth(), KButtonHeight + 34);
                } else {
                    button.frame = CGRectMake(0, KButtonHeight * (items.count - 1) + cornersSpace, kScreenWidth(), KButtonHeight);
                }
            } else {
                if (kDevice_iPhoneX()) {
                    button.frame = CGRectMake(0, KButtonHeight * items.count + KSpace + cornersSpace, kScreenWidth(), KButtonHeight + 34);
                } else {
                    button.frame = CGRectMake(0, KButtonHeight * items.count + KSpace + cornersSpace, kScreenWidth(), KButtonHeight);
                }
            }
            [view.backView addSubview:button];

        } else {
            WDAlterSheetControl *button = [view createButtonWithModel:obj tag:idx];
            if (idx == 0) {
                button.bottomLineView.hidden = idx == items.count - 1;
                button.frame = CGRectMake(0, 0, kScreenWidth(), KButtonHeight + cornersSpace);
            } else {
                button.bottomLineView.hidden = idx == items.count - 1;
                button.frame = CGRectMake(0, cornersSpace + KButtonHeight * idx, kScreenWidth(), KButtonHeight);
            }
            [view.backView addSubview:button];
        }
    }];

    
    [view showView];
    
    CGFloat totoalHeight = KButtonHeight * items.count;
    
    if (cancelText.length != 0) {
        totoalHeight += KSpace + KButtonHeight;
    }
    
    if (kDevice_iPhoneX()) {
        totoalHeight += 34;
    }
    
    view.backView.frame = CGRectMake(0, kScreenHeight(), kScreenWidth(), totoalHeight);

    [UIView animateWithDuration:0.35 animations:^{
        view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.35];
        view.backView.frame = CGRectMake(0, kScreenHeight() - totoalHeight, kScreenWidth(), totoalHeight);
    }];
    [self setupRoundedCornersWithView:view.backView];

    view.totalHeight = totoalHeight;
}


#pragma mark - Private

#pragma mark Operation Methods
- (UIView *)kWindowView {
    if ([self viewController].view != nil) {
        return [self viewController].view;
    } else if ([UIApplication sharedApplication].delegate.window != nil) {
        return [UIApplication sharedApplication].delegate.window;
    } else {
        return [UIApplication sharedApplication].keyWindow;
    }
}

- (void)hidenView {
    [UIView animateWithDuration:0.35 animations:^{
        self.backView.frame = CGRectMake(0, kScreenHeight(), kScreenWidth(), self.totalHeight);
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
    } completion:^(BOOL finished) {
        [self removeSubViews];
        [self removeFromSuperview];
    }];
}

- (void)showView {
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
    [[self kWindowView] addSubview:self];
}

- (void)addSubViews {
    _backView = [[UIView alloc] init];
    _backView.backgroundColor = kSpaceColor();
    [self addSubview:_backView];
}

- (void)removeSubViews {
    [_backView removeFromSuperview];
}

#pragma mark Target Action Method
- (void)buttonDidClickTargetAction:(UIControl *)sender {
    sender.backgroundColor = [UIColor whiteColor];

    [self hidenView];
    if (sender.tag != 0) {
        if (self.selectedBlock) {
            self.selectedBlock(sender.tag - 1);
        }
    }
}

- (void)buttonTouchUpOutSideTargetAction:(UIControl *)sender {
    sender.backgroundColor = [UIColor colorWithRed:0.93 green:0.93 blue:0.93 alpha:1];
}

#pragma mark Common UI Create Methods
- (UIButton *)creatButton:(NSString *)title tag:(NSUInteger)tag {
    UIButton *button = [[UIButton alloc] init];
    button.tag = tag;
    [button setTitle:title forState:UIControlStateNormal];
    button.titleLabel.font = kTitleFont();
    button.backgroundColor = [UIColor whiteColor];
    [button addTarget:self action:@selector(buttonDidClickTargetAction:) forControlEvents:UIControlEventTouchUpInside];
    return button;
}

- (WDAlterSheetControl *)createButtonWithModel:(WDAlterSheetModel *)model tag:(NSUInteger)tag {
    WDAlterSheetControl *v = [[WDAlterSheetControl alloc] init];
    v.titleLabel.text = model.title;
    v.titleLabel.textColor = model.titleColor;
    v.titleLabel.font = model.titleFont;
    v.subTitleLabel.text = model.subTitle;
    v.subTitleLabel.textColor = model.subTitleColor;
    v.subTitleLabel.font = model.subTitleFont;
    v.tag = tag;
    [v addTarget:self action:@selector(buttonTouchUpOutSideTargetAction:) forControlEvents:UIControlEventTouchDown];
    [v addTarget:self action:@selector(buttonDidClickTargetAction:) forControlEvents:UIControlEventTouchUpInside];
    return v;
}

- (WDAlterSheetLastControl *)createLastButtonWithModel:(WDAlterSheetModel *)model tag:(NSUInteger)tag {
    WDAlterSheetLastControl *v = [[WDAlterSheetLastControl alloc] init];
    v.titleLabel.text = model.title;
    v.titleLabel.textColor = model.titleColor;
    v.titleLabel.font = model.titleFont;
    v.subTitleLabel.text = model.subTitle;
    v.subTitleLabel.textColor = model.subTitleColor;
    v.subTitleLabel.font = model.subTitleFont;
    v.tag = tag;
    [v addTarget:self action:@selector(buttonTouchUpOutSideTargetAction:) forControlEvents:UIControlEventTouchDown];
    [v addTarget:self action:@selector(buttonDidClickTargetAction:) forControlEvents:UIControlEventTouchUpInside];
    return v;
}

- (UIViewController *)viewController {
    UIResponder *next = [self nextResponder];
    do {
        if ([next isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)next;
        }
        next = [next nextResponder];
    } while (next != nil);
    return nil;
}

#pragma mark - Tool Method
+ (void)setupRoundedCornersWithView:(UIView *)view {
    
    CAShapeLayer *mask = [CAShapeLayer layer];
    UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:view.bounds
                                                byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight)
                                                      cornerRadii:CGSizeMake(15, 15)];
    mask.path = path.CGPath;
    mask.frame = view.bounds;
    
    CAShapeLayer *borderLayer = [CAShapeLayer layer];
    borderLayer.path = path.CGPath;
    borderLayer.fillColor = [UIColor clearColor].CGColor;
    borderLayer.strokeColor = [UIColor clearColor].CGColor;
    borderLayer.lineWidth = 0;
    borderLayer.frame = view.bounds;
    view.layer.mask = mask;
    [view.layer addSublayer:borderLayer];
}
@end

使用实例

WDAlterSheetModel *m1 = [WDAlterSheetModel setupWithTitle:@"拍摄" titleColor:UIColor.blackColor titleFont:[UIFont systemFontOfSize:16] subTitle:@"照片或视频" subTitleColor:UIColor.lightGrayColor subTitleFont:[UIFont systemFontOfSize:11]];
WDAlterSheetModel *m2 = [WDAlterSheetModel setupWithTitle:@"从手机相册选择" titleColor:UIColor.blackColor];
[WDAlterSheetView showAlterWithTitleAttItems:@[m1, m2] cancelText:@"取消" cancelColor:[UIColor redColor] didSelectBlock:^(NSUInteger index) {
    NSLog(@"点击了:%lu", index);
}];

示例图

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

推荐阅读更多精彩内容