一天一个第三方库iOS-SCLAlertView使用

SCLAlertView使用

SCLAlertView是个好用的弹出框,使用于Swift。可以用来代替UIAllertView和UIAlertController

下面是我做的几个简单的演示demo地址

效果演示

很多都是可以自定义的,使用起来也是非常方便。

安装

我使用pod安装

在Podfile文件中添加

pod 'SCLAlertView'

然后

根据情况 pod install 或者 pod update

使用时

import SCLAlertView

在引入第三库后,首先要做的就是编译一下,看看能不能编译通过,有问题的话赶紧解决。

我的就有问题,一开始就跑不起了,不知道为什么。

报了一连串的错误

最后试了很多方法,不知道怎么解决的。可能的解决方案有:

更改pod内容为下面这样,指定源以获取最新版本。

pod 'SCLAlertView', :git => 'https://github.com/vikmeup/SCLAlertView-Swift.git'

在Build Phases 中 删除Pods_XXXX.framework 重新导入一遍,这一次我把SCLAlertView.framework也导入了。

最后就好了。

起步

简单使用可以弹出提示框

SCLAlertView().showInfo("Important info", subTitle: "You are great")

更新提示框内容

let alertViewResponder: SCLAlertViewResponder = SCLAlertView().showSuccess("Hello World", subTitle: "This is a more descriptive text.")

// Upon displaying, change/close view
alertViewResponder.setTitle("New Title") // Rename title
alertViewResponder.setSubTitle("New description") // Rename subtitle
alertViewResponder.close() // Close view

常用的弹框选项

SCLAlertView().showError("Hello Error", subTitle: "This is a more descriptive error text.") // Error
SCLAlertView().showNotice("Hello Notice", subTitle: "This is a more descriptive notice text.") // Notice
SCLAlertView().showWarning("Hello Warning", subTitle: "This is a more descriptive warning text.") // Warning
SCLAlertView().showInfo("Hello Info", subTitle: "This is a more descriptive info text.") // Info
SCLAlertView().showEdit("Hello Edit", subTitle: "This is a more descriptive info text.") // Edit

调用showTitle

let timeout = SCLAlertView.SCLTimeoutConfiguration(timeoutValue: 2.0, timeoutAction: {
   print("well done printed after two seconds")
})
SCLAlertView().showTitle("Congratulations", subTitle: "Operation successfully completed.", timeout: timeout, completeText: "Well Done", style: .success)

自定义外观

SCLAlertView.SCLAppearanc有超过15个属性可以设置

比如:

let appearance = SCLAlertView.SCLAppearance(
    kTitleFont: UIFont(name: "HelveticaNeue", size: 20)!,
    kTextFont: UIFont(name: "HelveticaNeue", size: 14)!,
    kButtonFont: UIFont(name: "HelveticaNeue-Bold", size: 14)!,
    showCloseButton: false
)

let alert = SCLAlertView(appearance: appearance)

添加按钮

可以使用target和selector也可使用闭包

let alertView = SCLAlertView()
alertView.addButton("First Button", target:self, selector:Selector("firstButton"))
alertView.addButton("Second Button") {
    print("Second button tapped")
}
alertView.showSuccess("Button View", subTitle: "This alert view has buttons")

隐藏关闭按钮

let appearance = SCLAlertView.SCLAppearance(
    showCloseButton: false
)
let alertView = SCLAlertView(appearance: appearance)
alertView.showSuccess("No button", subTitle: "You will have hard times trying to close me")

隐藏关闭按钮并且设置几秒后自动关闭

let appearance = SCLAlertView.SCLAppearance(
    showCloseButton: false
)
let alertView = SCLAlertView(appearance: appearance)
alertView.showWarning("No button", subTitle: "Just wait for 3 seconds and I will disappear", duration: 3)

隐藏icon

let appearance = SCLAlertView.SCLAppearance(
    showCircularIcon: false
)
let alertView = SCLAlertView(appearance: appearance)
alertView.showSuccess("No icon", subTitle: "This is a clean alert without Icon!")

使用自定义icon

let appearance = SCLAlertView.SCLAppearance(
    showCircularIcon: true
)
let alertView = SCLAlertView(appearance: appearance)
let alertViewIcon = UIImage(named: "IconImage") //Replace the IconImage text with the image name
alertView.showInfo("Custom icon", subTitle: "This is a nice alert with a custom icon you choose", circleIconImage: alertViewIcon)

添加文本框

// Add a text field
let alert = SCLAlertView()
let txt = alert.addTextField(title:"Enter your name")
alert.addButton("Show Name") {
    println("Text value: \(txt.text)")
}
alert.showEdit("Edit View", subTitle: "This alert view shows a text box")

使用自定义子视图替代子标题

        // 创建显示配置
        let appearance = SCLAlertView.SCLAppearance(
            kTitleFont:UIFont(name: "HelveticaNeue", size: 20)!,
            kTextFont:UIFont(name: "HelveticaNeue", size: 14)!,
            kButtonFont:UIFont(name: "HelveticaNeue-Bold", size: 14)!,
            showCloseButton:false,
            dynamicAnimatorActive:true
        )
        
        // 使用自定义显示配置创建弹出框
        let alert = SCLAlertView(appearance: appearance)
        
        //创建子视图
        let subview = UIView(frame: CGRect(x: 0, y: 0, width: 216, height: 70))
        let x = (subview.frame.width - 180 ) / 2
        
        //添加一个输入框
        let textfield1 = UITextField(frame: CGRect(x: x, y: 10, width: 180, height: 25))
        textfield1.layer.borderColor = UIColor.green.cgColor
        textfield1.layer.borderWidth = 1.5
        textfield1.layer.cornerRadius = 5
        textfield1.placeholder = "Username"
        textfield1.textAlignment = NSTextAlignment.center
        subview.addSubview(textfield1)
        
        //添加第二个输入框
        let textfield2 = UITextField(frame: CGRect(x: x, y: textfield1.frame.maxY + 10, width: 180, height: 25))
        textfield2.isSecureTextEntry = true
        textfield2.layer.borderColor = UIColor.green.cgColor
        textfield2.layer.borderWidth = 1.5
        textfield2.layer.cornerRadius = 5
        textfield2.placeholder = "Password"
        textfield2.textAlignment = NSTextAlignment.center
        subview.addSubview(textfield2)
        
        //设置弹框的自定义视图属性
        alert.customSubview = subview
        
        _ = alert.addButton("Login", action: {
            print("Logged in")
        })
        
        //添加超时按钮
        let showTimeout = SCLButton.ShowTimeoutConfiguration(prefix:"(",suffix:"s)")
        _ = alert.addButton("Timeout Button", backgroundColor: UIColor.brown, textColor: UIColor.yellow, showTimeout: showTimeout) {
            print("Timeout button tapped")
        }
        
        let timeoutValue: TimeInterval = 10.0
        let timeoutAction: SCLAlertView.SCLTimeoutConfiguration.ActionType = {
            print("Timeout occurred")
        }
        
        //显示
        alert.showInfo("Login", subTitle: "", timeout: SCLAlertView.SCLTimeoutConfiguration(timeoutValue: timeoutValue, timeoutAction: timeoutAction))

可以自定义的属性有

// Button 
kButtonFont: UIFont                     
buttonCornerRadius : CGFloat            
showCloseButton: Bool                   
kButtonHeight: CGFloat                  

// Circle Image
showCircularIcon: Bool
kCircleTopPosition: CGFloat
kCircleBackgroundTopPosition: CGFloat
kCircleHeight: CGFloat
kCircleIconHeight: CGFloat

// Text
kTitleFont: UIFont
kTitleTop:CGFloat
kTitleHeight:CGFloat
kTextFont: UIFont
kTextHeight: CGFloat
kTextFieldHeight: CGFloat
kTextViewdHeight: CGFloat

// View 
kDefaultShadowOpacity: CGFloat          
kWindowWidth: CGFloat
kWindowHeight: CGFloat
shouldAutoDismiss: Bool // Set this false to 'Disable' Auto hideView when SCLButton is tapped
fieldCornerRadius : CGFloat
contentViewCornerRadius : CGFloat
disableTapGesture: Bool // set this to true if adding tableview to subView

弹框风格

enum SCLAlertViewStyle: Int {
    case success, error, notice, warning, info, edit, wait, question
}

弹框动画风格

// Animation Styles
public enum SCLAnimationStyle {
    case noAnimation, topToBottom, bottomToTop, leftToRight, rightToLeft
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,219评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,363评论 1 293
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,933评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,020评论 0 206
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,400评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,640评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,896评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,597评论 0 199
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,327评论 1 244
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,581评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,072评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,399评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,054评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,083评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,849评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,672评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,585评论 2 270

推荐阅读更多精彩内容

  • 现在对于我们 iOS 开发来说,基本上说不可能不使用第三方轮子啦,毕竟没那么多时间,而且自己造的轮子往往想着成为上...
    天空中的球阅读 49,116评论 75 982
  • 简介 此文用于总结,本人使用过或者收藏过的Github第三方类库,以便日后查阅,也便他人借鉴。 资料整理中不定期更...
    Oneruofeng阅读 7,118评论 5 79
  • 图表 ZFChart(star:525)PNChart(star:8836)Charts(star:16043) ...
    Jackson_Z阅读 662评论 0 1
  • JAVA RMI是什么? Java RMI 指的是远程方法调用 (Remote Method Invocation...
    公顷阅读 672评论 0 6
  • 泪总无觉中, 心却有知觉。 化成无穷力, 希望不遥远。
    A刘世剑阅读 116评论 0 0