通过IB添加时,UIBarButtonItem图标为白色,以编程方式添加时为黑色

tob*_*yer 7 iphone cocoa-touch uikit uibarbuttonitem

当我UIBarButtonItem通过Interface Builder 向a添加图标时,图标显示为白色.当我以编程方式将相同的图标文件添加到另一个图标文件时UIToolbar,图标显示为黑色.为什么?

UIImage *image = [UIImage imageNamed:@"icon.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:image forState:UIControlStateNormal];
rootViewController.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:reloadButton] autorelease];
Run Code Online (Sandbox Code Playgroud)

ton*_*lon 6

Jongsma所说的一切都是对的,你应该使用initWithImage:style:message.

下一个问题不是您创建的方式UIBarButtonItem,而是您分配它的位置.您可以使用UIBarButtonItemStylePlain创建它,它通常应该将图标的轮廓呈现为白色,但UIBavigationItem的RightBarButtonItem(就像左边一样)不允许使用UIBarButtonItemStylePlain.它隐式转换为UIBarButtonItemStyleBordered.在边框样式中,图标按"原样"呈现,黑色带有轻微渐变.

我想如果你想要一个有边框的barButton上的白色项目,你将不得不触摸图像本身.


ben*_*ord 6

答:如果您想要白色,请将图像的颜色设置为白色.

细节:

UIBarButtonItems的行为略有不同,具体取决于您如何使用它们.

添加到UIToolbar:

initWithImage:style:target:action:创建"白色图标"(忽略图像颜色,不透明像素用作创建白色图像的蒙版).
这适用于borderedplain样式(但仅限于UIToolbar).

initWithCustomView: 显示正常的彩色图像.

添加到UINavigationItem:

initWithImage:style:target:action:创建彩色图像并转换plainbordered.