【新浪微博專案】07--設定navigationItem和自定義TitleButton
阿新 • • 發佈:2019-02-15
#import "MRHomeViewController.h" #import "MRNavigationController.h" #import "Header.h" #import "UIImage+MJ.h" #import "UIBarButtonItem+MJ.h" #import "MRTitleButton.h" @interface MRHomeViewController () @end @implementation MRHomeViewController - (void)viewDidLoad { [super viewDidLoad]; //左邊按鈕 self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"navigationbar_friendsearch" highIcon:@"navigationbar_friendsearch_highlighted" action:@selector(findFriend) target:self]; //右邊按鈕 self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithIcon:@"navigationbar_pop" highIcon:@"navigationbar_pop_highlighted" action:@selector(pop) target:self]; //中間按鈕 MRTitleButton *titleButton = [[MRTitleButton alloc]init]; titleButton.bounds = CGRectMake(0, 0, 100, 40); [titleButton setImage:[UIImage imageWithName:@"navigationbar_arrow_down"] forState:UIControlStateNormal]; [titleButton setTitle:@"好友圈" forState:UIControlStateNormal]; [titleButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; [titleButton addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside]; self.navigationItem.titleView = titleButton; } - (void)titleClick:(MRTitleButton *)titleButton { if (titleButton.tag == -1) { [titleButton setImage:[UIImage imageWithName:@"navigationbar_arrow_down"] forState:UIControlStateNormal]; titleButton.tag = 0; } else { [titleButton setImage:[UIImage imageWithName:@"navigationbar_arrow_up"] forState:UIControlStateNormal]; titleButton.tag = -1; } } -(void)findFriend { NSLog(@"--------findFriend"); } -(void)pop { NSLog(@"------------pop"); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { #warning Potentially incomplete method implementation. // Return the number of sections. return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { #warning Incomplete method implementation. // Return the number of rows in the section. return 20; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *ID = @"cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (cell == nil) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID]; } // Configure the cell... cell.textLabel.text = @"HHHHHHHH"; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UIViewController *vc = [[UIViewController alloc]init]; vc.view.backgroundColor = [UIColor redColor]; //隱藏下面的TabBar //vc.hidesBottomBarWhenPushed = YES; [self.navigationController pushViewController:vc animated:YES]; } @end
3.UIBarButtonItem新增分類
+ (UIBarButtonItem*)itemWithIcon:(NSString*)icon highIcon:(NSString*)highIcon action:(SEL)action target:(id)target { UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setBackgroundImage:[UIImage imageWithName:icon] forState:UIControlStateNormal]; [button setBackgroundImage:[UIImage imageWithName:highIcon] forState:UIControlStateHighlighted]; button.bounds = CGRectMake(0, 0, button.currentBackgroundImage.size.width , button.currentBackgroundImage.size.height); [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside]; return [[UIBarButtonItem alloc]initWithCustomView:button]; }
結果圖: