The iPhone struggles with providing useful information through the iOS notifications framework. Even something as simple as a calendar notification results in a screen takeover that most users probably don’t want to see.
A few months ago I posted a tutorial on how to create a custom iOS alert. This time I’ll show you how to create an advanced alert that mimics the ones found in OSX. To create the alert, we will use the following components:
When you first started communicating with the world via mobile devices, you probably received a sequence of notifications: texts, emails, and reminders. Today, notifications have become more interactive, and many apps even let you reply back to messages and keep you updated on the latest calls and messages.
LIHAlert
LIHAlert offers animated banners for iOS. Upgrade to Swift 3
Demonstration project
The LIHAlert workspace contains a demo project that is also used for development.
Requirements
Xcode 7+ (in English)
Setting
LIHAlert is available through CocoaPods. To install , simply add the following line to your podfile: under LIHAlert or Copy the LIHAlert folder to your project. For the swift 2 version pod ‘LIHAlert’, ‘~> 1.1.0’.
Use
Import the module with Import LIHAlert To run the example project, clone the repository and first run pod install from the Example folder.
Models
LIHAlert includes several predefined alarm templates for each alarm type. You can use the following code snippets to make use of it.
1. Text Banner
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) } Call the showBanner() function to display the banner.
2. Success message
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) } To change the icon, successAlert ?.icon = UIImage(named:imageName)
3. Error warning
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) } To change the icon, successAlert ?.icon = UIImage(named:imageName)
4. header
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. Load alarm
It is a banner with text and an activity indicator. It’s not a banner that closes automatically. Once the process is complete, it should be hidden. var processingAlert : LIHAlert ? override func viewDidLoad() { super.viewDidLoad() self.processingAlert = LIHAlertManager.getProcessingAlert(fetch data…) self.processingAlert ?.initAlert(self. view.view) } func showBanner(sender : AnyObject) { self.processingAlert ?.show(nil, hidden : nil) } override hideBanner(sender : AnyObject) { self.processingAlert ?.hide(nil) } Call showBanner() to show the banner and hideBanner() to hide it. To change the style of the activity view, processingAlert ?.activityIndicatorStyle = UIActivityIndicatorViewStyle.WhiteLarge
6. Text with warning button
This message contains a button and text. More suitable for notifying the user of important messages. var textWithButtonAlert : LIHAlert ? override func viewDidLoad() { super.viewDidLoad() self.textWithButtonAlert = LIHAlertManager.getTextWithButtonAlert(You have successfully signed up for the monthly newsletter, buttonText : Dismiss) self.textWithButtonAlert ?.initAlert(self.view) } func showBanner(sender : AnyObject) { textWithButtonAlert ?.show(nil, hidden : nil) } Call the showBanner() function to display the banner. Implement LIHAlertDelegate for keyboard events. ViewController class: LIHAlertDelegate { func buttonPressed(button : UIButton) { print(you pressed button) self.textWithButtonAlert ?.hideAlert(nil) } }
7. Two button warning text
This message contains two buttons and some text. var textWithTwoButtonsAlert : LIHAlert? override func viewDidLoad() { super.viewDidLoad() self.textWithTwoButtonsAlert = LIHAlertManager.getTextWithTwoButtonsAlert(Do you want to subscribe to the monthly newsletter? buttonOneText : Subscribe, buttonTwoText : Cancel) self.textWithTwoButtonsAlert ?.initAlert(self.view) } func showBanner(sender : AnyObject) { textWithTwoButtonsAlert ?.show(nil, hidden : nil) } Call the showBanner() function to display the banner. Implement LIHAlertDelegate for keyboard events. ViewController class: LIHAlertDelegate { func buttonOnePressed(button : UIButton) { self.textWithTwoButtonsAlert ?.hideAlert({ () -> () in self.successAlert ?.show(nil, hidden : nil) }) } func buttonTwoPressed(button : UIButton) { self.textWithTwoButtonsAlert ?.hideAlert(nil) } }
8. Custom display warning
Each performance can be specified as a banner. var customViewAlert : LIHAlert? override func viewDidLoad() { super.viewDidLoad() //In this case I am using ImageView as 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) }
To integrate theview controller
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) } See CustomAlertsViewController for more examples of including view controllers.
How to make your own banner
Release 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
Types of alerts
enum LIHAlertType { case Custom, Text, TextWithLoad, TextWithIcon, TextWithButton, TextWithTwoButton, TextWithTitle }
List of all objects
//delegates public var delegate : LIHAlertDelegate? // title public var titleLabel : UILabel ? public var titleText : String //Defaults to Sample Title public var titleTextColor : UIColor //Default UIColor.blackColor() public var titleTextFont: UIFont ? public var titleTextFontSize : CGFloat ? public var titleTextNumberOfLines : Int //Default 1 // content text public var contentLabel : UILabel ? public var contentText : String //Defaults to Sample Content public var contentTextColor : UIColor //Default UIColor.blackColor() public var contentTextFont: UIFont ? public var contentTextNumberOfLines : Int //Default 2 //TextWithLoading public var activityIndicatorStyle : UIActivityIndicatorViewStyle // Default UIActivityIndicatorViewStyle.White //Icoon public var iconImageView : UIImageView ? public var icon : You’re kidding? //A single button public var button_textWithButton : UIButton ? public var buttonText : String //Dismiss public var buttonColor : UIColor //Default UIColor.blueColor() public var buttonTextColor : UIColor //The default value is UIColor.whiteColor() public var buttonFont: UIFont ? public var buttonBordercolor : UIColor //Default UIColor.whiteColor() public var buttonBorderWidth : CGFloat //Default 1.0 public var buttonCornerRadius : CGFloat //Default 3.0 public var buttonWidth : CGFloat ? //TWO BUTTONS //ButtonOne public var buttonOne_textWithButton : UIButton ? public var buttonOneText : String //Dismiss public var buttonOneColor : UIColor //Default UIColor.blueColor() public var buttonOneTextColor : UIColor //Default UIColor.whiteColor() public var buttonOneFont: UIFont ? public var buttonOneBordercolor : UIColor //Default UIColor.whiteColor() public var buttonOneBorderWidth : CGFloat //Default 1.0 public var buttonOneCornerRadius : CGFloat //Default 3.0 //ButtonTwo public var buttonTwo_textWithButton : UIButton ? public var buttonTwoText : String //Default is Dismiss public var buttonTwoColor : UIColor //Default UIColor.blueColor() public var buttonTwoTextColor : UIColor //Default UIColor.whiteColor() public var buttonTwoFont: UIFont ? public var buttonTwoBordercolor : UIColor //Default UIColor.whiteColor() public var buttonTwoBorderWidth : CGFloat //Default 1.0 public var buttonTwoCornerRadius : CGFloat //Default 3.0 //AlertView public var alertView : UIView ? public var alertColor : UIColor //The default value is UIColor.grayColor() public var alertAlpha : CGFloat /Standard at 1.0 public var alertHeight:CGFloat /Standard at 75.0 public var paddingLeft : CGFloat //Default 0.0 public vardingTop : CGFloat //Default 0.0 public var animationDuration : NSTimeInterval //Default 0.5 public var alertType : LIHAlertType //Default LIHAlertType.Text public var autoCloseEnabled : Bool //Default true public var autoCloseTimeInterval : Double //Default 3.0 public var hasNavigationBar : Bool //Default false public var touchBackgroundToDismiss : Bool // default value is false public var dimsBackground : Bool //default false public var backgroundDimOpacity : CGFloat // standard 0.4
Use exit reminders
//when the auto-hide banner is displayed lihAlert?.show({ () -> () in //the alert is displayed }, hidden: { () -> () in //the alert is hidden }) /hides banner lihAlert?.hideAlert({ () -> () in /hides banner })
GitHub
https://github.com/Lasithih/LIHAlertApple’s native alerts have a certain quality. They’re easy to understand, especially when they’re animated. But native alerts often suffer from a lack of symmetry, and this can be a problem if you’re trying to create a consistent visual experience across multiple screens. This post shows how to add advanced animated alerts to your app, using Swift, and without native code.. Read more about ios alerts and let us know what you think.
Related Tags:
ios custom popup dialogshow uiview as popup ios, swiftalertview swiftios alertsalert github iosswiftui alert library,People also search for,Privacy settings,How Search works,ios custom popup dialog,show uiview as popup ios, swift,alertview swift,ios alerts,alert github ios,swiftui alert library,alertview in swift github