Swift-自定义一个Alert弹窗

代码示例

import Foundation
import UIKit
import SnapKit

@objc public class CustomAlertViewController: UIViewController {
    
    private let imageView: UIImageView = UIImageView()
    private let contentView: UIView = UIView()
    private let titleLabel: UILabel = UILabel()
    private let messageLabel: UILabel = UILabel()
    private let confirmButton: UIButton = UIButton()
    private let cancelButton: UIButton = UIButton()
    //定义两个闭包属性作为按钮的回调
    var confirmHandler: (() -> Void)?
    var cancelHandler: (() -> Void)?
    
    @objc public init(title: String, message: String, confirmTitle: String, cancelTitle: String, confirmHandler: @escaping () -> Void = {}, cancelHandler: @escaping () -> Void = {}) {
        super.init(nibName: nil, bundle: nil)
        self.confirmHandler = confirmHandler
        self.cancelHandler = cancelHandler
        setupUI(title: title, message: message, confirmTitle: confirmTitle, cancelTitle: cancelTitle)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(title: String, message: String, confirmTitle: String, cancelTitle: String) {
        view.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.5)
        
        imageView.backgroundColor = UIColor.clear
        imageView.image = UIImage(named: "CustomAlertImage")
        imageView.contentMode = .scaleToFill
        imageView.clipsToBounds = true
        view.addSubview(imageView)
        imageView.snp.makeConstraints { make in
            make.center.equalToSuperview() // 居中显示
            make.leading.trailing.equalToSuperview().inset(50)
            make.height.equalTo(50)
        }
        
        view.addSubview(contentView)
        contentView.snp.makeConstraints { make in
            make.center.equalToSuperview() // 居中显示
            make.leading.trailing.equalToSuperview().inset(50)
        }
        
        titleLabel.text = title
        titleLabel.numberOfLines = 0 //可以显示的行数
        titleLabel.textColor = UIColor.black
        titleLabel.textAlignment = .center
        titleLabel.font = UIFont.boldSystemFont(ofSize: 20)
        contentView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.leading.trailing.equalTo(contentView)
            make.top.equalTo(contentView.snp.top).offset(10)
        }
        
        messageLabel.text = message
        messageLabel.numberOfLines = 0 //可以显示的行数
        messageLabel.textColor = UIColor.lightGray
        messageLabel.textAlignment = .center
        messageLabel.font = UIFont.systemFont(ofSize: 16)
        contentView.addSubview(messageLabel)
        messageLabel.snp.makeConstraints { make in
            make.leading.trailing.equalTo(contentView)
            make.top.equalTo(titleLabel.snp.bottom).offset(10)
            make.bottom.equalTo(contentView.snp.bottom).offset(-60)
        }
        
        updateImageViewSize()
        
        confirmButton.setTitle(confirmTitle, for: .normal)
        confirmButton.setTitleColor(UIColor.white, for: .normal)
        confirmButton.setBackgroundImage(UIImage(named: "CustomAlertConfirmButton"), for: .normal)
        confirmButton.layer.cornerRadius = 5
        confirmButton.isUserInteractionEnabled = true
        confirmButton.addTarget(self, action: #selector(confirmButtonTapped), for: .touchUpInside)
        view.addSubview(confirmButton)
        confirmButton.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: (self.view.frame.width - 250) / 2, height: 30))
            make.bottom.equalTo(imageView.snp.bottom).offset(-31)
            make.trailing.equalTo(imageView.snp.trailing).offset(-50)
        }
        
        cancelButton.setTitle(cancelTitle, for: .normal)
        cancelButton.setTitleColor(UIColor.white, for: .normal)
        cancelButton.setBackgroundImage(UIImage(named: "CustomAlertCancelButton"), for: .normal)
        cancelButton.layer.cornerRadius = 5
        cancelButton.isUserInteractionEnabled = true
        cancelButton.addTarget(self, action: #selector(cancelButtonTapped), for: .touchUpInside)
        view.addSubview(cancelButton)
        cancelButton.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: (self.view.frame.width - 250) / 2, height: 30))
            make.bottom.equalTo(imageView.snp.bottom).offset(-31)
            make.leading.equalTo(imageView.snp.leading).offset(50)
        }
    }
    
    func updateImageViewSize() {
        // 更新 contentView 的大小以适应内部 Label
        contentView.setNeedsLayout()
        contentView.layoutIfNeeded()
        // 获取 contentView 的实际大小
        let fittingSize = contentView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
        // 更新 imageView 的大小
        imageView.snp.updateConstraints { make in
            make.center.equalToSuperview() // 居中显示
            make.leading.trailing.equalToSuperview().inset(50)
            make.height.equalTo(fittingSize.height + 50)
        }
        imageView.setNeedsLayout()
        imageView.layoutIfNeeded()
    }
    
    public override func viewDidLoad() {
        super.viewDidLoad()
        view.clipsToBounds = true
        modalPresentationStyle = .overFullScreen
    }
    
    @objc private func confirmButtonTapped() {
        confirmHandler?() // 调用确认按钮的回调
        dismiss(animated: true, completion: nil)
    }
    
    @objc private func cancelButtonTapped() {
        cancelHandler?() // 调用取消按钮的回调
        dismiss(animated: true, completion: nil)
    }
}

使用代码示例 OC

		//oc调用swift
		#import "SYHandBook-swift.h"



         typedef void (^CompletionBlock)(void);
         CompletionBlock confirmBlock = ^{
             NSLog(@"用户点击了确定按钮");
             // 处理确认逻辑
         };
         
         CompletionBlock cancelBlock = ^{
             NSLog(@"用户点击了取消按钮");
             // 处理取消逻辑
         };
         
         CustomAlertViewController *customAlert = [[CustomAlertViewController alloc] initWithTitle:@"自定义标题" message:@"这里是详细信息。" confirmTitle:@"确定" cancelTitle:@"取消" confirmHandler:confirmBlock cancelHandler:cancelBlock];
         [self presentViewController:customAlert animated:YES completion:nil];

使用代码示例 Swift

         let customAlert = CustomAlertViewController(title: "自定义标题", message: "这里是详细信息。", confirmTitle: "确定", cancelTitle: "取消") { // 确定按钮的回调
             print("用户点击了确定按钮")
         } { // 取消按钮的回调
             print("用户点击了取消按钮")
         }
         present(customAlert, animated: true, completion: nil)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

敛柒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值