我认为这将是一个愚蠢的问题,但我似乎找不到答案。我有几行简单的代码可以在导航栏中放置一个按钮:
UIBarButtonItem *cancelButton=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"button-cancel.png"] style:UIBarButtonItemStylePlain target:self action:@selector(cancelPressed:)];
UINavigationItem *item = [[UINavigationItem alloc] init];
item.leftBarButtonItem = cancelButton;
item.hidesBackButton = YES;
[self.navigationBar pushNavigationItem:item animated:NO];
这个按钮可以正常工作,但是看起来像这样:
有什么想法吗?
最佳答案
您可能想使用自定义视图创建条形按钮项目,其中自定义视图为UIButton
:
UIImage *cancelImage = [UIImage imageNamed:@"button-cancel"];
UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
cancelButton.frame = (CGRect){CGPointZero, cancelImage.size);
[cancelButton setImage:cancelImage forState:UIControlStateNormal];
UIBarButtonItem *cancelBarButton = [[UIBarButtonItem alloc] initWithCustomView:cancelButton];