iOS開發(fā)項目篇—14點擊標(biāo)題按鈕彈出菜單 一、簡單說明 (1)簡單實現(xiàn) 點擊標(biāo)題按鈕彈框,,在箭頭向上的時候,,顯示標(biāo)題菜單 把ImageView添加到誰的身上?三角形的箭頭在導(dǎo)航欄上,,因此不能添加到tableview上,。 分析圖示: 有兩個兩種方法可以考慮: (1)添加到導(dǎo)航控制器上,因為導(dǎo)航欄是在導(dǎo)航控制器上的。 (2)不用考慮控制器,,直接添加到窗口上,。 拿到窗口 (1)self.view.window這個屬性謹(jǐn)慎使用,當(dāng)開始加載的時候,,為空 (2)獲取主窗口 [UIApplication sharedApplication].keyWindow 實現(xiàn)代碼 1-(void)titleButtonClick:(UIButton *)titleButton 2{ 3UIImage *titleImage=[UIImage imageWithName:@"navigationbar_arrow_down"]; 4 5if(titleButton.currentImage==titleImage) { 6//換成箭頭向上 7[titleButton setImage:[UIImage imageWithName:@"navigationbar_arrow_up"] forState:UIControlStateNormal]; 8//顯示菜單 9UIImageView *titleMenu=[[UIImageView alloc]init]; 10titleMenu.size=CGSizeMake(150, 250); 11titleMenu.image=[UIImage resizedImage:@"popover_background"]; 12//獲取主窗口 13UIView *window=[UIApplication sharedApplication].keyWindow; 14[window addSubview:titleMenu]; 15}else 16{ 17//換成箭頭向下 18[titleButton setImage:[UIImage imageWithName:@"navigationbar_arrow_down"] forState:UIControlStateNormal]; 19} 20} 實現(xiàn)效果: (2)調(diào)整窗口位置 代碼: 1-(void)titleButtonClick:(UIButton *)titleButton 2{ 3UIImage *titleImage=[UIImage imageWithName:@"navigationbar_arrow_down"]; 4 5if(titleButton.currentImage==titleImage) { 6//換成箭頭向上 7[titleButton setImage:[UIImage imageWithName:@"navigationbar_arrow_up"] forState:UIControlStateNormal]; 8//顯示菜單 9UIImageView *titleMenu=[[UIImageView alloc]init]; 10titleMenu.size=CGSizeMake(150, 250); 11titleMenu.image=[UIImage resizedImage:@"popover_background"]; 12//獲取主窗口 13UIView *window=[UIApplication sharedApplication].keyWindow; 14titleMenu.centerX=window.width*0.5; 15titleMenu.y=55; 16[window addSubview:titleMenu]; 17}else 18{ 19//換成箭頭向下 20[titleButton setImage:[UIImage imageWithName:@"navigationbar_arrow_down"] forState:UIControlStateNormal]; 21} 22} 效果: (3)點擊屏幕的任何區(qū)域,,它都消失,,除非是點擊它自己,。 思路:在窗口上蓋一個一樣大的button設(shè)置為透明,然后監(jiān)聽它的點擊。 提示:屏蔽所有東西,,可以考慮使用一個全屏的透明按鈕,。 注意添加的層級關(guān)系: 代碼實現(xiàn): 1-(void)titleButtonClick:(UIButton *)titleButton 2{ 3UIImage *titleImage=[UIImage imageWithName:@"navigationbar_arrow_down"]; 4 5if(titleButton.currentImage==titleImage) { 6//換成箭頭向上 7[titleButton setImage:[UIImage imageWithName:@"navigationbar_arrow_up"] forState:UIControlStateNormal]; 8 9//獲取主窗口 10UIView *window=[UIApplication sharedApplication].keyWindow; 11 12//添加一個全局的遮蓋按鈕 13UIButton *cover=[[UIButton alloc]init]; 14cover.frame=window.bounds; 15cover.backgroundColor=[UIColor redColor]; 16cover.alpha=0.2; 17[window addSubview:cover]; 18 19//顯示菜單 20UIImageView *titleMenu=[[UIImageView alloc]init]; 21titleMenu.size=CGSizeMake(150, 250); 22titleMenu.image=[UIImage resizedImage:@"popover_background"]; 23titleMenu.centerX=window.width*0.5; 24titleMenu.y=55; 25[window addSubview:titleMenu]; 26}else 27{ 28//換成箭頭向下 29[titleButton setImage:[UIImage imageWithName:@"navigationbar_arrow_down"] forState:UIControlStateNormal]; 30} 31} 效果: 說明:設(shè)置背景顏色為clearcolor,清掉顏色 提示:需要設(shè)置菜單的imageview變?yōu)榭山换サ?/div> 1-(void)titleButtonClick:(UIButton *)titleButton 2{ 3UIImage *titleImage=[UIImage imageWithName:@"navigationbar_arrow_down"]; 4 5if(titleButton.currentImage==titleImage) { 6//換成箭頭向上 7[titleButton setImage:[UIImage imageWithName:@"navigationbar_arrow_up"] forState:UIControlStateNormal]; 8 9//獲取主窗口 10UIView *window=[UIApplication sharedApplication].keyWindow; 11 12//添加一個全局的遮蓋按鈕 13UIButton *cover=[[UIButton alloc]init]; 14cover.frame=window.bounds; 15//cover.backgroundColor=[UIColor redColor]; 16//清除顏色 17cover.backgroundColor=[UIColor clearColor]; 18cover.alpha=0.2; 19[window addSubview:cover]; 20 21//顯示菜單 22UIImageView *titleMenu=[[UIImageView alloc]init]; 23//設(shè)置為可交互的 24titleMenu.userInteractionEnabled=YES; 25titleMenu.size=CGSizeMake(150, 250); 26titleMenu.image=[UIImage resizedImage:@"popover_background"]; 27titleMenu.centerX=window.width*0.5; 28titleMenu.y=55; 29[window addSubview:titleMenu]; 30}else 31{ 32//換成箭頭向下 33[titleButton setImage:[UIImage imageWithName:@"navigationbar_arrow_down"] forState:UIControlStateNormal]; 34} 35} 二,、封裝菜單欄 封裝:把全屏按鈕和菜單控件封裝在一起,,專門用來封裝彈出菜單 在封裝的時候先考慮需要提供什么接口給外界,思考外面的接口夠不夠用 新建一個類,用來封裝彈出菜單的操作: 自定義類的頭文件: 1// 2//YYPopMenu.h 3 4#import<UIKit/UIKit.h> 5@classYYPopMenu; 6 7@protocolYYPopMenuDelegate <NSObject> 8 9@optional 10-(void)popMenuDidDismissed:(YYPopMenu *)popMenu; 11@end 12 13 14@interfaceYYPopMenu : UIView 15/** 16* 初始化方法 17*/ 18-(instancetype)initWithContentView:(UIView *)contentView; 19+(instancetype)popMenuWithContentView:(UIView *)contentView; 20 21/** 22* 設(shè)置菜單的背景圖片 23*/ 24-(void)setBackground:(UIImage *)background; 25/** 26* 顯示菜單 27*/ 28-(void)showInRect:(CGRect)rect; 29 30/** 31* 關(guān)閉菜單 32*/ 33-(void)dismiss; 34 35@property(nonatomic,strong)id<YYPopMenuDelegate> delegate; 36@end 實現(xiàn): 1// 2//YYPopMenu.m 3//08-微博彈出菜單 4// 5 6#import"YYPopMenu.h" 7 8@interfaceYYPopMenu() 9@property(nonatomic,strong)UIView *contentView; 10/** 11* 最底部的遮蓋 :屏蔽除菜單以外控件的事件 12*/ 13@property(nonatomic,strong)UIImageView *container; 14/** 15* 最底部的遮蓋 :屏蔽除菜單以外控件的事件 16*/ 17@property(nonatomic,strong)UIButton *cover; 18@end 19@implementationYYPopMenu 20 21#pragmamark-初始化方法 22//init方法會調(diào)用該方法 23- (id)initWithFrame:(CGRect)frame 24{ 25self =[super initWithFrame:frame]; 26if(self) { 27/**添加菜單內(nèi)部的兩個子控件*/ 28//1.添加一個遮蓋按鈕 29UIButton *cover=[[UIButton alloc]init]; 30//清除顏色 31cover.backgroundColor=[UIColor clearColor]; 32[cover addTarget:self action:@selector(coverClick) forControlEvents:UIControlEventTouchUpInside]; 33[self addSubview:cover]; 34self.cover=cover; 35 36//2.添加單箭頭的菜單圖片 37UIImageView *container=[[UIImageView alloc]init]; 38//設(shè)置為可交互的 39container.userInteractionEnabled=YES; 40container.size=CGSizeMake(200, 100); 41container.image=[UIImage resizedImage:@"popover_background"]; 42[self addSubview:container]; 43self.container=container; 44} 45returnself; 46} 47 48-(instancetype)initWithContentView:(UIView *)contentView 49{ 50if(self=[super init]) { 51self.contentView=contentView; 52} 53returnself; 54} 55 56+(instancetype)popMenuWithContentView:(UIView *)contentView 57{ 58return[[self alloc]initWithContentView:contentView]; 59} 60 61-(void)layoutSubviews 62{ 63[super layoutSubviews]; 64self.cover.frame=self.bounds; 65} 66#pragmamark-內(nèi)部方法 67-(void)coverClick 68{ 69[self dismiss]; 70} 71 72#pragmamark-公共方法 73-(void)setBackground:(UIImage *)background 74{ 75self.container.image=background; 76} 77 78-(void)showInRect:(CGRect)rect 79{ 80//添加菜單到整體的窗口上 81UIWindow *window=[UIApplication sharedApplication].keyWindow; 82self.frame=window.bounds; 83[window addSubview:self]; 84 85//設(shè)置容器的frame 86self.container.frame=rect; 87[self.container addSubview:self.contentView]; 88 89//設(shè)置容器里面內(nèi)容的frame 90CGFloat topMargin = 12; 91CGFloat leftMargin = 5; 92CGFloat rightMargin = 5; 93CGFloat bottomMargin = 8; 94 95self.contentView.y =topMargin; 96self.contentView.x =leftMargin; 97self.contentView.width = self.container.width - leftMargin -rightMargin; 98self.contentView.height = self.container.height - topMargin -bottomMargin; 99 100} 101 102-(void)dismiss 103{ 104//一旦調(diào)用了該方法,,就通知代理刪除菜單欄 105if([self.delegaterespondsToSelector:@selector(popMenuDidDismissed:)]) { 106[self.delegatepopMenuDidDismissed:self]; 107} 108[self removeFromSuperview]; 109} 110@end 使用: 1// 2//YYHomeTableViewController.m 3// 4 5#import"YYHomeTableViewController.h" 6#import"YYOneViewController.h" 7#import"YYTitleButton.h" 8#import"YYPopMenu.h" 9 10@interfaceYYHomeTableViewController ()<YYPopMenuDelegate> 11@property(nonatomic,assign)BOOL down; 12@end 13 14@implementationYYHomeTableViewController 15 16- (id)initWithStyle:(UITableViewStyle)style 17{ 18self =[super initWithStyle:style]; 19if(self) { 20//Custom initialization 21} 22returnself; 23} 24 25- (void)viewDidLoad 26{ 27[super viewDidLoad]; 28 29//設(shè)置導(dǎo)航欄的按鈕 30self.navigationItem.leftBarButtonItem=[UIBarButtonItem itemWithImageName:@"navigationbar_friendsearch"highImageName:@"navigationbar_friendsearch_highlighted"target:self action:@selector(friendsearch)]; 31self.navigationItem.rightBarButtonItem=[UIBarButtonItem itemWithImageName:@"navigationbar_pop"highImageName:@"navigationbar_pop_highlighted"target:self action:@selector(pop)]; 32 33//設(shè)置導(dǎo)航欄按鈕 34YYTitleButton *titleButton=[[YYTitleButton alloc]init]; 35//設(shè)置文字 36[titleButton setTitle:@"首頁"forState:UIControlStateNormal]; 37//設(shè)置圖標(biāo) 38[titleButton setImage:[UIImage imageWithName:@"navigationbar_arrow_down"] forState:UIControlStateNormal]; 39//設(shè)置背景 40[titleButton setBackgroundImage:[UIImage resizedImage:@"navigationbar_filter_background_highlighted"] forState:UIControlStateHighlighted]; 41 42//設(shè)置尺寸 43titleButton.width=100; 44titleButton.height=35; 45//監(jiān)聽按鈕的點擊事件 46[titleButton addTarget:self action:@selector(titleButtonClick:) forControlEvents:UIControlEventTouchUpInside]; 47self.navigationItem.titleView=titleButton; 48} 49 50-(void)titleButtonClick:(UIButton *)titleButton 51{ 52//UIImage *titleImage=[UIImage imageWithName:@"navigationbar_arrow_down"]; 53// 54//if (titleButton.currentImage==titleImage) { 55//換成箭頭向上 56[titleButton setImage:[UIImage imageWithName:@"navigationbar_arrow_up"] forState:UIControlStateNormal]; 57 58UITableView *tableView=[[UITableView alloc]init]; 59[tableView setBackgroundColor:[UIColor yellowColor]]; 60YYPopMenu *menu=[YYPopMenu popMenuWithContentView:tableView]; 61[menu showInRect:CGRectMake(60, 55, 200, 200)]; 62menu.delegate=self; 63 64//}else 65//{ 66////換成箭頭向下 67//[titleButton setImage:[UIImage imageWithName:@"navigationbar_arrow_down"] forState:UIControlStateNormal]; 68//} 69} 70 71 72#pragmamark-YYPopMenuDelegate 73//彈出菜單 74-(void)popMenuDidDismissed:(YYPopMenu *)popMenu 75{ 76YYTitleButton *titleButton=(YYTitleButton *)self.navigationItem.titleView; 77[titleButton setImage:[UIImage imageWithName:@"navigationbar_arrow_down"] forState:UIControlStateNormal]; 78} 79-(void)pop 80{ 81YYLog(@"---POP---"); 82} 83-(void)friendsearch 84{ 85//跳轉(zhuǎn)到one這個子控制器界面 86YYOneViewController *one=[[YYOneViewController alloc]init]; 87one.title=@"One"; 88//拿到當(dāng)前控制器 89[self.navigationController pushViewController:one animated:YES]; 90 91} 92 93#pragmamark - Table view data source 94- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 95{ 96return20; 97} 98 99- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 100{ 101staticNSString *ID = @"cell"; 102UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:ID]; 103if(!cell) { 104cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID]; 105} 106cell.textLabel.text = [NSString stringWithFormat:@"%d----首頁測試數(shù)據(jù)", indexPath.row]; 107returncell; 108} 109 110- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 111{ 112//點擊cell的時候,,跳到下一個界面 113UIViewController *newVc =[[UIViewController alloc] init]; 114newVc.view.backgroundColor =[UIColor redColor]; 115newVc.title = @"新控制器"; 116[self.navigationController pushViewController:newVc animated:YES]; 117} 118 119@end 實現(xiàn): iOS開發(fā)項目篇—14點擊標(biāo)題按鈕彈出菜單 一、簡單說明 (1)簡單實現(xiàn) 點擊標(biāo)題按鈕彈框,,在箭頭向上的時候,,顯示標(biāo)題菜單 把ImageView添加到誰的身上?三角形的箭頭在導(dǎo)航欄上,,因此不能添加到tableview上,。 分析圖示: 有兩個兩種方法可以考慮: (1)添加到導(dǎo)航控制器上,因為導(dǎo)航欄是在導(dǎo)航控制器上的,。 (2)不用考慮控制器,,直接添加到窗口上。 拿到窗口 (1)self.view.window這個屬性謹(jǐn)慎使用,,當(dāng)開始加載的時候,,為空 (2)獲取主窗口 [UIApplication sharedApplication].keyWindow 實現(xiàn)代碼 1-(void)titleButtonClick:(UIButton *)titleButton 2{ 3UIImage *titleImage=[UIImage imageWithName:@"navigationbar_arrow_down"]; 4 5if(titleButton.currentImage==titleImage) { 6//換成箭頭向上 7[titleButton setImage:[UIImage imageWithName:@"navigationbar_arrow_up"] forState:UIControlStateNormal]; 8//顯示菜單 9UIImageView *titleMenu=[[UIImageView alloc]init]; 10titleMenu.size=CGSizeMake(150, 250); 11titleMenu.image=[UIImage resizedImage:@"popover_background"]; 12//獲取主窗口 13UIView *window=[UIApplication sharedApplication].keyWindow; 14[window addSubview:titleMenu]; 15}else 16{ 17//換成箭頭向下 18[titleButton setImage:[UIImage imageWithName:@"navigationbar_arrow_down"] forState:UIControlStateNormal]; 19} 20} 實現(xiàn)效果: (2)調(diào)整窗口位置 代碼: 1-(void)titleButtonClick:(UIButton *)titleButton 2{ 3UIImage *titleImage=[UIImage imageWithName:@"navigationbar_arrow_down"]; 4 5if(titleButton.currentImage==titleImage) { 6//換成箭頭向上 7[titleButton setImage:[UIImage imageWithName:@"navigationbar_arrow_up"] forState:UIControlStateNormal]; 8//顯示菜單 9UIImageView *titleMenu=[[UIImageView alloc]init]; 10titleMenu.size=CGSizeMake(150, 250); 11titleMenu.image=[UIImage resizedImage:@"popover_background"]; 12//獲取主窗口 13UIView *window=[UIApplication sharedApplication].keyWindow; 14titleMenu.centerX=window.width*0.5; 15titleMenu.y=55; 16[window addSubview:titleMenu]; 17}else 18{ 19//換成箭頭向下 20[titleButton setImage:[UIImage imageWithName:@"navigationbar_arrow_down"] forState:UIControlStateNormal]; 21} 22} 效果: (3)點擊屏幕的任何區(qū)域,它都消失,,除非是點擊它自己,。 思路:在窗口上蓋一個一樣大的button設(shè)置為透明,然后監(jiān)聽它的點擊。 提示:屏蔽所有東西,,可以考慮使用一個全屏的透明按鈕,。 注意添加的層級關(guān)系: 代碼實現(xiàn): 1-(void)titleButtonClick:(UIButton *)titleButton 2{ 3UIImage *titleImage=[UIImage imageWithName:@"navigationbar_arrow_down"]; 4 5if(titleButton.currentImage==titleImage) { 6//換成箭頭向上 7[titleButton setImage:[UIImage imageWithName:@"navigationbar_arrow_up"] forState:UIControlStateNormal]; 8 9//獲取主窗口 10UIView *window=[UIApplication sharedApplication].keyWindow; 11 12//添加一個全局的遮蓋按鈕 13UIButton *cover=[[UIButton alloc]init]; 14cover.frame=window.bounds; 15cover.backgroundColor=[UIColor redColor]; 16cover.alpha=0.2; 17[window addSubview:cover]; 18 19//顯示菜單 20UIImageView *titleMenu=[[UIImageView alloc]init]; 21titleMenu.size=CGSizeMake(150, 250); 22titleMenu.image=[UIImage resizedImage:@"popover_background"]; 23titleMenu.centerX=window.width*0.5; 24titleMenu.y=55; 25[window addSubview:titleMenu]; 26}else 27{ 28//換成箭頭向下 29[titleButton setImage:[UIImage imageWithName:@"navigationbar_arrow_down"] forState:UIControlStateNormal]; 30} 31} 效果: 說明:設(shè)置背景顏色為clearcolor,清掉顏色 提示:需要設(shè)置菜單的imageview變?yōu)榭山换サ?/div>
1-(void)titleButtonClick:(UIButton *)titleButton 2{ 3UIImage *titleImage=[UIImage imageWithName:@"navigationbar_arrow_down"]; 4 5if(titleButton.currentImage==titleImage) { 6//換成箭頭向上 7[titleButton setImage:[UIImage imageWithName:@"navigationbar_arrow_up"] forState:UIControlStateNormal]; 8 9//獲取主窗口 10UIView *window=[UIApplication sharedApplication].keyWindow; 11 12//添加一個全局的遮蓋按鈕 13UIButton *cover=[[UIButton alloc]init]; 14cover.frame=window.bounds; 15//cover.backgroundColor=[UIColor redColor]; 16//清除顏色 17cover.backgroundColor=[UIColor clearColor]; 18cover.alpha=0.2; 19[window addSubview:cover]; 20 21//顯示菜單 22UIImageView *titleMenu=[[UIImageView alloc]init]; 23//設(shè)置為可交互的 24titleMenu.userInteractionEnabled=YES; 25titleMenu.size=CGSizeMake(150, 250); 26titleMenu.image=[UIImage resizedImage:@"popover_background"]; 27titleMenu.centerX=window.width*0.5; 28titleMenu.y=55; 29[window addSubview:titleMenu]; 30}else 31{ 32//換成箭頭向下 33[titleButton setImage:[UIImage imageWithName:@"navigationbar_arrow_down"] forState:UIControlStateNormal]; 34} 35} 二,、封裝菜單欄 封裝:把全屏按鈕和菜單控件封裝在一起,,專門用來封裝彈出菜單 在封裝的時候先考慮需要提供什么接口給外界,思考外面的接口夠不夠用 新建一個類,用來封裝彈出菜單的操作: 自定義類的頭文件: 1// 2//YYPopMenu.h 3 4#import<UIKit/UIKit.h> 5@classYYPopMenu; 6 7@protocolYYPopMenuDelegate <NSObject> 8 9@optional 10-(void)popMenuDidDismissed:(YYPopMenu *)popMenu; 11@end 12 13 14@interfaceYYPopMenu : UIView 15/** 16* 初始化方法 17*/ 18-(instancetype)initWithContentView:(UIView *)contentView; 19+(instancetype)popMenuWithContentView:(UIView *)contentView; 20 21/** 22* 設(shè)置菜單的背景圖片 23*/ 24-(void)setBackground:(UIImage *)background; 25/** 26* 顯示菜單 27*/ 28-(void)showInRect:(CGRect)rect; 29 30/** 31* 關(guān)閉菜單 32*/ 33-(void)dismiss; 34 35@property(nonatomic,strong)id<YYPopMenuDelegate> delegate; 36@end 實現(xiàn): 1// 2//YYPopMenu.m 3//08-微博彈出菜單 4// 5 6#import"YYPopMenu.h" 7 8@interfaceYYPopMenu() 9@property(nonatomic,strong)UIView *contentView; 10/** 11* 最底部的遮蓋 :屏蔽除菜單以外控件的事件 12*/ 13@property(nonatomic,strong)UIImageView *container; 14/** 15* 最底部的遮蓋 :屏蔽除菜單以外控件的事件 16*/ 17@property(nonatomic,strong)UIButton *cover; 18@end 19@implementationYYPopMenu 20 21#pragmamark-初始化方法 22//init方法會調(diào)用該方法 23- (id)initWithFrame:(CGRect)frame 24{ 25self =[super initWithFrame:frame]; 26if(self) { 27/**添加菜單內(nèi)部的兩個子控件*/ 28//1.添加一個遮蓋按鈕 29UIButton *cover=[[UIButton alloc]init]; 30//清除顏色 31cover.backgroundColor=[UIColor clearColor]; 32[cover addTarget:self action:@selector(coverClick) forControlEvents:UIControlEventTouchUpInside]; 33[self addSubview:cover]; 34self.cover=cover; 35 36//2.添加單箭頭的菜單圖片 37UIImageView *container=[[UIImageView alloc]init]; 38//設(shè)置為可交互的 39container.userInteractionEnabled=YES; 40container.size=CGSizeMake(200, 100); 41container.image=[UIImage resizedImage:@"popover_background"]; 42[self addSubview:container]; 43self.container=container; 44} 45returnself; 46} 47 48-(instancetype)initWithContentView:(UIView *)contentView 49{ 50if(self=[super init]) { 51self.contentView=contentView; 52} 53returnself; 54} 55 56+(instancetype)popMenuWithContentView:(UIView *)contentView 57{ 58return[[self alloc]initWithContentView:contentView]; 59} 60 61-(void)layoutSubviews 62{ 63[super layoutSubviews]; 64self.cover.frame=self.bounds; 65} 66#pragmamark-內(nèi)部方法 67-(void)coverClick 68{ 69[self dismiss]; 70} 71 72#pragmamark-公共方法 73-(void)setBackground:(UIImage *)background 74{ 75self.container.image=background; 76} 77 78-(void)showInRect:(CGRect)rect 79{ 80//添加菜單到整體的窗口上 81UIWindow *window=[UIApplication sharedApplication].keyWindow; 82self.frame=window.bounds; 83[window addSubview:self]; 84 85//設(shè)置容器的frame 86self.container.frame=rect; 87[self.container addSubview:self.contentView]; 88 89//設(shè)置容器里面內(nèi)容的frame 90CGFloat topMargin = 12; 91CGFloat leftMargin = 5; 92CGFloat rightMargin = 5; 93CGFloat bottomMargin = 8; 94 95self.contentView.y =topMargin; 96self.contentView.x =leftMargin; 97self.contentView.width = self.container.width - leftMargin -rightMargin; 98self.contentView.height = self.container.height - topMargin -bottomMargin; 99 100} 101 102-(void)dismiss 103{ 104//一旦調(diào)用了該方法,就通知代理刪除菜單欄 105if([self.delegaterespondsToSelector:@selector(popMenuDidDismissed:)]) { 106[self.delegatepopMenuDidDismissed:self]; 107} 108[self removeFromSuperview]; 109} 110@end 使用: 1// 2//YYHomeTableViewController.m 3// 4 5#import"YYHomeTableViewController.h" 6#import"YYOneViewController.h" 7#import"YYTitleButton.h" 8#import"YYPopMenu.h" 9 10@interfaceYYHomeTableViewController ()<YYPopMenuDelegate> 11@property(nonatomic,assign)BOOL down; 12@end 13 14@implementationYYHomeTableViewController 15 16- (id)initWithStyle:(UITableViewStyle)style 17{ 18self =[super initWithStyle:style]; 19if(self) { 20//Custom initialization 21} 22returnself; 23} 24 25- (void)viewDidLoad 26{ 27[super viewDidLoad]; 28 29//設(shè)置導(dǎo)航欄的按鈕 30self.navigationItem.leftBarButtonItem=[UIBarButtonItem itemWithImageName:@"navigationbar_friendsearch"highImageName:@"navigationbar_friendsearch_highlighted"target:self action:@selector(friendsearch)]; 31self.navigationItem.rightBarButtonItem=[UIBarButtonItem itemWithImageName:@"navigationbar_pop"highImageName:@"navigationbar_pop_highlighted"target:self action:@selector(pop)]; 32 33//設(shè)置導(dǎo)航欄按鈕 34YYTitleButton *titleButton=[[YYTitleButton alloc]init]; 35//設(shè)置文字 36[titleButton setTitle:@"首頁"forState:UIControlStateNormal]; 37//設(shè)置圖標(biāo) 38[titleButton setImage:[UIImage imageWithName:@"navigationbar_arrow_down"] forState:UIControlStateNormal]; 39//設(shè)置背景 40[titleButton setBackgroundImage:[UIImage resizedImage:@"navigationbar_filter_background_highlighted"] forState:UIControlStateHighlighted]; 41 42//設(shè)置尺寸 43titleButton.width=100; 44titleButton.height=35; 45//監(jiān)聽按鈕的點擊事件 46[titleButton addTarget:self action:@selector(titleButtonClick:) forControlEvents:UIControlEventTouchUpInside]; 47self.navigationItem.titleView=titleButton; 48} 49 50-(void)titleButtonClick:(UIButton *)titleButton 51{ 52//UIImage *titleImage=[UIImage imageWithName:@"navigationbar_arrow_down"]; 53// 54//if (titleButton.currentImage==titleImage) { 55//換成箭頭向上 56[titleButton setImage:[UIImage imageWithName:@"navigationbar_arrow_up"] forState:UIControlStateNormal]; 57 58UITableView *tableView=[[UITableView alloc]init]; 59[tableView setBackgroundColor:[UIColor yellowColor]]; 60YYPopMenu *menu=[YYPopMenu popMenuWithContentView:tableView]; 61[menu showInRect:CGRectMake(60, 55, 200, 200)]; 62menu.delegate=self; 63 64//}else 65//{ 66////換成箭頭向下 67//[titleButton setImage:[UIImage imageWithName:@"navigationbar_arrow_down"] forState:UIControlStateNormal]; 68//} 69} 70 71 72#pragmamark-YYPopMenuDelegate 73//彈出菜單 74-(void)popMenuDidDismissed:(YYPopMenu *)popMenu 75{ 76YYTitleButton *titleButton=(YYTitleButton *)self.navigationItem.titleView; 77[titleButton setImage:[UIImage imageWithName:@"navigationbar_arrow_down"] forState:UIControlStateNormal]; 78} 79-(void)pop 80{ 81YYLog(@"---POP---"); 82} 83-(void)friendsearch 84{ 85//跳轉(zhuǎn)到one這個子控制器界面 86YYOneViewController *one=[[YYOneViewController alloc]init]; 87one.title=@"One"; 88//拿到當(dāng)前控制器 89[self.navigationController pushViewController:one animated:YES]; 90 91} 92 93#pragmamark - Table view data source 94- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 95{ 96return20; 97} 98 99- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 100{ 101staticNSString *ID = @"cell"; 102UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:ID]; 103if(!cell) { 104cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID]; 105} 106cell.textLabel.text = [NSString stringWithFormat:@"%d----首頁測試數(shù)據(jù)", indexPath.row]; 107returncell; 108} 109 110- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 111{ 112//點擊cell的時候,,跳到下一個界面 113UIViewController *newVc =[[UIViewController alloc] init]; 114newVc.view.backgroundColor =[UIColor redColor]; 115newVc.title = @"新控制器"; 116[self.navigationController pushViewController:newVc animated:YES]; 117} 118 119@end 實現(xiàn): |
|
來自: 嘆落花 > 《iOS開發(fā)UI》