YUSegment
A customizable segmented control for iOS.
Features
- Supports both (Attributed)text and image
- Supports show separator
- Supports hide indicator
- Indicator could be located at the top and bottom
- YUSegment works on iOS 8.0+ and is compatible with ARC projects
Installation
Cocoapods
- Add a pod entry to your Podfile
pod 'YUSegment'
- Running
pod install
#import <YUSegment/YUSegment.h>
where you need
Carthage
github "afishhhhh/YUSegment"
Usage
YUSegmentedControl
inherits from UIControl
, supports Target-Action
.
YUSegmentedControl *segmentedControl = [[YUSegmentedControl alloc] initWithTitles:@[@"健身", @"摄影", @"科技", @"美食", @"旅行"]];
[self.view addSubview:segmentedControl];
[segmentedControl addTarget:self action:@selector(segmentedControlTapped:) forControlEvents:UIControlEventValueChanged];
By default, the background color of segmented control is whiteColor
, the color of indicator is darkGrayColor
, the height of indicator is 3.0, the color of separator is #e7e7e7
.
APIs
Methods
- (instancetype)initWithTitles:(NSArray <NSString *> *)titles;
- (instancetype)initWithImages:(NSArray <UIImage *> *)images;
- (instancetype)initWithImages:(NSArray <UIImage *> *)images
selectedImages:(nullable NSArray <UIImage *> *)selectedImages;
You can use selectedImages
if you want to show different images when a specific segment selected.
- (nullable NSString *)titleAtIndex:(NSUInteger)index;
- (nullable UIImage *)imageAtIndex:(NSUInteger)index;
You can get the corresponding text or image based on a specific index.
- (void)showBadgeAtIndexes:(NSArray <NSNumber *> *)indexes;
- (void)hideBadgeAtIndex:(NSUInteger)index;
example:
[segmentedControl show BadgeAtIndexes:@[@1, @2]];
- (void)setTextAttributes:(nullable NSDictionary *)attributes
forState:(YUSegmentedControlState)state;
The attributes for text. For a list of attributes that you can include in this dictionary, see Character Attributes. The value of state
could be YUSegmentedControlNormal
and YUSegmentedControlSelected
.
example:
[segmentedControl setTextAttributes:@{
NSFontAttributeName: [UIFont systemFontOfSize:20.0 weight:UIFontWeightLight],
NSForegroundColorAttributeName: [UIColor blackColor]
} forState:YUSegmentedControlNormal];
Properties
- numberOfSegments(NSUInteger, readonly)
return the number of segments in a segmented control.
-
selectedSegmentIndex(NSUInteger, readonly)
The index number identifying the selected segment. Default is 0.
-
horizontalPadding
Default is 0.
If assign it to 32.0.
-
showsTopSeparator
A Boolean value that controls whether the top separator is visible. Default is
YES
. -
showsBottomSeparator
A Boolean value that controls whether the bottom separator is visible. Default is
YES
. -
showsVerticalDivider
A Boolean value that controls whether the vertical divider is visible. Default is
NO
. -
showsIndicator
A Boolean value that controls whether the indicator is visible. Default is
YES
. -
backgroundColor(YUSegmentedControl)
The background color of segmented control. Default is white.
-
height(YUSegmentedControlIndicator)
The height of indicator. Default is 3.0. You should use this property like this:
segment.indicator.height = 3.0;
-
locate(YUSegmentedControlIndicator)
The vertical alignment of indicator. Default is
YUSegmentedControlIndicatorLocateBottom
. Also could beYUSegmentedControlIndicatorLocateTop
You should use this property like this:segment.indicator.locate = YUSegmentedControlIndicatorLocateTop;
-
backgroundColor(YUSegmentedControlIndicator)
The background color of indicator. Default is dark gray. You should use this property like this:
segment.indicator.backgroundColor = [UIColor whiteColor];
License
This code is distributed under the terms and conditions of the MIT license.