iOS自定义提示弹出框实现类似UIAlertView的效果

(编辑:jimmy 日期: 2024/10/3 浏览:2)

首先来看看实现的效果图

iOS自定义提示弹出框实现类似UIAlertView的效果

下面话不多说,以下是实现的示例代码

#import <UIKit/UIKit.h>

typedef void(^AlertResult)(NSInteger index);

@interface XLAlertView : UIView

@property (nonatomic,copy) AlertResult resultIndex;

- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message sureBtn:(NSString *)sureTitle cancleBtn:(NSString *)cancleTitle;

- (void)showXLAlertView;

@end
#import "XLAlertView.h"

///alertView 宽
#define AlertW 280
///各个栏目之间的距离
#define XLSpace 10.0

@interface XLAlertView()

//弹窗
@property (nonatomic,retain) UIView *alertView;
//title
@property (nonatomic,retain) UILabel *titleLbl;
//内容
@property (nonatomic,retain) UILabel *msgLbl;
//确认按钮
@property (nonatomic,retain) UIButton *sureBtn;
//取消按钮
@property (nonatomic,retain) UIButton *cancleBtn;
//横线线
@property (nonatomic,retain) UIView *lineView;
//竖线
@property (nonatomic,retain) UIView *verLineView;

@end

@implementation XLAlertView

- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message sureBtn:(NSString *)sureTitle cancleBtn:(NSString *)cancleTitle
{
  if (self == [super init]) {

    self.frame = [UIScreen mainScreen].bounds;

    self.backgroundColor = [UIColor colorWithWhite:0.8 alpha:0.6];

    self.alertView = [[UIView alloc] init];
    self.alertView.backgroundColor = [UIColor whiteColor];
    self.alertView.layer.cornerRadius = 5.0;

    self.alertView.frame = CGRectMake(0, 0, AlertW, 100);
    self.alertView.layer.position = self.center;

    if (title) {

      self.titleLbl = [self GetAdaptiveLable:CGRectMake(2*XLSpace, 2*XLSpace, AlertW-4*XLSpace, 20) AndText:title andIsTitle:YES];
      self.titleLbl.textAlignment = NSTextAlignmentCenter;

      [self.alertView addSubview:self.titleLbl];

      CGFloat titleW = self.titleLbl.bounds.size.width;
      CGFloat titleH = self.titleLbl.bounds.size.height;

      self.titleLbl.frame = CGRectMake((AlertW-titleW)/2, 2*XLSpace, titleW, titleH);

    }
    if (message) {

      self.msgLbl = [self GetAdaptiveLable:CGRectMake(XLSpace, CGRectGetMaxY(self.titleLbl.frame)+XLSpace, AlertW-2*XLSpace, 20) AndText:message andIsTitle:NO];
      self.msgLbl.textAlignment = NSTextAlignmentCenter;

      [self.alertView addSubview:self.msgLbl];

      CGFloat msgW = self.msgLbl.bounds.size.width;
      CGFloat msgH = self.msgLbl.bounds.size.height;

      self.msgLbl.frame = self.titleLbl"htmlcode">
XLAlertView *xlAlertView = [[XLAlertView alloc] initWithTitle:@"自定义UIAlertView" message:@"不喜勿喷,大神多多指导。不胜感激" sureBtn:@"确认" cancleBtn:@"取消"];
xlAlertView.resultIndex = ^(NSInteger index){
//回调---处理一系列动作
};
[xlAlertView showXLAlertView];

总结

以上就是这篇文章的全部内容了,希望本文的内容对各位iOS开发们能有所帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。

一句话新闻
高通与谷歌联手!首款骁龙PC优化Chrome浏览器发布
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。