UIBarButtonItem离后退按钮太远了

时间:2016-08-14 07:22:16

标签: objective-c uibarbuttonitem uinavigationitem

我正在使用navigtaionItem的leftItemsSupplementBackButton设置后退按钮,然后将UIBarButtonItem设置为我的leftBarButton。 问题是按钮离后退按钮太远了。如何控制其位置并使其与后退按钮相邻?

下面的示例代码设置了后退按钮和左侧按钮项目。

UIBarButtonItem* bla = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"delete"] style:UIBarButtonItemStyleDone target:self action:nil];
self.navigationItem.leftItemsSupplementBackButton = YES;
self.navigationItem.leftBarButtonItem = bla;

请查看随附的屏幕截图。enter image description here

谢谢!

2 个答案:

答案 0 :(得分:0)

尝试此操作并根据需要更改值(CGFloat)。

<>

答案 1 :(得分:0)

您应该在代码后添加此语句:

 [self.navigationItem.leftBarButtonItem setImageInsets:UIEdgeInsetsMake(0, -40, 0, 0)];//may be not -40 ,you can alter this argument.
                          //UIEdgeInsetsMake(topMargin, leftMargin,buttomMargin,rightMargin)
像这样:

enter image description here

学习此方法的最佳方法是使用它!

所以你应该将不同的参数传递给这个方法' setImageInsets:(UIEdgeInset)',如下所示:

//UIEdgeInsetsMake(topMargin, leftMargin,buttomMargin,rightMargin)
[self.navigationItem.leftBarButtonItem setImageInsets:UIEdgeInsetsMake(-50, 0, 0, 0)];
[self.navigationItem.leftBarButtonItem setImageInsets:UIEdgeInsetsMake(0, -50, 0, 0)];
[self.navigationItem.leftBarButtonItem setImageInsets:UIEdgeInsetsMake(0, 0, 50, 0)];
[self.navigationItem.leftBarButtonItem setImageInsets:UIEdgeInsetsMake(0, 0, 0, 50)];

当您发现图片的形状发生变化时,请参阅以下内容:

如果您发现自己的图像形状为self.navigationItem.leftBarButtonItem已更改,则应按以下方式编码:

 [self.navigationItem.leftBarButtonItem setImageInsets:UIEdgeInsetsMake(0, -40, 0, 40)];

原因是: enter image description here

希望对你有所帮助!