我正在尝试使用UIBarButtonItem设置image。问题是它永远不会出现。当我只添加带有UIBarButtonItemtitle时,它可以工作。

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

最佳答案

 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;

关于ios - 带有图像的UIBarButtonItem不显示,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29794575/

10-13 03:59