LIHAlert 2.1.1

LIHAlert 2.1.1

测试已测试
Lang语言 SwiftSwift
许可 MIT
发布最后发布2017年8月
SwiftSwift 版本3.0
SPM支持 SPM

Lasith Hettiarachchi 维护。



LIHAlert 2.1.1

  • Lasith Hettiarachchi

LIHAlert

LIHAlert

LIHAlert 为 iOS 提供动画横幅。已更新到 Swift 3

general custom

示例项目

LIHAlert 工作区包含一个示例项目,也可用于开发。

要求

Xcode 7+

安装

LIHAlert 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中

pod "LIHAlert"

或者

将 LIHAlert 文件夹复制到您的项目中。

对于 Swift 2 版本

pod 'LIHAlert', '~> 1.1.0'

用法

使用以下代码导入模块

    import LIHAlert

要运行示例项目,先克隆仓库,并在 Example 目录下首先运行 pod install

模板

LIHAlert 包含一些为每个警报类型预定义的警报模板。

您可以使用以下代码片段来使用它们。

1. 文本横幅

text

    var textAlert: LIHAlert?
    override func viewDidLoad() {
        super.viewDidLoad()
        self.textAlert = LIHAlertManager.getTextAlert("Sample Message")
        self.textAlert?.initAlert(self.view)
    }
    func showBanner(sender: AnyObject) {
        self.textAlert?.show(nil, hidden: nil)
    }

调用 showBanner() 函数来显示横幅

2. 成功警报

success

var successAlert: LIHAlert?
override func viewDidLoad() {
    super.viewDidLoad()
    self.successAlert = LIHAlertManager.getSuccessAlert(message: "Successfully subscribed")
    self.successAlert?.initAlert(self.view)
}
func showBanner(sender: AnyObject) {
    self.successAlert?.show(nil, hidden: nil)
}

要更改图标,

    successAlert?.icon = UIImage(named:"imageName")
3. 错误警报

error

var errorAlert: LIHAlert?
override func viewDidLoad() {
    super.viewDidLoad()
    self.errorAlert = LIHAlertManager.getErrorAlert(message: "Failed. Please try again")
    self.errorAlert?.initAlert(self.view)
}
func showBanner(sender: AnyObject) {
    self.errorAlert?.show(nil, hidden: nil)
}

要更改图标,

    successAlert?.icon = UIImage(named:"imageName")
4. 带标题的文本

textwithtitle

    var textWithTitleAlert: LIHAlert?
    override func viewDidLoad() {
        super.viewDidLoad()
        self.textWithTitleAlert = LIHAlertManager.getTextWithTitleAlert("This is The Title", message: "This is a sample message.")
        self.textWithTitleAlert?.initAlert(self.view)
    }
    func showBanner(sender: AnyObject) {
        self.textWithTitleAlert?.show(nil, hidden: nil)
    }
5. 加载警报

这是一个带有文本和 ActivityIndicator 的横幅。这不是自动关闭的横幅。完成操作后必须隐藏它。

processing

    var processingAlert: LIHAlert?
    override func viewDidLoad() {
        super.viewDidLoad()
        self.processingAlert = LIHAlertManager.getProcessingAlert("Fetching data...")
        self.processingAlert?.initAlert(self.view)
    }
    func showBanner(sender: AnyObject) {
        self.processingAlert?.show(nil, hidden: nil)
    }
    override hideBanner(sender: AnyObject) {
        self.processingAlert?.hide(nil)
    }

调用 showBanner() 函数来显示横幅,调用 hideBanner() 函数来隐藏横幅。要更改 ActivityIndicator 风格,

    processingAlert?.activityIndicatorStyle = UIActivityIndicatorViewStyle.WhiteLarge
6. 带按钮的警示文本

此警示包含文本和一个按钮。更适合通知用户重要信息。 withbutton

    var textWithButtonAlert: LIHAlert?
    override func viewDidLoad() {
        super.viewDidLoad()
        self.textWithButtonAlert = LIHAlertManager.getTextWithButtonAlert("You have successfully subscribed for the monthly newsletter", buttonText: "Dismiss")
        self.textWithButtonAlert?.initAlert(self.view)
    }
    func showBanner(sender: AnyObject) {
        textWithButtonAlert?.show(nil, hidden: nil)
    }

调用 showBanner() 函数显示横幅。实现对按钮点击事件的 LIHAlertDelegate。

class ViewController: LIHAlertDelegate {
    func buttonPressed(button: UIButton) {
        print(“You have pressed the button”)
        self.textWithButtonAlert?.hideAlert(nil)
    }
}
7. 带有两个按钮的警示文本

此警示包含两个按钮和文本。

twobuttons

    var textWithTwoButtonsAlert: LIHAlert?

    override func viewDidLoad() {
        super.viewDidLoad()
        self.textWithTwoButtonsAlert = LIHAlertManager.getTextWithTwoButtonsAlert("Do you want to subscribe for the monthly newsletter?", buttonOneText: "Subscribe", buttonTwoText: "Cancel")
        self.textWithTwoButtonsAlert?.initAlert(self.view)
    }
    func showBanner(sender: AnyObject) {
        textWithTwoButtonsAlert?.show(nil, hidden: nil)
    }

调用 showBanner() 函数显示横幅。实现对按钮点击事件的 LIHAlertDelegate。

class ViewController: LIHAlertDelegate {
    func buttonOnePressed(button: UIButton) {
        self.textWithTwoButtonsAlert?.hideAlert({ () -> () in
            self.successAlert?.show(nil, hidden: nil)
        })
    }
    func buttonTwoPressed(button: UIButton) {
        self.textWithTwoButtonsAlert?.hideAlert(nil)
    }
}
8. 自定义视图警示

您可以指定任何视图作为横幅。

customview

    var customViewAlert: LIHAlert?

    override func viewDidLoad() {
        super.viewDidLoad()
    //In this case I am using an ImageView as the banner
        let customView = UIImageView(frame: CGRectMake(0.0, 64.0, 100, 50))
        customView.image = UIImage(named: "customViewImage")
        self.customViewAlert = LIHAlertManager.getCustomViewAlert(customView)
        self.customViewAlert?.initAlert(self.view)
    }
    func showBanner(sender: AnyObject) {
        self.customViewAlert?.show(nil, hidden: nil)
    }

如何嵌入视图控制器

custom_list

var customViewAlert: LIHAlert?
override func viewDidLoad() {
    super.viewDidLoad()
    let vc = (self.storyboard?.instantiateViewController(withIdentifier: "TableVc"))!
    self.customViewAlert = LIHAlertManager.getCustomViewAlert(customView: vc.view)
    self.customViewAlert?.initAlert(self.view)
    self.addChildViewController(vc)
    vc.didMove(toParentViewController: self)
}
func showBanner(sender: AnyObject) {
    self.customViewAlert?.show(nil, hidden: nil)
}

请参阅 ‘CustomAlertsViewController’ 以获取更多嵌入视图控制器的示例

如何创建自己的横幅

    let alertTextAlert: LIHAlert = LIHAlert()
    alertTextAlert.alertType = LIHAlertType.Text
    alertTextAlert.contentText = message
    alertTextAlert.alertColor = UIColor(red: 102.0/255.0, green: 197.0/255.0, blue: 241.0/255.0, alpha: 1.0)
    alertTextAlert.alertHeight = 50.0
    alertTextAlert.alertAlpha = 1.0
    alertTextAlert.autoCloseEnabled=true
    alertTextAlert.contentTextColor = UIColor.whiteColor()
    alertTextAlert.hasNavigationBar = true
    alertTextAlert.paddingTop = 0.0
    alertTextAlert.animationDuration = 0.35
    alertTextAlert.autoCloseTimeInterval = 1.5
警示类型
    enum LIHAlertType {
            case Custom, Text, TextWithLoading, TextWithIcon, TextWithButton, TextWithTwoButtons, TextWithTitle
    }

所有属性的列表

    //delegates
    public var delegate: LIHAlertDelegate?
    
    //Title
    public var titleLabel: UILabel?
    public var titleText: String //Default is "Sample Title"
    public var titleTextColor: UIColor //Default is UIColor.blackColor()
    public var titleTextFont: UIFont?
    public var titleTextFontSize: CGFloat?
    public var titleTextNumberOfLines: Int //Default is 1 
    
    
    //Content Text
    public var contentLabel: UILabel?
    public var contentText: String //Default is "Sample Content"
    public var contentTextColor: UIColor //Default is UIColor.blackColor()
    public var contentTextFont: UIFont?
    public var contentTextNumberOfLines: Int //Default is 2
    
    //TextWithLoading
    public var activityIndicatorStyle: UIActivityIndicatorViewStyle //Default is UIActivityIndicatorViewStyle.White
    
    //Icon
    public var  iconImageView: UIImageView?
    public var  icon: UIImage?
    
    //OneButton
    public var  button_textWithButton: UIButton?
    public var  buttonText: String //Default is "Dismiss"
    public var  buttonColor: UIColor //Default is UIColor.blueColor()
    public var  buttonTextColor: UIColor //Default is UIColor.whiteColor()
    public var  buttonFont: UIFont?
    public var  buttonBordercolor: UIColor //Default is UIColor.whiteColor()
    public var  buttonBorderWidth: CGFloat //Default is 1.0 
    public var  buttonCornerRadius: CGFloat //Default is 3.0
    public var  buttonWidth: CGFloat?
    
    //TWO BUTTONS
        //ButtonOne
    public var  buttonOne_textWithButton: UIButton?
    public var  buttonOneText: String //Default is "Dismiss"
    public var  buttonOneColor: UIColor //Default is UIColor.blueColor()
    public var  buttonOneTextColor: UIColor //Default is UIColor.whiteColor() 
    public var  buttonOneFont: UIFont?
    public var  buttonOneBordercolor: UIColor //Default is UIColor.whiteColor()
    public var  buttonOneBorderWidth: CGFloat //Default is 1.0
    public var  buttonOneCornerRadius: CGFloat //Default is 3.0
    
        //ButtonTwo
    public var  buttonTwo_textWithButton: UIButton?
    public var  buttonTwoText: String //Default is "Dismiss"
    public var  buttonTwoColor: UIColor //Default is UIColor.blueColor()
    public var  buttonTwoTextColor: UIColor //Default is UIColor.whiteColor()
    public var  buttonTwoFont: UIFont?
    public var  buttonTwoBordercolor: UIColor //Default is UIColor.whiteColor()
    public var  buttonTwoBorderWidth: CGFloat //Default is 1.0
    public var  buttonTwoCornerRadius: CGFloat //Default is 3.0
    
    //AlertView
    public var  alertView: UIView?
    public var  alertColor: UIColor //Default is UIColor.grayColor()
    public var  alertAlpha: CGFloat //Default is 1.0
    public var  alertHeight:CGFloat //Default is 75.0
    public var  paddingLeft: CGFloat //Default is 0.0
    public var  paddingTop: CGFloat //Default is 0.0
    public var  animationDuration: NSTimeInterval //Default is 0.5
    public var  alertType: LIHAlertType //Default is LIHAlertType.Text
    public var  autoCloseEnabled: Bool //Default is true
    public var  autoCloseTimeInterval: Double //Default is 3.0
    public var  hasNavigationBar: Bool //Default is false
    public var  touchBackgroundToDismiss: Bool //default is false
    public var  dimsBackground: Bool //default is false
    public var backgroundDimOpacity: CGFloat //default is 0.4

使用完成回调

    //when showing an auto hiding banner
    lihAlert?.show({ () -> () in
                //alert showed
            }, hidden: { () -> () in
                //alert hidden
        })
        
        //when hiding a banner
        lihAlert?.hideAlert({ () -> () in
            //Banner hidden
        })

已知问题

1. 顶部边距

在一些项目中,警示可能会在顶部出现边距。希望在下一个版本中得到修复。在此之前,请使用以下解决方案。

解决方案

即使在导航栏中,也要将其设置为 false。

alert.hasNavigationBar = false

更改日志

v2.1.1

淡色背景,触摸背景以关闭

v2.0.4

支持Objective-C(beta)

v2.0.2

嵌入视图控制器支持

v2.0.0

更新到Swift 3

v1.1.0

修复问题——警示出现时浮在最上面

v1.0.0

横幅模板
  • 仅文本
  • 带标题的文本
  • 文本与 ActivityIndicator
  • 文本与图标
  • 带按钮的文本
  • 带两个按钮的文本
  • 自定义视图横幅

作者

Lasith Hettiarachchi,[email protected]

许可证

LIHAlert 在 MIT 许可证下可用。请参阅 LICENSE 文件以获取更多信息。