最新消息:文章中包含代码时,请遵守代码高亮规范!

【原创】IOS自定义弹窗

iOS 王 伟 114浏览 0评论

自定义弹窗是一个必备的技能,毕竟在工作中,设计师可不会按系统的做设计。就算设计师那么干了,客户也不会同意呀。

那我们来看看怎么自定义弹窗。我这里提供一个例子:

先看一下TipPopView.h文件:

#import <UIKit/UIKit.h>
typedef void (^ TipPopViewBlock) (UIView *popView);
@interface TipPopView : UIView
@property (nonatomic,strong) TipPopViewBlock block;
- (void)initWithBlock:(TipPopViewBlock )block superVC:(UIViewController *)superVC;
@end

可以看到是继承自UIView的,有一个方法initWithBlock。

再看一下TipPopView.m文件:

#import "TipPopView.h"

@implementation TipPopView
- (void)initWithBlock:(TipPopViewBlock )block superVC:(UIViewController *)superVC{
    TipPopView * popView = [[TipPopView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    popView.block = block;
    [[UIApplication sharedApplication].keyWindow addSubview:popView];

    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    backButton.backgroundColor = RGBA(0, 0, 0, 0.6);
    [backButton addTarget:popView action:@selector(backButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [popView addSubview:backButton];

    //中心view
    UIView *centerView = [UIView viewWithFrame:CGRectMake(33 * kScreenWidthProportion, 174 * kScreenHeightProportion, 250 * kScreenWidthProportion, 122 * kScreenHeightProportion) backgroundColor:kWhiteColor];
    [popView addSubview:centerView];
    [centerView setCornerRadius:6.f];

    UILabel *isSureLable = [[UILabel alloc]initWithFrame:CGRectMake(5*kScreenWidthProportion, 10 * kScreenHeightProportion, centerView.width-10*kScreenWidthProportion, 60 * kScreenHeightProportion)];
    isSureLable.textAlignment = NSTextAlignmentCenter;
    isSureLable.textColor = kBlackLabelColor;
    isSureLable.text = @"请绑定手机号,即可获得共享积分";
    isSureLable.lineBreakMode = UILineBreakModeWordWrap;
    isSureLable.numberOfLines = 0;
    isSureLable.font = FONT(16 * kFontProportion);
    [centerView addSubview:isSureLable];

    UIButton *commitButton =[[UIButton alloc] initWithFrame:CGRectMake(0, 80 *kScreenWidthProportion, 250 * kScreenWidthProportion, 40 * kScreenHeightProportion)];
    [commitButton buttonWithTitle:@"确定" Color:kBlackLabelColor BackgroundColor:kWhiteColor Font:13];
    [centerView addSubview:commitButton];
    [[commitButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        [popView backButtonAction];
    }];

    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(7 * kScreenWidthProportion,  80 * kScreenHeightProportion, centerView.width - 14 *kScreenWidthProportion, 1)];
    line.backgroundColor = kLineGrayColor;
    [centerView addSubview:line];
}

#pragma mark - 返回
- (void)backButtonAction {
    [self removeFromSuperview];
}
@end

其中isSureLable是中间显示的文本。当然如果你是需要传进来,那么就在initWithBlock上加上一个title的参数。以此类推。需要什么传什么。这里的按钮只有一个作用,点击消失的作用。大部分的时候,我们需要两个按钮。一个确定一个取消。

那么我们就需要在.h文件中定义:

typedef void(^PaySuccessBlock)(UIView *view, NSInteger isSure);

然后再在.m文件中使用:

[[leftButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x){
NSLog(@"否");
popView.block(popView, 0);
[popView backButtonAction];
}];

[[rightButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x){
NSLog(@"是");
popView.block(popView, 1);
[popView backButtonAction];
}];

在ViewController中使用:

__weak typeof(self) weakSelf = self;
[[PaySuccessView alloc] initWithBlock:^(UIView *view, NSInteger isSure) {
//这里添加点击事件的处理;isSure是0,则是否;isSure是1,则是是
} superVC:weakSelf titStr:@"确认删除地址"];

转载时请注明出处及相应链接,本文永久地址:https://blog.yayuanzi.com/24527.html


pay_weixin
pay_weixin
微信打赏
pay_weixin
支付宝打赏
感谢您对作者Bob的打赏,我们会更加努力!    如果您想成为作者,请点我

您必须 登录 才能发表评论!