UIAlertView、UIAlertView和UIActionSheet的可定制实现。全部集成。您可以定制每一处细节。_available your dream AlertView! :)
LGAlertView版本 | iOS版本 |
---|---|
<= 2.0.13 | >= 6.0 |
>= 2.1.0 | >= 8.0 |
下载仓库https://github.com/Friend-LGA/LGAlertView/archive/master.zip,然后将LGAlertView目录添加到您的项目中。
然后,将所需的头文件导入到您需要使用库的地方
#import "LGAlertView.h"
对于Swift,您需要创建桥接头文件
// BridgingHeader.h
#import "LGAlertView.h"
platform :ios, '8.0'
use_frameworks!
pod 'LGAlertView'
然后,导入您需要使用库的地方所需框架
#import <LGAlertView/LGAlertView.h>
// OR
@import LGAlertView;
import LGAlertView
github "Friend-LGA/LGAlertView"
然后,导入您需要使用库的地方所需框架
#import <LGAlertView/LGAlertView.h>
// OR
@import LGAlertView;
import LGAlertView
您有几种初始化方法
- (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?)
更多初始化方法请见LGAlertView.h
你只能在显示alert视图之前更改属性,之后更改将无法进行。
而不是为每个新的alert视图更改属性,你可以使用appearance
一次设置它们,并且新的alert视图将默认使用这些设置
[LGAlertView appearance].tintColor = UIColor.greenColor;
[LGAlertView appearance].cancelOnTouch = NO;
[LGAlertView appearance].dismissOnAction = NO;
[LGAlertView appearance]...
[LGAlertView appearance]...
LGAlertView.appearance().tintColor = .green
LGAlertView.appearance().cancelOnTouch = false
LGAlertView.appearance().dismissOnAction = false
LGAlertView.appearance()...
LGAlertView.appearance()...
如果你想为每个按钮单独设置属性,你可以使用该方法
- (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...
}
你可以启用和禁用按钮
alertView.cancelButtonEnabled = YES;
alertView.destructiveButtonEnabled = YES;
[alertView setButtonEnabled:YES atIndex:0];
alertView.cancelButtonEnabled = true
alertView.destructiveButtonEnabled = true
alertView.setButtonEnabled(true, index: 0)
当你使用代码块并在其中需要使用self
时,你需要将self
设置为弱引用以避免保留周期
__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()
}
你可以使用具有以下属性的UIBlurEffect
UIBlurEffect *coverBlurEffect;
例如
alertView.coverBlurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleRegular];
alertView.coverBlurEffect = UIBlurEffect(style: .regular)
如果你想更改模糊视图的颜色,使用
UIColor *coverColor;
例如
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)
如果你想更改模糊视图的强度,使用
CGFloat coverAlpha;
例如
alertView.coverAlpha = 0.9;
要处理操作,你可以使用代码块、代理或通知
<LGAlertViewDelegate>
@optional
- (void)alertViewWillShow:(nonnull LGAlertView *)alertView;
- (void)alertViewDidShow:(nonnull LGAlertView *)alertView;
- (void)alertViewWillDismiss:(nonnull LGAlertView *)alertView;
- (void)alertViewDidDismiss:(nonnull LGAlertView *)alertView;
- (void)alertView:(nonnull LGAlertView *)alertView clickedButtonAtIndex:(NSUInteger)index title:(nullable NSString *)title;
- (void)alertViewCancelled:(nonnull LGAlertView *)alertView;
- (void)alertViewDestructed:(nonnull LGAlertView *)alertView;
- (void)alertView:(nonnull LGAlertView *)alertView didDismissAfterClickedButtonAtIndex:(NSUInteger)index title:(nullable NSString *)title;
- (void)alertViewDidDismissAfterCancelled:(nonnull LGAlertView *)alertView;
- (void)alertViewDidDismissAfterDestructed:(nonnull 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, clickedButtonAtIndex index: UInt, title: String?)
optional public func alertViewCancelled(_ alertView: LGAlertView)
optional public func alertViewDestructed(_ alertView: LGAlertView)
optional public func alertView(_ alertView: LGAlertView, didDismissAfterClickedButtonAtIndex index: UInt, title: String?)
optional public func alertViewDidDismissAfterCancelled(_ alertView: LGAlertView)
optional public func alertViewDidDismissAfterDestructed(_ 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, NSUInteger index, NSString * _Nullable title);
void(^ _Nullable cancelHandler)(LGAlertView * _Nonnull alertView);
void(^ _Nullable destructiveHandler)(LGAlertView * _Nonnull alertView);
void(^ _Nullable didDismissAfterActionHandler)(LGAlertView * _Nonnull alertView, NSUInteger index, NSString * _Nullable title);
void(^ _Nullable didDismissAfterCancelHandler)(LGAlertView * _Nonnull alertView);
void(^ _Nullable didDismissAfterDestructiveHandler)(LGAlertView * _Nonnull alertView);
open var willShowHandler: ((alertView: LGAlertView) -> Swift.Void)?
open var didShowHandler: ((alertView: LGAlertView) -> Swift.Void)?
open var willDismissHandler: ((alertView: LGAlertView) -> Swift.Void)?
open var didDismissHandler: ((alertView: LGAlertView) -> Swift.Void)?
open var actionHandler: ((alertView: LGAlertView, index: NSUInteger, title: NSString) -> Swift.Void)?
open var cancelHandler: ((alertView: LGAlertView) -> Swift.Void)?
open var destructiveHandler: ((alertView: LGAlertView) -> Swift.Void)?
open var didDismissAfterActionHandler: ((alertView: LGAlertView, index: NSUInteger, title: NSString) -> Swift.Void)?
open var didDismissAfterCancelHandler: ((alertView: LGAlertView) -> Swift.Void)?
open var didDismissAfterDestructiveHandler: ((alertView: LGAlertView) -> Swift.Void)?
LGAlertViewWillShowNotification
LGAlertViewDidShowNotification
LGAlertViewWillDismissNotification
LGAlertViewDidDismissNotification
LGAlertViewActionNotification
LGAlertViewCancelNotification
LGAlertViewDestructiveNotification
LGAlertViewDidDismissAfterActionNotification;
LGAlertViewDidDismissAfterCancelNotification;
LGAlertViewDidDismissAfterDestructiveNotification;
如需更多信息,请尝试Xcode中的演示项目并列出LGAlertView.h
如果您喜欢LGAlertView,请查看我其他有用的库
LGAlertView遵守MIT许可证发布。有关详细信息,请参阅LICENSE