Customizable implementation of UIAlertViewController, UIAlertView and UIActionSheet. All in one. You can customize every detail. Make AlertView of your dream! :)
LGAlertView version | iOS version |
---|---|
<= 2.0.13 | >= 6.0 |
>= 2.1.0 | >= 8.0 |
Download repository, then add LGAlertView directory to your project.
Then import header files where you need to use the library
#import "LGAlertView.h"
For swift you need to create bridging header
// BridgingHeader.h
#import "LGAlertView.h"
CocoaPods is a dependency manager for Objective-C, which automates and simplifies the process of using 3rd-party libraries in your projects. To install with cocoaPods, follow the "Get Started" section on CocoaPods.
platform :ios, '8.0'
use_frameworks!
pod 'LGAlertView'
Then import framework where you need to use the library
#import <LGAlertView/LGAlertView.h>
// OR
@import LGAlertView;
import LGAlertView
Carthage is a lightweight dependency manager for Swift and Objective-C. It leverages CocoaTouch modules and is less invasive than CocoaPods. To install with carthage, follow the instruction on Carthage.
github "Friend-LGA/LGAlertView"
Then import framework where you need to use the library
#import <LGAlertView/LGAlertView.h>
// OR
@import LGAlertView;
import LGAlertView
You have several methods for initialization:
- (nonnull instancetype)initWithTitle:(nullable NSString *)title
message:(nullable NSString *)message
style:(LGAlertViewStyle)style
buttonTitles:(nullable NSArray<NSString *> *)buttonTitles
cancelButtonTitle:(nullable NSString *)cancelButtonTitle
destructiveButtonTitle:(nullable NSString *)destructiveButtonTitle;
- (nonnull instancetype)initWithViewAndTitle:(nullable NSString *)title
message:(nullable NSString *)message
style:(LGAlertViewStyle)style
view:(nullable UIView *)view
buttonTitles:(nullable NSArray<NSString *> *)buttonTitles
cancelButtonTitle:(nullable NSString *)cancelButtonTitle
destructiveButtonTitle:(nullable NSString *)destructiveButtonTitle;
- (nonnull instancetype)initWithActivityIndicatorAndTitle:(nullable NSString *)title
message:(nullable NSString *)message
style:(LGAlertViewStyle)style
buttonTitles:(nullable NSArray<NSString *> *)buttonTitles
cancelButtonTitle:(nullable NSString *)cancelButtonTitle
destructiveButtonTitle:(nullable NSString *)destructiveButtonTitle;
- (nonnull instancetype)initWithProgressViewAndTitle:(nullable NSString *)title
message:(nullable NSString *)message
style:(LGAlertViewStyle)style
progressLabelText:(nullable NSString *)progressLabelText
buttonTitles:(nullable NSArray<NSString *> *)buttonTitles
cancelButtonTitle:(nullable NSString *)cancelButtonTitle
destructiveButtonTitle:(nullable NSString *)destructiveButtonTitle;
- (nonnull instancetype)initWithTextFieldsAndTitle:(nullable NSString *)title
message:(nullable NSString *)message
numberOfTextFields:(NSUInteger)numberOfTextFields
textFieldsSetupHandler:(LGAlertViewTextFieldsSetupHandler)textFieldsSetupHandler
buttonTitles:(nullable NSArray<NSString *> *)buttonTitles
cancelButtonTitle:(nullable NSString *)cancelButtonTitle
destructiveButtonTitle:(nullable NSString *)destructiveButtonTitle;
public init(title: String?,
message: String?,
style: LGAlertViewStyle,
buttonTitles: [String]?,
cancelButtonTitle: String?,
destructiveButtonTitle: String?)
public init(viewAndTitle title: String?,
message: String?,
style: LGAlertViewStyle,
view: UIView?,
buttonTitles: [String]?,
cancelButtonTitle: String?,
destructiveButtonTitle: String?)
public init(activityIndicatorAndTitle title: String?,
message: String?,
style: LGAlertViewStyle,
buttonTitles: [String]?,
cancelButtonTitle: String?,
destructiveButtonTitle: String?)
public init(progressViewAndTitle title: String?,
message: String?,
style: LGAlertViewStyle,
progressLabelText: String?,
buttonTitles: [String]?,
cancelButtonTitle: String?,
destructiveButtonTitle: String?)
public init(textFieldsAndTitle title: String?,
message: String?,
numberOfTextFields: UInt,
textFieldsSetupHandler: LGAlertView.LGAlertViewTextFieldsSetupHandler?,
buttonTitles: [String]?,
cancelButtonTitle: String?,
destructiveButtonTitle: String?)
More init methods you can find in LGAlertView.h
You can change properties only before you show alert view, after this to change something is impossible.
Instead of change properties for every new alert view, you can use global class properties to set them all only once and new alert views will use it by default:
LGAlertView.tintColor = UIColor.greenColor;
LGAlertView.cancelOnTouch = NO;
LGAlertView.dismissOnAction = NO;
LGAlertView...
LGAlertView...
LGAlertView.tintColor = .green
LGAlertView.cancelOnTouch = false
LGAlertView.dismissOnAction = false
LGAlertView...
LGAlertView...
If you want to set properties for each button individually, you can use method:
- (void)setButtonPropertiesAtIndex:(NSUInteger)index handler:(void(^ _Nonnull)(LGAlertViewButtonProperties * _Nonnull properties))handler;
[alertView setButtonPropertiesAtIndex:0 handler:^(LGAlertViewButtonProperties * _Nonnull properties) {
properties.titleColor = UIColor.yellowColor;
properties.image = [UIImage imageNamed:@"SuperImage"];
// properties...
// properties...
}];
open func setButtonPropertiesAt(_ index: UInt, handler: @escaping (LGAlertViewButtonProperties) -> Swift.Void)
alertView.setButtonPropertiesAt(0) { (properties: LGAlertViewButtonProperties) in
properties.titleColor = .yellow
properties.image = UIImage(named: "SuperImage")
// properties...
// properties...
}
You can enable and disable buttons:
alertView.cancelButtonEnabled = YES;
alertView.destructiveButtonEnabled = YES;
[alertView setButtonEnabled:YES atIndex:0];
alertView.cancelButtonEnabled = true
alertView.destructiveButtonEnabled = true
alertView.setButtonEnabled(true, index: 0)
When you use blocks and if you need to use self
inside it, then you need to make weak reference to self
to avoid retain cycle:
__weak typeof(self) wself = self;
alertView.cancelHandler = ^(LGAlertView *alertView) {
__strong typeof(wself) sself = wself;
[sself someMethod];
};
alertView.cancelHandler = { [unowned self](alertView: LGAlertView) in
self.someMethod()
}
You can use UIBlurEffect with next properties:
UIBlurEffect *coverBlurEffect;
For example:
alertView.coverBlurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleRegular];
alertView.coverBlurEffect = UIBlurEffect(style: .regular)
If you want to change color of blurred view, use:
UIColor *coverColor;
For example:
alertView.coverColor = [UIColor colorWithRed:0.0 green:0.5 blue:1.0 alpha:0.1];
alertView.coverColor = UIColor(red: 0.0, green: 0.5, blue: 1.0, alpha: 0.1)
If you want to change intensity of blurred view, use:
CGFloat coverAlpha;
For example:
alertView.coverAlpha = 0.9;
To handle actions you can use blocks, delegate or notifications:
<LGAlertViewDelegate>
@optional
- (void)alertViewWillShow:(LGAlertView *)alertView;
- (void)alertViewWillDismiss:(LGAlertView *)alertView;
- (void)alertViewDidShow:(LGAlertView *)alertView;
- (void)alertViewDidDismiss:(LGAlertView *)alertView;
- (void)alertView:(LGAlertView *)alertView buttonPressedWithTitle:(NSString *)title index:(NSUInteger)index;
- (void)alertViewCancelled:(LGAlertView *)alertView;
- (void)alertViewDestructiveButtonPressed:(LGAlertView *)alertView;
<LGAlertViewDelegate>
optional public func alertViewWillShow(_ alertView: LGAlertView)
optional public func alertViewDidShow(_ alertView: LGAlertView)
optional public func alertViewWillDismiss(_ alertView: LGAlertView)
optional public func alertViewDidDismiss(_ alertView: LGAlertView)
optional public func alertView(_ alertView: LGAlertView, buttonPressedWithTitle title: String?, index: UInt)
optional public func alertViewCancelled(_ alertView: LGAlertView)
optional public func alertViewDestructiveButtonPressed(_ alertView: LGAlertView)
void(^ _Nullable willShowHandler)(LGAlertView * _Nonnull alertView);
void(^ _Nullable willShowHandler)(LGAlertView * _Nonnull alertView);
void(^ _Nullable didShowHandler)(LGAlertView * _Nonnull alertView);
void(^ _Nullable willDismissHandler)(LGAlertView * _Nonnull alertView);
void(^ _Nullable didDismissHandler)(LGAlertView * _Nonnull alertView);
void(^ _Nullable actionHandler)(LGAlertView * _Nonnull alertView, NSString * _Nullable title, NSUInteger index);
void(^ _Nullable cancelHandler)(LGAlertView * _Nonnull alertView);
void(^ _Nullable destructiveHandler)(LGAlertView * _Nonnull alertView);
open var willShowHandler: ((LGAlertView) -> Swift.Void)?
open var didShowHandler: ((LGAlertView) -> Swift.Void)?
open var willDismissHandler: ((LGAlertView) -> Swift.Void)?
open var didDismissHandler: ((LGAlertView) -> Swift.Void)?
open var actionHandler: ((LGAlertView, NSString, NSUInteger) -> Swift.Void)?
open var cancelHandler: ((LGAlertView) -> Swift.Void)?
open var destructiveHandler: ((LGAlertView) -> Swift.Void)?
LGAlertViewWillShowNotification
LGAlertViewDidShowNotification
LGAlertViewWillDismissNotification
LGAlertViewDidDismissNotification
LGAlertViewActionNotification
LGAlertViewCancelNotification
LGAlertViewDestructiveNotification
For more details try Xcode Demo project and see LGAlertView.h
If you like LGAlertView, check out my other useful libraries:
- LGSideMenuController iOS view controller, shows left and right views by pressing button or gesture.
- LGPlusButtonsView Customizable iOS implementation of Floating Action Button (Google Plus Button, fab).
LGAlertView is released under the MIT license. See LICENSE for details.