This example for you to share a simple package of iOS dropdown menu code for your reference, the specific content as follows
//OrderListDownMenu.h
#import <UIKit/UIKit.h>
@protocol orderlistdownmenudelegate < Nsobject>
-(void) Orderlistdownmenu: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath;
@end
typedef void (^dismiss) (void);
@interface Orderlistdownmenu:uiview<uitableviewdatasource, uitableviewdelegate>
@property (nonatomic, Strong) UITableView *tableview;
@property (nonatomic, assign) id<orderlistdownmenudelegate> delegate;
@property (nonatomic, strong) Nsarray *arrdata;
@property (nonatomic, strong) Nsarray *arrimgname;
@property (nonatomic, copy) Dismiss Dismiss;
-(Instancetype) Initwithdataarr: (Nsarray *) Dataarr Origin: (Cgpoint) Origin width: (cgfloat) Width rowHeight: (cgfloat) RowHeight;
-(void) Dismisswithcompletion: (void (^) (Orderlistdownmenu *object)) completion;
@end
#import "OrderListDownMenu.h" #define Toptoview 63.0f #define Righttoview kscreenwidth-15.0f #define Lefttoview KS creenwidth-145.0-10.0f #define Celllineedgeinsets uiedgeinsetsmake (0, -80, 0, 0) #define Kscreenwidth [UIScreen m Ainscreen].bounds.size.width #define Kscreenheight [UIScreen mainscreen].bounds.size.height @interface OrderListDow
Nmenu () @property (nonatomic, assign) Cgpoint origin;
@property (nonatomic, assign) CGFloat rowHeight; @end @implementation Orderlistdownmenu-(Instancetype) Initwithdataarr: (Nsarray *) Dataarr Origin: (Cgpoint) Origin wid Th: (cgfloat) Width rowHeight: (cgfloat) rowHeight {self = [super Initwithframe:cgrectmake (0, 0, Kscreenwidth, Kscreen
Height)];
if (self) {if (rowHeight <= 0) {rowHeight = 50;
//Set Background color Self.backgroundcolor = [Uicolor colorwithred:0 green:0 blue:0 alpha:0.2];
Self.origin = origin;
Self.rowheight = RowHeight; Self.arrdata = [Dataarr copy];
Self.tableview = [[UITableView alloc] Initwithframe:cgrectmake (origin.x + lefttoview, ORIGIN.Y + TopToView, width, r
Owheight * dataarr.count) Style:uitableviewstyleplain];
_tableview.datasource = self;
_tableview.delegate = self;
[Self addsubview:_tableview];
_tableview.backgroundcolor = [Uicolor Whitecolor];
_tableview.layer.cornerradius = 2;
_tableview.bounces = NO;
_tableview.layer.cornerradius = 8;
_tableview.separatorcolor = [Uicolor colorwithwhite:0.3 alpha:1];
_tableview.separatorstyle = Uitableviewcellselectionstylenone;
[_tableview Registerclass:[uitableviewcell class] forcellreuseidentifier:@ "cell"]; if ([Self.tableview respondstoselector: @selector (setseparatorinset:)]) {[Self.tableview setseparatorinset:cellline
Edgeinsets]; } if ([Self.tableview respondstoselector: @selector (setlayoutmargins:)]) {[Self.tableview Setlayoutmargi
Ns:celllineedgeinsets]; } RETUrn self;
}-(void) layoutsubviews {[Super layoutsubviews]; }-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section {return Self.arrData.cou
nt }-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) Indexpath {return self.rowHe
ight; }-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) IndexPath {UITab
Leviewcell *cell = [TableView dequeuereusablecellwithidentifier:@ "Cell"];
Cell.textLabel.textColor = theme_color_gray_1;
Cell.textLabel.font = [Uifont systemfontofsize:15];
Cell.textLabel.text = Self.arrdata[indexpath.row]; if (Self.arrImgName.count > Indexpath.row) {cell.imageView.image = [uiimage imagenamed:self.arrimgname[indexpath.
Row]];
Cell.imageView.contentMode = Uiviewcontentmodescaleaspectfit;
} Uilabel *label = [[Uilabel alloc] init]; Label.frame = CGRectMake (0, _tableview.frame.size.width,0.5);
Label.backgroundcolor = Theme_separator_color;
[Cell.contentview Addsubview:label];
return cell; }-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath {if ([Self.delegat E respondstoselector: @selector (Orderlistdownmenu:didselectrowatindexpath:)]) {[Self.delegate Orderlistdownmenu:
TableView Didselectrowatindexpath:indexpath];
} [TableView Deselectrowatindexpath:indexpath animated:yes];
[Self dismisswithcompletion:nil]; }-(void) Dismisswithcompletion: (void (^) (Orderlistdownmenu *object)) Completion {__weak __typeof (self) weakself
= self;
[UIView animatewithduration:0.2 animations:^{weakself.alpha = 0;
WeakSelf.tableView.frame = CGRectMake (weakself.origin.x + lefttoview + 145, WEAKSELF.ORIGIN.Y + toptoview, 0, 0);
} completion:^ (BOOL finished) {[weakself Removefromsuperview];
if (completion) {completion (weakself);
} if (Weakself.dismiss) { Weakself.dismiss ();
}
}]; }-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *) Event {Uitouch *touch = [touches
Anyobject]; if (![
Touch.view IsEqual:self.tableView]) {[Self dismisswithcompletion:nil];
}-(void) DrawRect: (cgrect) rect {//[colors[serie] setfill];
Get the current view ready artboard cgcontextref context = Uigraphicsgetcurrentcontext (); Use path to draw the Triangle Cgcontextbeginpath (context);//Tag Cgcontextmovetopoint (context, rightToView-1
3, 53);/Set starting point Cgcontextaddlinetopoint (context, rightToView-21, Toptoview);
Cgcontextaddlinetopoint (context, rightToView-4, Toptoview); Cgcontextclosepath (context);//path end flag, do not write default closed [Self.tableView.backgroundColor Setfill]; Set the fill color [Self.tableView.backgroundColor setstroke]; Sets the border color Cgcontextdrawpath (context, kcgpathfillstroke);//Draw Paths path} @end
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.