Now a lot of apps, especially shopping, have filtered sort, like ascending descending kind, recent projects have done a similar share to everyone.
1. Prepare two photos
2. Defining the UIButton Subclass
#import <UIKit/UIKit.h>typedef void(^dropbtnclickblock) (BOOL isseelect) ; @interface *title; @property (Nonatomic,strong) uiimageview *upordownimg; @property (nonatomic,copy) Dropbtnclickblock Btnclickblock; @end
#import "DropBtn.h"#defineRGB (r,g,b) [Uicolor colorwithred:r/255.0 green:g/255.0 blue:b/255.0 alpha:1.0]@implementationdropbtn-(Instancetype) initWithFrame: (cgrect) frame{ Self=[Super Initwithframe:frame]; if(self) {_title=[[NSString Alloc]init]; _upordownimg=[[Uiimageview alloc]init]; _upordownimg.image=[uiimage imagenamed:@"Yzp_data_dropdown.png"]; _upordownimg.frame=cgrectmake (self.frame.size.width- --8, (frame.size.height-5)/2,8,5); [Self addsubview:_upordownimg]; [Self addtarget:self Action: @selector (Btnclickhandle:) forcontrolevents:uicontroleventtouchupinside]; } returnSelf ;}-(void) DrawRect: (CGRect) rect{[Super Drawrect:rect]; //frame that calculates the stringNsdictionary *[email protected]{nsfontattributename: [Uifont systemfontofsize: -. F],NSFOREGROUNDCOLORATTRIBUTENAME:RGB ( -, -, -)}; CGRect Frame=[_title boundingrectwithsize:cgsizemake (Maxfloat, maxfloat) options:nsstringdrawinguseslinefragmentorigin Attributes:attributes Context:nil]; [_title drawatpoint:cgpointmake (Self.bounds.size.width-8-frame.size.width- -)/2, (Self.bounds.size.height-frame.size.height)/2) withattributes:attributes];}-(void) Btnclickhandle: (ID) sender{UIButton*btn= (UIButton *) sender; Btn.selected=!btn.selected; _btnclickblock (btn.selected);}-(void) setselected: (BOOL) selected{[Super setselected:selected]; if(!selected) {_upordownimg.image=[uiimage imagenamed:@"Yzp_data_dropdown.png"]; } Else{_upordownimg.image=[uiimage imagenamed:@"Yzp_data_dropback.png"]; } }@end
3. Call
DROPBTN *dropbtn=[[dropbtn Alloc]initwithframe:cgrectmake (0, self.view.bounds.size.width/ 2)]; Dropbtn.title=@ " price "; Dropbtn.backgroundcolor=[Uicolor yellowcolor]; Dropbtn.btnclickblock=^(BOOL isSelected) { }; [Self.view addsubview:dropbtn];
4. Effects
Custom Sort button