Coder Social home page Coder Social logo

dinhquan / swiftalertview Goto Github PK

View Code? Open in Web Editor NEW
295.0 4.0 35.0 765 KB

A powerful AlertView library written in Swift

License: MIT License

Swift 98.04% Ruby 1.96%
alert popup swift ios uialertcontroller uialertview customize swiftui uikit cocoapods swift-package-manager carthage

swiftalertview's Introduction

SwiftAlertView

A powerful customizable Alert View library written in Swift.

SwiftAlertView is the best alternative for UIAlertController and SwiftUI alert. With SwiftAlertView, you can easily make your desired Alert View in some lines of code.

Highlight Features

Features SwiftAlertView UIAlertController
Change button color
Change button font
Change title, message color/font
Change title, message margin
Change background color/image
Change dim background color
Change border radius, separator color
Dark mode
Add text fields
Callback for handling text changed
Init alert with custom view/xib file
TextField Validation Label
Customize transtion type
Easy-to-use APIs Super easy Not so easy

Installation

CocoaPods

pod 'SwiftAlertView', '~> 2.2.1'

Carthage

github "https://github.com/dinhquan/SwiftAlertView" ~> 2.2.1

Swift Package Manager

dependencies: [
    .package(url: "https://github.com/dinhquan/SwiftAlertView", .upToNextMajor(from: "2.2.1"))
]

Manually

Drag and drop the file named SwiftAlertView inside Source in your project and you are done.

Usage

Showing alert

SwiftAlertView.show(title: "Title", message: "Message", buttonTitles: "Cancel", "OK")

Customization

SwiftAlertView.show(title: "Title",
                    message: "Message",
                    buttonTitles: "OK", "Cancel") { alert in
    alert.backgroundColor = .yellow
    alert.cancelButtonIndex = 1
    alert.buttonTitleColor = .blue
}

Handle button clicked events

SwiftAlertView.show(title: "Title",
                    message: "Message",
                    buttonTitles: "Cancel", "OK") {
    $0.style = .dark
}
.onButtonClicked { _, buttonIndex in
    print("Button Clicked At Index \(buttonIndex)")
}

Add text fields

SwiftAlertView.show(title: "Sign in", buttonTitles: "Cancel", "Sign In") { alertView in
    alertView.addTextField { textField in
        textField.placeholder = "Username"
    }
    alertView.addTextField { textField in
        textField.placeholder = "Password"
    }
    alertView.isEnabledValidationLabel = true
    alertView.isDismissOnActionButtonClicked = false
}
.onActionButtonClicked { alertView, buttonIndex in
    let username = alert.textField(at: 0)?.text ?? ""
    if username.isEmpty {
        alertView.validationLabel.text = "Username is incorrect"
    } else {
        alertView.dismiss()
    }
}
.onTextChanged { _, text, textFieldIndex in
    if textFieldIndex == 0 {
        print("Username text changed: ", text ?? "")
    }
}

You can show alert with custom content view

// with xib file
SwiftAlertView.show(nibName: "CustomView", buttonTitles: "OK")

// with custom UIView
SwiftAlertView.show(contentView: customView, buttonTitles: "OK")

Programmatically creating an alert

Initialize an alert

let alertView = SwiftAlertView(title: "Title", message: "Message", buttonTitles: "Cancel", "Button 1", "Button 2", "Button 3")

let alertView = SwiftAlertView(contentView: customView, buttonTitles: "OK")

let alertView = SwiftAlertView(nibName: "CustomView", buttonTitles: "OK")

Show or dismiss

// Show at center of screen
alertView.show()

// Show at center of a view
alertView.show(in: view)

// Programmatically dismiss the alert view
alertView.dismiss()

Handle button clicked event

alertView.onButtonClicked { _, buttonIndex in
    print("Button Clicked At Index \(buttonIndex)")
}
alertView.onActionButtonClicked { _, buttonIndex in
    print("Action Button Clicked At Index \(buttonIndex)")
}

If you don't want to use closures, make your view controller conform SwiftAlertViewDelegate and use delegate methods:

alertView.delegate = self

func alertView(_ alertView: SwiftAlertView, clickedButtonAtIndex buttonIndex: Int) {
    println("Button Clicked At Index \(buttonIndex)")
}

func didPresentAlertView(_ alertView: SwiftAlertView) {
    println("Did Present Alert View")
}

func didDismissAlertView(_ alertView: SwiftAlertView) {
    println("Did Dismiss Alert View")
}

Customization

SwiftAlertView can be customized with the following properties:

public var style: Style = .auto // default is based on system color

public var titleLabel: UILabel! // access titleLabel to customize the title font, color
public var messageLabel: UILabel! // access messageLabel to customize the message font, color

public var backgroundImage: UIImage?
// public var backgroundColor: UIColor? // inherits from UIView

public var cancelButtonIndex = 0 // default is 0, set this property if you want to change the position of cancel button
public var buttonTitleColor = UIColor(red: 0, green: 0.478431, blue: 1, alpha: 1) // to change the title color of all buttons
public var buttonHeight: CGFloat = 44.0

public var separatorColor = UIColor(red: 196.0/255, green: 196.0/255, blue: 201.0/255, alpha: 1.0) // to change the separator color
public var isHideSeparator = false
public var cornerRadius: CGFloat = 12.0

public var isDismissOnActionButtonClicked = true // default is true, if you want the alert view will not be dismissed when clicking on action buttons, set this property to false
public var isHighlightOnButtonClicked = true
public var isDimBackgroundWhenShowing = true
public var isDismissOnOutsideTapped = false
public var dimAlpha: CGFloat = 0.4
public var dimBackgroundColor: UIColor? = .init(white: 0, alpha: 0.4)

public var appearTime = 0.2
public var disappearTime = 0.1

public var transitionType: TransitionType = .default

// customize the margin & spacing of title & message
public var titleSideMargin: CGFloat = 20.0
public var messageSideMargin: CGFloat = 20.0
public var titleTopMargin: CGFloat = 20.0
public var messageBottomMargin: CGFloat = 20.0
public var titleToMessageSpacing: CGFloat = 20.0

// customize text fields
public var textFieldHeight: CGFloat = 34.0
public var textFieldSideMargin: CGFloat = 15.0
public var textFieldBottomMargin: CGFloat = 15.0
public var textFieldSpacing: CGFloat = 10.0
public var isFocusTextFieldWhenShowing = true
public var isEnabledValidationLabel = false
public var validationLabel: UILabel! // access to validation label to customize font, color
public var validationLabelTopMargin: CGFloat = 8.0
public var validationLabelSideMargin: CGFloat = 15.0

Contributing

Contributions for bug fixing or improvements are welcomed. Feel free to submit a pull request. If you have any questions, feature suggestions or bug reports, please send me an email to [email protected].

swiftalertview's People

Contributors

dinhquan avatar jessearmand avatar quansmartosc avatar weichengwu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

swiftalertview's Issues

Question

Hi, i've a question for "cancelButtonTitle", can we don't use it(because i want to use custom cancel button on my xib), or it's a mandatory UI? Thank you

Feature request: add animation for a "pop" effect

I'd like to add an animation (transitionType) that makes the alert enlarge and then shrink to normal size using a spring animation. I'd be happy to create a PR, just wondering if this repo is still maintained and open to changes?

This view can not scroll?

By now, i have only used this view in simulator. when i add too many buttons in the view ,it looks like this:
simulator screen shot 2015 11 24 10 38 04

and i can not scroll the view to show other buttons, is there something i missed?

Memory Leak Issue Due to Retain Cycles in onButtonClicked, onActionButtonClicked, and onTextChanged

Issue Description:
I've noticed that the SwiftAlertView object is not getting deallocated properly due to strong reference cycles. Specifically, the onButtonClicked, onActionButtonClicked, and onTextChanged closure properties are causing a memory leak by retaining self strongly.

Suggested Fix:
To break the strong reference cycle, I've used [unowned self] in the closure. Here's how I've modified the code to fix the memory leak issue:

// handle events
@discardableResult
public func onButtonClicked(_ handler: @escaping (_ alertView: SwiftAlertView, _ buttonIndex: Int) -> Void) -> SwiftAlertView {
    self.onButtonClicked = { [unowned self] index in
        handler(self, index)
    }
    return self
}

@discardableResult
public func onActionButtonClicked(_ handler: @escaping (_ alertView: SwiftAlertView, _ buttonIndex: Int) -> Void) -> SwiftAlertView {
    self.onActionButtonClicked = { [unowned self] index in
        handler(self, index)
    }
    return self
}

@discardableResult
public func onTextChanged(_ handler: @escaping (_ alertView: SwiftAlertView, _ text: String?, _ textFieldIndex: Int) -> Void) -> SwiftAlertView {
    self.onTextChanged = { [unowned self] text, index in
        handler(self, text, index)
    }
    return self
}

I hope this helps to resolve the issue. Looking forward to your feedback.

Errors

When I installed the pod and opened my project I got a LOT of errors..
ended up by deleting the pod.

DO NOT DOWNLOAD THIS UNTIL THE AUTHOR UPDATE IT.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.