IOS development-custom segment Control for customizing styles and iossegment
The segment control of the system is too closed. It is too difficult to add a background in a different color. You can't help writing a segment control by yourself.
This control provides a lot of settings for custom attributes, which is more convenient to use. Note that if itemWidth is not set, the width of each item is evenly allocated according to the width of the control, if this parameter is set, the sliding effect is displayed when the total width exceeds the control width.
Go directly to the Code:
Header file:
# Import <Foundation/Foundation. h> @ protocol upload-(void) wcSegmentControlSelectionChanged :( id) sender; @ end @ interface WCSegmentControl: UIView @ property (nonatomic, strong) id <strong> delegate; @ property (nonatomic, strong) NSMutableArray * performanceoftitle; @ property (nonatomic, assign, setter = setCurrentSelectedIndex :) int currentSelectedIndex; // title color @ property (nonatomic, strong) UIColor * titleColor; @ property (nonatomic, strong) UIColor * selectedTitleColor; // font @ property (nonatomic, strong) UIFont * titleFont; // item selectedBackground Color; @ property (nonatomic, strong) UIColor * itemBackgroundColor; @ property (nonatomic, strong) UIColor * selectedItemBackgroundColor; // item selectedBackground image; @ property (nonatomic, strong) UIImage * itemBackgroundImage; @ property (nonatomic, strong) UIImage * handle; // item border @ property (nonatomic, strong) UIColor * itemBorderColor; @ property (nonatomic, assign) BOOL isShowItemBorderWhenHilight; @ property (nonatomic, assign) int itemBorderWidth; @ property (nonatomic, assign) int itemCornerRadius; // item. If this parameter is not set, the control width @ property (nonatomic, assign) int itemWidth is evenly allocated. // control border @ property (nonatomic, strong) UIColor * borderColor; @ property (nonatomic, assign) BOOL isShowBorder; @ property (nonatomic, assign) int borderWidth; @ property (nonatomic, assign) int cornerRadius; // split line @ property (nonatomic, strong) UIColor * splitColor; @ property (nonatomic, assign) int splitBorderWidth; @ property (nonatomic, assign) BOOL isShowSplitBorder; @ end
Implementation file:
# Import "WCSegmentControl. h "# import" WCSegmentControlItemButton. h "@ implementation WCSegmentControl {UIScrollView * _ scrollView;}-(id) initWithFrame :( CGRect) frame {self = [super initWithFrame: frame]; if (self) {self. clipsToBounds = YES; _ scrollView = [[UIScrollView alloc] initWithFrame: self. bounds]; _ scrollView. backgroundColor = self. backgroundColor; [self addSubview: _ scrollView]; _ performanceoftit Le = [NSMutableArray array]; _ selectedTitleColor = [UIColor whiteColor]; _ titleColor = [UIColor blackColor]; _ itemBackgroundColor = [UIColor whiteColor]; _ selectedItemBackgroundColor = kWCColor7; _ borderWidth = 1; _ borderColor = kWCColor7; _ cornerRadius = 5; _ itemBorderColor = kWCColor7; _ itemBorderWidth = 1; _ itemCornerRadius = 0; _ titleFont = [UIFont systemFontOfSize: 12] _ splitColor = kWCColor7; _ SplitBorderWidth = 1; _ isShowBorder = YES; _ isShowItemBorderWhenHilight = NO; _ isShowSplitBorder = YES;} return self;}-(void) setperformanceoftitle :( NSMutableArray *) performanceoftitle {_ performanceoftitle = performanceoftitle; [self reloadData];}-(WCSegmentControlItemButton *) createItemControlWithTitle :( NSString *) title {WCSegmentControlItemButton * btn = [externalloc] T]; if (_ itemBackgroundImage) {[btn failed: _ itemBackgroundImage forState: UIControlStateNormal];} if (_ blank) {[btn failed: _ blank forState: UIControlStateSelected];} [btn setBackgroundColor: _ itemBackgroundColor]; [btn setTitleColor: _ selectedTitleColor forState: UIControlStateSelected]; [btn setTitleColor: _ titleColor forState: UICont RolStateNormal]; btn. titleLabel. font = _ titleFont; [btn setTitle: title forState: UIControlStateNormal]; btn. layer. cornerRadius = _ itemCornerRadius; return btn;}-(void) refreshUI {if (_ isShowBorder) {self. layer. borderWidth = _ borderWidth; self. layer. borderColor = _ borderColor. CGColor; self. layer. cornerRadius = _ cornerRadius;} else {self. layer. borderWidth = 0 ;}}-(void) reloadData {[_ scrollView. Subviews makeObjectsPerformSelector: @ selector (removeFromSuperview)]; if ([_ performanceoftitle count]> 0) {// UIEdgeInsets CGRect fra = CGRectMake (0, 0, _ itemWidth> 0? _ ItemWidth: self. width/[_ performanceoftitle count], self. height); CGFloat leftMargin = MAX (0, (self. width-fra. size. width * [self. performanceoftitle count])/2); _ block CGFloat contentWidth = leftMargin; [self. performanceoftitle enumerateObjectsUsingBlock: ^ (NSString * title, NSUInteger idx, BOOL * stop) {WCSegmentControlItemButton * btn = [self createItemControlWithTitle: title]; btn. frame = fra; btn. Left = leftMargin + idx * btn. width; [btn addTarget: self action: @ selector (btnTapped :) forControlEvents: UIControlEventTouchUpInside]; btn. index = idx; [_ scrollView addSubview: btn]; if (_ isShowSplitBorder & idx! = 0) {UIView * line = [[UIView alloc] initWithFrame: CGRectMake (0, 0, _ splitBorderWidth, btn. height)]; line. backgroundColor = _ splitColor; line. left = btn. left; [_ scrollView addSubview: line];} contentWidth = btn. right;}]; _ scrollView. contentSize = CGSizeMake (contentWidth, _ scrollView. height); [self setCurrentSelectedIndex: 0]; [self refreshUI] ;}}-(void) btnTapped :( id) sender {WCSegmentControlItemButton * btn = sender; if (self. currentSelectedIndex = btn. index) {return;} [self setCurrentSelectedIndex: btn. index]; // It cannot be triggered in setCurrentSelectedIndex. Otherwise, if ([(NSObject *) (self. delegate) respondsToSelector: @ selector (wcSegmentControlSelectionChanged :)]) {[self. delegate wcSegmentControlSelectionChanged: self] ;}}-(void) setCurrentSelectedIndex :( int) currentSelectedIndex {_ currentSelectedIndex = currentSelectedIndex; [_ scrollView. subviews controls: ^ (id obj, NSUInteger idx, BOOL * stop) {if ([obj isKindOfClass: [WCSegmentControlItemButton class]) {WCSegmentControlItemButton * view = obj; if (view. index = currentSelectedIndex) {[view setSelected: YES]; [view setBackgroundColor: _ selectedItemBackgroundColor]; // if you are outside the screen, move it to the screen if (view. right-_ scrollView. width> 0) {_ scrollView. contentOffset = CGPointMake (view. right-_ scrollView. width, 0);} else if (view. left-_ scrollView. contentOffset. x <0) {_ scrollView. contentOffset = CGPointMake (view. left, 0);} if (_ isShowItemBorderWhenHilight) {view. layer. borderWidth = _ borderWidth; view. layer. borderColor = _ borderColor. CGColor; view. layer. cornerRadius = _ cornerRadius ;}} else {[view setSelected: NO]; [view setBackgroundColor: _ itemBackgroundColor]; view. layer. borderWidth = 0 ;}}] ;}@ end