Iphone 在UINavigationBar中更改UIButton高亮显示的图像
当突出显示时,我试图在Iphone 在UINavigationBar中更改UIButton高亮显示的图像,iphone,objective-c,ios,uinavigationcontroller,uinavigationbar,Iphone,Objective C,Ios,Uinavigationcontroller,Uinavigationbar,当突出显示时,我试图在ui按钮中更改ui图像。ui按钮位于ui导航控制器中 我有以下代码: UIView *containingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 28, 28)]; UIButton *barUIButton = [UIButton buttonWithType:UIButtonTypeCustom]; [barUIButton setImage:[UIImage imageNamed:@"Add.png"
ui按钮
中更改ui图像
。ui按钮
位于ui导航控制器
中
我有以下代码:
UIView *containingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 28, 28)];
UIButton *barUIButton = [UIButton buttonWithType:UIButtonTypeCustom];
[barUIButton setImage:[UIImage imageNamed:@"Add.png"] forState:UIControlStateNormal];
barUIButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
barUIButton.frame = CGRectMake(-9, 0, 28, 28);
[barUIButton addTarget:self action:@selector(addButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[barUIButton setImage:[UIImage imageNamed:@"AddHighlighted.png"] forState:UIControlStateSelected | UIControlStateHighlighted];
[containingView addSubview:barUIButton];
UIBarButtonItem *containingBarButton = [[[UIBarButtonItem alloc] initWithCustomView:containingView] autorelease];
self.navigationItem.rightBarButtonItem = containingBarButton;
与高亮显示时显示的新图像不同,现有图像周围只有一个黑色阴影
这是为什么?在触碰事件中,
ui按钮
实例似乎没有突出显示或选中。这可能是因为UIBarButtonItem
实例的行为与普通按钮不同;事实上,它们甚至不是UIButton子类
有一个变通办法。如果在实例变量中保留对ui按钮的引用,则可以添加代码来更改按钮的图像:
[barUIButton addTarget:self action:@selector(pressDown:) forControlEvents:UIControlEventTouchDown];
[barUIButton addTarget:self action:@selector(pressUp:) forControlEvents:UIControlEventTouchUp];
在下按:
和上按:
,您可以设置
-(void)pressDown:(id)sender
{
[barUIButton setImage:[UIImage imageNamed:@"Add.png"] forState:UIControlStateNormal];
}
同样,对于按UP:
使用png
文件和imageNamed:
时,不必指定文件扩展名<代码>[UIImage ImageName:@“Add”]
就足够了。尽管新图像上刚刚出现了黑色光晕,但这仍然有效。如何停止默认的uibarbuttoneim黑色发光?我不确定您是否能够-发光是uibarbuttoneim
的一部分,可能无法配置。