我正在尝试在viewWillAppear方法中使用此代码使用自定义rightBarButtonItem图像:
// Custom rightBarButtonItem
UIImage *buttonImage = [UIImage imageNamed:@"menu_button"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[button addTarget:self action: @selector(handleBackButton)
forControlEvents:UIControlEventTouchUpInside];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, buttonImage.size.width - 10, buttonImage.size.height - 10)];
[view addSubview:button];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:view];
self.navigationItem.rightBarButtonItem = customBarItem;
结果如下:
但我希望它更小,就像我在Sketch 3中设计的那样:
为实现这一目标,我需要做些什么?
谢谢! 埃里克
答案 0 :(得分:0)
我相信你需要调整图像边缘插图:
[button setImageEdgeInsets:UIEdgeInsetsMake(topInset, leftInset, bottomInset, rightInset)];
答案 1 :(得分:0)
建议您将UIIMage的渲染模式设置为 UIImageRenderingModeAlwaysOriginal ,如下所示。
UIImage *barButtonimage = [[UIImage imageNamed:@"menu_button"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
然后您可以将UIBarButtonItem的图像设置为以下
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImage: barButtonimage style:UIBarButtonItemStylePlain target:self action:@selector(buttonAction:)];
UIImageRenderingModeAlwaysOriginal:始终绘制原始图像,而不将其视为模板。