All Products
Search
Document Center

Mobile Platform as a Service:Action sheet

Last Updated:Mar 07, 2022

AUActionSheet is migrated from APActionSheet. The style is slightly adjusted, supporting the common layout with the deletion button and the common sheet layout.

Sample images

  • Common layout with the delete button:

    Confirm deletion
  • Tab layout:

    option 1
  • Badges:

    Normal Action Sheet

Dependency

The dependency of AUActionSheet is as follows:

    AntUI(iOS)
    1.0.0.161108003457
    APCommonUI(iOS)
    1.2.0.161108102201

API description

typedef NS_ENUM(NSInteger, AUActionSheetButtonType) {
AUActionSheetButtonTypeDefault = 0,         // The default type.
AUActionSheetButtonTypeDisabled,            // The button cannot be tapped.
AUActionSheetButtonTypeDestructive,         // The red destructive button.
AUActionSheetButtonTypeCustom               // The customized type.
};

/**
AUActionSheet The API is migrated from APActionSheet, and the style is adjusted.
*/
@interface AUActionSheet: UIView<UIAppearanceContainer>

/// The button height is 42 by default.
@property (nonatomic) CGFloat buttonHeight UI_APPEARANCE_SELECTOR;
/// The height of the Cancel button.
@property (nonatomic) CGFloat cancelButtonHeight UI_APPEARANCE_SELECTOR;
/// The color of the separation line, which is AU_COLOR_LINE by default.
@property (strong, nonatomic) UIColor *separatorColor UI_APPEARANCE_SELECTOR;
/// The background color of a tapped button.
@property (strong, nonatomic) UIColor *selectedBackgroundColor UI_APPEARANCE_SELECTOR;
// The attributes of UI components.
@property (copy, nonatomic) NSDictionary *titleTextAttributes UI_APPEARANCE_SELECTOR;
@property (copy, nonatomic) NSDictionary *buttonTextAttributes UI_APPEARANCE_SELECTOR;
@property (copy, nonatomic) NSDictionary *disabledButtonTextAttributes UI_APPEARANCE_SELECTOR;
@property (copy, nonatomic) NSDictionary *destructiveButtonTextAttributes UI_APPEARANCE_SELECTOR;
@property (copy, nonatomic) NSDictionary *cancelButtonTextAttributes UI_APPEARANCE_SELECTOR;

/// The duration for showing or hiding an animation, which is 0.5s by default.
@property (nonatomic) NSTimeInterval animationDuration UI_APPEARANCE_SELECTOR;
/// The title.
@property(nonatomic,copy) NSString *title;
/// Whether the item is visible
@property(nonatomic, readonly, getter=isVisible) BOOL visible;
/// The header view of a custom button.
@property (strong, nonatomic) UIView *headerView;
/// The keyWindow before the ActionSheet instance is displayed.
@property (weak, nonatomic, readonly) UIWindow *previousKeyWindow;
/// The protocol delegate.
@property(nonatomic,weak)id<UIActionSheetDelegate> delegate;
/// The title of the Cancel button.
@property (copy, nonatomic) NSString *cancelButtonTitle;
/// The number of buttons.
@property(nonatomic, readonly) NSInteger numberOfButtons;
/// The index of the Cancel button, which is -1 by default.
@property(nonatomic) NSInteger cancelButtonIndex;
/// The index of a destructive red button, which is -1 by default and can be ignored if only one button exists.<UActionSheetButtonTypeDestructive,         // The red destructive button.> 
@property(nonatomic) NSInteger destructiveButtonIndex;
/**
The AUActionSheet initialization method

@param title                    The title information.
@param delegate                 The delegate object.
@param cancelButtonTitle        The title of the Cancel button.
@param destructiveButtonTitle   The title of a destructive button.
@param otherButtonTitles        The list of other button title parameters.
@return The AUActionSheet instance.
*/
- (instancetype)initWithTitle:(NSString *)title delegate:(id<UIActionSheetDelegate>)delegate cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION;

/**
The AUActionSheet initialization method.

@param title                    The title information.
@param delegate                 The delegate object.
@param cancelButtonTitle        The title of the Cancel button.
@param destructiveButtonTitle   The title of a destructive button.
@param items                    The customOption data list (with custom title colors and badges).
@return                         The AUActionSheet instance.
*/
- (instancetype)initWithTitle:(NSString *)title
delegate:(id<UIActionSheetDelegate>)delegate
cancelButtonTitle:(NSString *)cancelButtonTitle
destructiveButtonTitle:(NSString *)destructiveButtonTitle
items:(NSArray<AUActionSheetItem *> *)items;

/**
Add a button of a default type.

@param title    The button title.
@return         The button index, starting from 0.
*/
- (NSInteger)addButtonWithTitle:(NSString *)title;

/**
Add a button.

@param title    The button title.
@param type     The button type.
@return         The button index, starting from 0.
*/
- (NSInteger)addButtonWithTitle:(NSString *)title type:(AUActionSheetButtonType)type;

/**
Obtain the button title based on the index.

@param buttonIndex  The button index.
@return             The button title.
*/
- (NSString *)buttonTitleAtIndex:(NSInteger)buttonIndex;

/**
Set a button in a position.

@param item The button type after information encapsulation.
@param index The index of the button to be replaced. The index is less than the number of existing buttons.
*/
- (void)setButton:(AUActionSheetItem *)item atIndex:(NSInteger)index;

/** The ActionSheet display method. */
- (void)show;

/**
Manually call the hiding method.

@param animate Whether a hiding animation is available.
*/
- (void)closeWithAnimate:(BOOL)animate;

/**
Manually simulate the hiding method based on button tapping (a protocol method related to button tapping is called back).

@param buttonIndex  The button index.
@param animated     Whether a hiding animation is available.
*/
- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated;

/**
* Dynamically add an item.
* Note: Call this method after the actionSheet is shown on the screen. To add a button before the action sheet is shown, use addButtonWithTitle.
*
* @param item   The custom item.
* @param index  The position where the item is added.
*/
- (void)addButton:(AUActionSheetItem *)item atIndex:(NSInteger)index;

// Set the background mode. If the value is YES or @(YES), all displayed action sheets are hidden. The default value is NO.
+(void)setIsBackGroundMode:(BOOL)isBackGroundMode;
+(void)weakSetIsBackGroundMode:(id)isBackGroundMode;

- (void)showFromToolbar:(UIToolbar *)view;
- (void)showFromTabBar:(UITabBar *)view;
- (void)showFromBarButtonItem:(UIBarButtonItem *)item animated:(BOOL)animated NS_AVAILABLE_IOS(3_2);
- (void)showFromRect:(CGRect)rect inView:(UIView *)view animated:(BOOL)animated NS_AVAILABLE_IOS(3_2);
- (void)showInView:(UIView *)view;

@end


/** The ActionSheet button class after encapsulation. */
@interface AUActionSheetItem: NSObject
/// The button title.
@property (copy, nonatomic) NSString *title;
/// The button type.
@property (nonatomic) AUActionSheetButtonType type;
/// The color of the button title. When you set this value, manually change the button type to AUActionSheetButtonTypeCustom.
@property (strong,nonatomic) UIColor *titleColor;

/**
* Set the style for displaying badges.
*
*        badgeValue:  @"."   A red dot is displayed.
*                     @"new" "new" is displayed.
*                     @"digit" A digit is displayed. If the digit is larger than 99, the more (...) image is displayed.
*                     @"a Chinese character for "xin""  "xin" is displayed.
*                     @"a Chinese character for "hui""  "hui" is displayed.
*                     nil    Clear the displayed content.
*/
@property (nonatomic, copy) NSString *badgeValue;

@end

Code sample

  • Common layout with the delete button:

AUActionSheet *actionSheet = [[AUActionSheet alloc] initWithTitle:@ "Provide one or two lines of comments for information classification."
                                                             delegate:self
                                                    cancelButtonTitle:@"Cancel"
                                               destructiveButtonTitle:@"Delete"
                                                    otherButtonTitles:nil];
[actionSheet show];
  • Tab layout:

AUActionSheet *actionSheet = [[AUActionSheet alloc] initWithTitle:nil
                                                             delegate:self
                                                    cancelButtonTitle:@"Cancel"
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:@"Option 1",@"Option 2",@"Option 3", nil];
[actionSheet show];
  • Add a badge to an option:

    AUActionSheet *actionSheet = [[AUActionSheet alloc] initWithTitle:nil
                                                             delegate:self
                                                    cancelButtonTitle:@"Cancel"
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:@"Option 1",@"Option 2",@"Option 3", nil];
    AUActionSheetItem *item = [[AUActionSheetItem alloc] init];
    item.title = @"Option 3";
    item.type = AUActionSheetButtonTypeCustom;
    item.badgeValue = @"new";
    item.titleColor = [UIColor redColor];
    [actionSheet setButton:item atIndex:2];

    [actionSheet show];