带图像的UIBarButtonItem不显示

时间:2015-04-22 10:23:44

标签: ios objective-c uinavigationcontroller uibarbuttonitem

我尝试使用UIBarButtonItem设置image。问题是它永远不会出现。当我只添加一个UIBarButtonItem title时就可以了。

UIBarButtonItem *openLeftSliderButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"filter.png"] style:UIBarButtonItemStylePlain target:self action:@selector(openLeftSlider)];
    self.navigationItem.leftBarButtonItem = openLeftSliderButton;

1 个答案:

答案 0 :(得分:1)

 UIImage *sideImage = [UIImage imageNamed:@"i1.jpg"];    
 UIButton *btnSideMenu = [UIButton buttonWithType:UIButtonTypeCustom];     
 [btnSideMenu setImage:sideImage forState:UIControlStateNormal]; 
 [btnSideMenu setImage:sideImage forState:UIControlStateHighlighted];  
 [btnSideMenu setFrame:CGRectMake(0, 0, 20, 20)]; 
 [btnSideMenu addTarget:self action:@selector(btnLeftMenuClicked) forControlEvents:UIControlEventTouchUpInside];  
 UIBarButtonItem *sideMenuItem = [[UIBarButtonItem alloc] initWithCustomView:btnSideMenu];    
 self.navigationItem.leftBarButtonItem=sideMenuItem;