MIAlertController 1.3

MIAlertController 1.3

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布最后发布2016年10月
SPM支持 SPM

Mario Iannotta 维护。



  • Mario Iannotta

MIAlertController

一个简单且可完全自定义的警告控制器

设置

  • pod 'MIAlertController', '~> 1.0' 添加到您的 Podfile 中,或将“MIAlertController”文件夹复制到您的项目中。
  • 只需一行代码即可创建一个新实例
MIAlertController(

  title: "Your alert title",
  message: "Your alert description",
  buttons: [
    MIAlertController.Button(title: "Button one", action: {
      print("button one tapped")
    }),
    MIAlertController.Button(title: "Button two", action: {
      print("button two apped
    })
  ]

).presentOn(self)

自定义

  • 您可以通过创建 MIAlertController.Config 结构的新实例并按您喜欢的更改属性来自定义警告的行为和 UI
    • config.dismissOnTouchOutsideEnabled -> 允许用户通过在警告外部触摸任意位置来撤销警告
    • config.backgroundColor -> 遮罩层背景
    • config.alertViewBackgroundColor -> 警告背景
    • config.alertViewCornerRadius -> 警告的圆角半径
    • config.alertMarginSize -> 警告与屏幕之间的最大边距
    • config.separatorColor -> 按钮之间的分隔线颜色
    • config.alertViewMaxSize -> 警告的最大尺寸
    • config.titleLabelFont -> 标题的字体
    • config.titleLabelTextColor -> 标题文本的颜色
    • config.titleLabelTextAlignment -> 标题文本的对齐方式
    • config.messageLabelFont -> 消息的字体
    • config.messageLabelTextColor -> 消息文本的颜色
    • config.messageLabelTextAlignment -> 消息文本的对齐方式
    • config.messageVerticalSpaceFromTitle -> 消息与标题之间的空间
    • config.buttonBackgroundView -> 包含按钮的视图的背景颜色
    • config.firstButtonRatio -> 按钮容器宽度和第一个按钮宽度的比例;仅在两个按钮的情况下可用

  • 有三个类型的按钮(.Default,.Destructive,.Cancel)具有不同的 UI,但是您可以使用 MIAlertController.Button.Config 结构创建一个新类型
    • config.font -> 按钮的字体
    • config.textColor -> 按钮文本的颜色
    • config.textAlignment -> 按钮文本的对齐方式
    • config.backgroundColor -> 按钮的背景颜色
    • config.buttonHeight -> 按钮的高度
    • config.contentEdgeOffset -> 按钮的边缘偏移量

示例

// Alert UI and behavior
var googlishAlertControllerConfig = MIAlertController.Config()

googlishAlertControllerConfig.firstButtonRatio = 0.8
googlishAlertControllerConfig.alertViewCornerRadius = 1
googlishAlertControllerConfig.messageLabelTextColor = UIColor(white: 0.45, alpha: 1)

googlishAlertControllerConfig.messageVerticalSpaceFromTitle = 25
googlishAlertControllerConfig.messageLabelFont = UIFont.systemFontOfSize(17)

googlishAlertControllerConfig.alertMarginSize = CGSize(width: 10, height: 10)
googlishAlertControllerConfig.alertViewMaxSize = CGSize(width: UIScreen.mainScreen().bounds.size.width - 80, height: 340)

googlishAlertControllerConfig.titleLabelTextAlignment = NSTextAlignment.Left
googlishAlertControllerConfig.messageLabelTextAlignment = NSTextAlignment.Left

// Buttons UI
var googlishAlertControllerLeftButtonConfig = MIAlertController.Button.Config()

googlishAlertControllerLeftButtonConfig.textColor = UIColor(red: 19/255.0, green: 152/255.0, blue: 138/255.0, alpha: 1)
googlishAlertControllerLeftButtonConfig.textAlignment = .Right
googlishAlertControllerLeftButtonConfig.font = UIFont.boldSystemFontOfSize(15)

var googlishAlertControllerRightButtonConfig = MIAlertController.Button.Config()

googlishAlertControllerRightButtonConfig.textColor = UIColor(red: 19/255.0, green: 152/255.0, blue: 138/255.0, alpha: 1)
googlishAlertControllerRightButtonConfig.font = UIFont.boldSystemFontOfSize(15)

let alertController = MIAlertController(
  title: "I'm a googlish alert!",
  message: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris",
  config: googlishAlertControllerConfig
)

alertController.addButton(
  MIAlertController.Button(title: "COOL", config: googlishAlertControllerLeftButtonConfig, action: {
    print("cool tapped")
  })
)

alertController.addButton(
  MIAlertController.Button(title: "OK", config: googlishAlertControllerRightButtonConfig, action: {
    print("ok tapped")
  })
)

alertController.presentOn(self)

演示

在此仓库中,您还可以找到演示。

信息

如果喜欢这个git,可以在以下地方关注我或关注我的twitter :) @MarioIannotta

来自意大利的问候!