AUSegment provides a scrollable tab bar.
AUSegment is not fully interchangeable with APSegmentedControl in APCommonUI. This is because APSegmentedControl only encapsulates the system's UISegmentedControl and does not provide other features.
Preview

Dependencies
AUSegment has the following dependencies:
import "AUSegmentedControlItem.h"API reference
@protocol AUSegmentedControlDelegate <UIScrollViewDelegate>
// Callback for AUSegment tap events.
@optional
- (void)didSegmentValueChanged:(AUSegment*)segmentControl;
- (void)didSelectSegmentItemModel:(AUSegmentItemModel*)selectedItemModel;
@end
// Default height of the segment.
#define AUSegmentHeight AU_SPACE13
/**
A segmented control component.
*/
@interface AUSegment : UIScrollView
/**
Initializes the component.
@param frame The frame.
@param titles An array that contains all title strings.
@return An AUSegment instance.
*/
- (instancetype)initWithFrame:(CGRect)frame titles:(NSArray<NSString*> *)titles;
/**
Disables the init method.
*/
- (instancetype)init NS_UNAVAILABLE;
/**
Disables the initWithFrame method.
*/
- (instancetype)initWithFrame:(CGRect)frame NS_UNAVAILABLE;
/**
The AUSegmentedControlDelegate.
*/
@property (nonatomic, weak) id <AUSegmentedControlDelegate> delegate;
/**/
/**
The title array.
*/
@property (nonatomic, strong) NSMutableArray *titles;
/**
* The menu font.
*/
@property (nonatomic, copy) UIFont *titleFont;
/**
The index of the currently selected segment.
*/
@property (nonatomic, assign) NSInteger selectedSegmentIndex;
/**
The color of the selected item, including the text and slider.
*/
@property (nonatomic, copy) UIColor *selecedColor;
/**
* The horizontal margin on the left and right of each text menu.
* The default value is 21 pixels.
* If the red dot style is used, the fixedItemWidth property does not take effect and the menus do not have a fixed width.
*/
@property(nonatomic, assign) NSInteger textHorizontalPadding;
/**
* Specifies whether to use a fixed width for menus.
* The default value is YES for compatibility with older menu styles.
* If this property is set to YES, the horizontalPadding property does not take effect and all menus have the same fixed width.
*/
@property (nonatomic, assign) BOOL fixedItemWidth;
/**
* Specifies whether to automatically scroll the selected menu item to a proper position. The item is centered by default. If there is not enough space, the item is aligned to the edge.
* The default value is NO.
*/
@property (nonatomic, assign) BOOL autoScroll;
/**
* Specifies whether to automatically update the indicator bar to the index of the currently selected item after a tap.
* The default value is YES.
*/
@property (nonatomic, assign) BOOL autoChangeSelectedIndex;
/*
* The model array.
*/
@property(nonatomic, strong) NSMutableArray<AUSegmentItemModel *> *itemModels;
/**
Inserts multiple items at specified indexes.
@param array The array of titles to insert.
@param indexes The indexes at which to insert the items.
*/
- (void)insertTitleArray:(NSArray<NSString*> *)array atIndexes:(NSIndexSet *)indexes;
/**
Appends multiple items to the end.
@param array The array of titles to add.
*/
- (void)addTitleArray:(NSArray<NSString*>*)array;
/**
* Automatically scrolls to the item at the specified index. Note: This only scrolls the item into view. The selection indicator does not change.
* By default, this is the same as selectedSegmentIndex, which is the selected item.
*/
- (void)autoScrollToIndex:(NSInteger)index;
- (BOOL)segmentItemIsInVisualAear:(NSInteger)index;
@end
@interface AUSegmentItemModel : NSObject
@property(nonatomic, copy) NSString *title;
@property(nonatomic, copy) UIImage *img;
@property(nonatomic, copy) NSString *imgId;
@property(nonatomic, copy) NSString *badgeNumber;
@property(nonatomic, copy) NSString *badgeWidgetId;
@property(nonatomic, assign) BOOL badgeReserved; // Specifies whether to reserve space for a red dot on the current item. If no space is reserved, the interface may flicker when the red dot is displayed.
@property(nonatomic, strong) NSDictionary *extendInfo; // Extension field.
@end
@interface AUSegment (ItemModel)
/**
* The second version of the initializer function.
* @param frame The frame.
* @param menus The item array.
*/
- (instancetype) initWithFrame:(CGRect)frame menus:(NSArray<AUSegmentItemModel *>*)menus;
/**
Updates the control items.
@param items The array of items to update. Use this to add, delete, or update all existing model data.
*/
- (void)updateItems:(NSArray<AUSegmentItemModel *>*)items;
/**
Updates the item model at a specific index.
@param model The new item model.
@param index The index of the item to update.
*/
- (void)updateItemModel:(AUSegmentItemModel *)model
atIndex:(NSInteger)index;
@end
// Displays an action icon button on the right. A plus sign (+) is displayed by default.
@interface AUSegment (AUActionIcon)
- (void)showActionIcon:(BOOL)showIcon target:(id)target action:(SEL)action;
@endCustom properties
Property | Purpose | Type |
titles | The title array of the segment. | NSArray |
selectedSegmentIndex | The selected segment. | NSInteger |
delegate | Implements the AUSegmentedControlDelegate protocol. | id |
autoScroll | Specifies whether to automatically scroll the selected menu item to a proper position. The item is centered by default. If there is not enough space, the item is aligned to the edge. | BOOL |
fixedItemWidth | Specifies whether to use a fixed width for menus. | BOOL |
textHorizontalPadding | The horizontal margin on the left and right of each text menu. | BOOL |
titleFont | A custom menu font. | UIFont |
Code examples
Segmented control without red dots:
NSArray *testArray1 = @[@"tab1",@"tab2",@"tab3",@"tab4",@"tab5",@"tab6",@"tab7",@"tab8"]; AUSegment *segment = [[AUSegment alloc] initWithFrame:CGRectMake(0, 300, self.view.width, 44) titles:testArray1]; segment.delegate = self; [self.view addSubview:segment]; // Callback - (void)didSegmentValueChanged:(AUSegment*)segmentControl { NSLog(@"AUSegmented switched"); }Segmented control with red dots:
NSMutableArray *array = [[NSMutableArray alloc] init]; for (int i=0; i<7; i++) { AUSegmentItemModel *model = [[AUSegmentItemModel alloc] init]; model.title = [NSString stringWithFormat:@"Option %d", i]; if (i == 0) { model.badgeNumber = @"."; } if (i == 1) { model.badgeNumber = @"new"; } if (i == 6) { model.badgeNumber = @"6"; } model.badgeReserved = YES; [array addObject:model]; } AUSegment *segment2 = [[AUSegment alloc] initWithFrame:CGRectMake(0, topMargin, self.view.width, 44) menus:array]; [self.view addSubview:segment2]; [segment2 autoScrollToIndex:6]; segment2.backgroundColor = [UIColor whiteColor]; [segment2 showActionIcon:YES target:self action:@selector(clickActionIcon:)];