代码示例
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.setNeedsLayout()
contentView.layoutIfNeeded()
let fittingSize = contentView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
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
#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)