美文网首页
OC 经验 View的弹框解决方案

OC 经验 View的弹框解决方案

作者: 冯汉栩 | 来源:发表于2021-07-01 11:42 被阅读0次

一直觉得自己写的不是技术,而是情怀,一个个的教程是自己这一路走来的痕迹。靠专业技能的成功是最具可复制性的,希望我的这条路能让你们少走弯路,希望我能帮你们抹去知识的蒙尘,希望我能帮你们理清知识的脉络,希望未来技术之巅上有你们也有我。

在日常的开发中我们都会遇到这样的一个需求,就是在当前控制器里面弹出一个提示框,如下面的图。


image.png

如果只是弹出一个框只是用于显示的话。就是框里面不用进行点击操作的话,就简单了。我们可以自定义一个view 我之前是添加到win里面的,为什么添加到win,目的就是想整个屏幕显示。如果添加到控制器的view里面的话,控制器有系统自带的导航栏,view不会覆盖住导航栏,就显得不好看,所以就加到win里面去。


image.png
但是如果,view里面还有还有点击的操作跳转到其他去,例如下面的需求
image.png

如果添加到win里面去进行跳转最好用模态弹出(Modal)方法去跳转不回影响界面效果,例如:下面的图片效果

yinsi.gif

但是这种需求局限于 界面弹出来是用于看的,如果需求弹出的是登录页面,登录页面又要出注册页面,而等于页面本身是用push去注册页面的这样就麻烦了,入下面的图片

denglu.gif

如果添加到win的话 用push跳转回出现不好修复的BUG 如果添加到view的话解决跳转的问题,控制器有系统自带的导航栏,view不会覆盖住导航栏,就显得不好看。

其实解决这个办法。还是要添加到控制器的view里面去的,我们把系统自定的导航栏,隐藏它,改为自定义的导航栏,其实就是一个自定义的view,这样子就能解决问题,覆盖全屏,跳转就没有问题。

#import <UIKit/UIKit.h>
@class FastLoginView;
NS_ASSUME_NONNULL_BEGIN

@interface FastLoginView : UIView

typedef void(^FastLoginViewBlock) (NSString* value, FastLoginView *view);

+(void)showFastLoginWithView:(UIView *)view withBlock:(FastLoginViewBlock)block;

-(void)dismiss;

@end

NS_ASSUME_NONNULL_END

#import "FastLoginView.h"
@interface FastLoginView()
@property (nonatomic,copy  ) FastLoginViewBlock callBlock;
@property (nonatomic,strong) UIView *bgView;
@property (nonatomic,strong) UIButton *cancelBtn;
@property (nonatomic,strong) UILabel *titleLabel;
@property (nonatomic,strong) UILabel *phoneLabel;
@property (nonatomic,strong) UIButton *loginBtn;
@property (nonatomic,strong) UIButton *otherLoginBtn;
@property (nonatomic,strong) UILabel *subTitleLabel;
@end

@implementation FastLoginView

+(void)showFastLoginWithView:(UIView *)view withBlock:(FastLoginViewBlock)block{
    FastLoginView *tipView = [[FastLoginView alloc] initWithFrame:CGRectZero byCallBlock:block];
    tipView.addTo(view).makeCons(^{
    make.left.top.right.bottom.equal.view(view);
    });
    [tipView show];
}

- (instancetype)initWithFrame:(CGRect)frame byCallBlock:(FastLoginViewBlock)callBlock{
    self = [super initWithFrame:frame];
    self.callBlock = callBlock;
    [self buildUI];
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    NSData *phoneData = [NSString base64EncodingWithDecryptWithData:[[AppModel new].phone dataUsingEncoding:NSUTF8StringEncoding]];
    NSString *phoneString = [[NSString alloc] initWithData:phoneData encoding:NSUTF8StringEncoding];
    self.phoneLabel.text = phoneString;
}
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    [self buildUI];
    return self;
}

-(void) buildUI{
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    self.alpha = 0.f;
    self.hidden = YES;

    self.bgView = [UIView new];
    self.bgView.addTo(self).bgColor([UIColor whiteColor]).borderRadius(5).makeCons(^{
    make.center.equal.view(self);
    make.width.equal.constants(Phone_Width*0.85);
    make.height.equal.constants(Phone_Width*0.85*700/720);
    });

    self.cancelBtn = [UIButton new];
    self.cancelBtn.addTo(self.bgView).img(@"noamal_cancel_grey").makeCons(^{
        make.width.height.equal.constants(20);
        make.top.equal.view(self.bgView).constants(10);
        make.right.equal.view(self.bgView).constants(-10);
    }).onClick(^{
        [self dismiss];
    });

    self.titleLabel = [UILabel new];
    self.titleLabel.textColor = [UIColor blackColor];
    self.titleLabel.addTo(self.bgView).str(@"登录/注册").fnt(18).makeCons(^{
        make.centerX.equal.view(self.bgView);
        make.top.equal.view(self.bgView).constants(20);
    });

    self.phoneLabel = [UILabel new];
    NSData *phoneData = [NSString base64EncodingWithDecryptWithData:[[AppModel new].phone dataUsingEncoding:NSUTF8StringEncoding]];
    NSString *phoneString = [[NSString alloc] initWithData:phoneData encoding:NSUTF8StringEncoding];
    self.phoneLabel.str(phoneString);
    self.phoneLabel.textColor = [UIColor blackColor];
    self.phoneLabel.addTo(self.bgView).fnt([UIFont boldSystemFontOfSize:22]).makeCons(^{
        make.centerX.equal.view(self.bgView);
        make.top.equal.view(self.titleLabel).bottom.constants(25);
    });

    self.loginBtn = [UIButton new];
    self.loginBtn.addTo(self.bgView).str(@"一键登录").color([UIColor whiteColor]).bgColor([Color theme]).borderRadius(20).makeCons(^{
        make.centerX.equal.view(self.bgView);
        make.top.equal.view(self.phoneLabel).bottom.constants(25);
        make.height.equal.constants(40);
        make.width.equal.constants(Phone_Width*0.55);
    }).onClick(^{
        self.callBlock(@"一键登录", self);
    });

    self.otherLoginBtn = [UIButton new];
    self.otherLoginBtn.addTo(self.bgView).str(@"其他登录").color([UIColor blackColor]).fnt(16).makeCons(^{
        make.centerX.equal.view(self.bgView);
        make.top.equal.view(self.loginBtn).bottom.constants(10);
        make.height.equal.constants(40);
        make.width.equal.constants(Phone_Width*0.55);
    }).onClick(^{
        self.callBlock(@"其他登录", self);
    });

    self.subTitleLabel = [UILabel new];
    self.subTitleLabel.attributedText = AttStr(AttStr(@"登录即表示同意中国移动认证服务条款和用户协议,隐私政策并接受鲁克篮球获取本机号码").color([UIColor blackColor])).fnt(14).match(@"中国移动认证服务条款").linkForLabel.color([Color theme]).match(@"用户协议").linkForLabel.color([Color theme]).match(@"隐私政策").linkForLabel.color([Color theme]);
    self.subTitleLabel.addTo(self.bgView).lines(0).lineGap((8)).makeCons(^{
        make.top.equal.view(self.otherLoginBtn).bottom.constants(10);
        make.left.equal.view(self.bgView).constants(20);
        make.right.equal.view(self.bgView).constants(-20);
        make.bottom.equal.view(self.bgView).constants(-15);
    }).onLink(^(NSString *value){
        if ([value isEqualToString:@"中国移动认证服务条款"]) {
            self.callBlock(@"中国移动认证服务条款", self);
        }else if ([value isEqualToString:@"用户协议"]) {
            self.callBlock(@"用户协议", self);
        }else if ([value isEqualToString:@"隐私政策"]){
            self.callBlock(@"隐私政策", self);
        }
    });
}

-(void)show{
  self.hidden = NO;
  [UIView animateWithDuration:1.0f animations:^{
    self.alpha = 1.f;
  }];
}

-(void)dismiss{
  [UIView animateWithDuration:0.2f animations:^{
      self.alpha = 0.f;
  } completion:^(BOOL finished) {
    self.hidden = YES;
    [self removeFromSuperview];
  }];
}

@end

使用

            [FastLoginView showFastLoginWithView:self.view withBlock:^(NSString * _Nonnull value, FastLoginView *view) {
                if ([value isEqualToString:@"一键登录"]) {
                    [self.viewModel getLoginPWDWithMobile:phoneString withPwd:pwdString withAreaCode:@"+86" withBlock:^(NSString * _Nonnull value) {
                        [FHXHUD showSuccessTime:1.0 showTitle:value];
                        [view dismiss];
                    }];
                }else if ([value isEqualToString:@"其他登录"]){
                    [self.navigationController pushViewController:[LoginViewController new] animated:YES];
                }else if ([value isEqualToString:@"中国移动认证服务条款"]){
                    [FHXHUD showInfoTime:1.0 showTitle:@"敬请期待"];
                }else if ([value isEqualToString:@"用户协议"]){
                    ProtocolViewController *vc = [ProtocolViewController new];
                    vc.title = @"用户协议";
                    vc.protocolContent = ProtocolContentUser;
                    [self presentViewController:vc animated:YES completion:nil];
                }else if ([value isEqualToString:@"隐私政策"]){
                    ProtocolViewController *vc = [ProtocolViewController new];
                    vc.title = @"隐私政策";
                    vc.protocolContent = ProtocolContentPrivacy;
                    [self presentViewController:vc animated:YES completion:nil];
                }
            }];

相关文章

网友评论

      本文标题:OC 经验 View的弹框解决方案

      本文链接:https://www.haomeiwen.com/subject/lqsjultx.html