BZPopupWindow 1.0.0

BZPopupWindow 1.0.0

Banghua_Zhao 维护。



  • banghuazhao

BZPopupWindow

Swift Version Version License Platform

简介

BZPopupWindow 是一个使用 Swift 编写的通用、可自定义和轻量级的弹出窗口

功能

  • 支持图片、自定义标题和消息、自定义视图和自定义动画
  • 比默认的 UIAlterController 更好的 UI 设计

示例

要运行示例项目,首先克隆仓库,然后从 Example 目录运行 pod install

1. 默认图片 + 标题 + 消息 + 按钮

只需提供图片(可选)、标题(可选)、消息(可选)、操作(可选)。要显示弹出窗口,我们可以使用 present 方法。

let image = UIImage(named: "correct.png")
let title = "TitleTitleTitle"
let message = "MessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessage"
let popupWindow = BZPopupWindow(image: image, imageSize: CGSize(width: 80, height: 80), title: title, message: message)
let action = BZPopupAction(title: "Ok")
popupWindow.addAction(action)
present(popupWindow, animated: true, completion: nil)

2. 显示方法

您也可以使用显示方法来显示弹出窗口。您还可以更改文本对齐方式。

let title = "TitleTitleTitle"
let message = "MessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessage"
let popupWindow = BZPopupWindow(title: title, message: message)
popupWindow.messageAligment = .left
popupWindow.show(completion: nil)

3. 双按钮

此示例在弹出窗口中创建双按钮。它还创建自定义标题和消息(属性字符串)。此外,还提供一个完成处理程序供 action2 使用。更重要的是,消息中有可点击的链接。

let title = "TitleTitleTitle"
let attributedTitle = NSAttributedString(string: title, attributes: [
    NSAttributedString.Key.font: UIFont.systemFont(ofSize: 17, weight: .bold),
    NSAttributedString.Key.foregroundColor: UIColor.black])

let message = "MessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageClickHereMessageMessageMessageMessageMessageMessageMessageMessageMessageMessageMessage"
let attributedMessage: NSMutableAttributedString = NSMutableAttributedString(string: message)
let clickHereTange = (attributedMessage.string as NSString).range(of: "ClickHere")
attributedMessage.addAttribute(NSAttributedString.Key.link, value: "https://github.com/", range: clickHereTange)
attributedMessage.addAttributes([
    NSAttributedString.Key.font: UIFont.systemFont(ofSize: 15),
    NSAttributedString.Key.foregroundColor: UIColor(red: 51/255, green: 51/255, blue: 51/255, alpha: 1)]
, range: NSRange(location: 0, length: attributedMessage.string.count))

let popupWindow = BZPopupWindow(title: attributedTitle, message: attributedMessage)
let action1 = BZPopupAction(title: "Cancel", style: .cancel)
let action2 = BZPopupAction(title: "Ok") {
    print("Ok")
}
popupWindow.addAction(action1)
popupWindow.addAction(action2)
present(popupWindow, animated: true, completion: nil)

4. 自定义视图

此示例在弹出窗口中创建自定义视图。该自定义视图是 UIScrollView。此示例可用于显示使用条款。此外,按钮的颜色也进行了自定义。

let customView = UIScrollView()
customView.snp.makeConstraints { make in
    make.width.equalTo(260)
    make.height.equalTo(300)
}

let messageLabel = UILabel(frame: .zero)

customView.addSubview(messageLabel)

messageLabel.text = "AgreementAgreementAgreementAgreementAgreementAgreementAgreementAgreementAgreementAgreementAgreement\n\nAgreementAgreementAgreementAgreementAgreementAgreementAgreementAgreementAgreementAgreementAgreement\n\nAgreementAgreementAgreementAgreementAgreementAgreementAgreementAgreementAgreementAgreementAgreement\n\nAgreementAgreementAgreementAgreementAgreementAgreementAgreementAgreementAgreementAgreementAgreement"
messageLabel.textAlignment = .left
messageLabel.font = UIFont.systemFont(ofSize: 15)
messageLabel.numberOfLines = 0
messageLabel.lineBreakMode = .byWordWrapping
messageLabel.textColor = UIColor(red: 51/255, green: 51/255, blue: 51/255, alpha: 1)
messageLabel.snp.makeConstraints { make in
    make.top.equalTo(customView.snp.top)
    make.width.equalTo(260)
    make.bottom.equalToSuperview()
    make.centerX.equalToSuperview()
}

let popupWindow = BZPopupWindow(title: "Term of Use", customContentView: customView)

let action1 = BZPopupAction(title: "Agree", color: .orange)
let action2 = BZPopupAction(title: "Disagree", color: .orange, style: .cancel)

popupWindow.addAction(action1)
popupWindow.addAction(action2)
popupWindow.show(completion: nil)

5. 自定义动画

左侧是默认动画。右侧是自底部过渡的动画。

let title = "TitTitleTitle"
let message = "AnimationFromBottomAnimationFromBottomAnimationFromBottomAnimationFromBottomAnimationFromBottomAnimationFromBottomAnimationFromBottomAnimationFromBottomAnimationFromBottomAnimationFromBottomAnimationFromBottomAnimationFromBottomAnimationFromBottomAnimationFromBottomAnimation"
let popupWindow = BZPopupWindow(image: nil, imageSize: CGSize(width: 60, height: 60), title: title, message: message)
let action = BZPopupAction(title: "Ok")
popupWindow.addAction(action)
//        popupWindow.show(completion: nil) // default animation
popupWindow.show(animationType: .bottom, completion: nil) // transition from bottom

要求

此版本要求 iOS 10.0+ 和 Swift 5。

安装

BZPopupWindow 可以通过 CocoaPods 获取。要安装它,只需将以下行添加到您的 Podfile 中

pod 'BZPopupWindow'

作者

banghuazhao, [email protected]

许可证

BZPopupWindow 采用 MIT 许可证。有关更多信息,请参阅 LICENSE 文件。