实例代码:
1 - (void)viewDidLoad { 2 [super viewDidLoad]; 3 4 [self setTitle:@"Test"]; 5 6 //以下是主要实现代码 7 UIButton *moreBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 50)]; 8 [moreBtn setTitle:@"more" forState:UIControlStateNormal]; 9 [moreBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 10 [moreBtn addTarget:self action:@selector(didClickMoreBtn) forControlEvents:UIControlEventTouchUpInside]; 11 UIBarButtonItem *moreItem = [[UIBarButtonItem alloc] initWithCustomView:moreBtn]; 12 self.navigationItem.leftBarButtonItem = moreItem; 13 14 } 15 16 - (void)didClickMoreBtn 17 { 18 NSLog(@"hello more"); 19 }
效果: