11

出于某种原因,导航栏中的右侧按钮距右侧 16 像素。我想缩小边距。这样做的正确方法是什么?

self.btnDone = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *btnImgDone = [UIImage imageNamed:@"btn_small_default.png"];
self.btnDone.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:15.0];
[self.btnDone setTitle:@"Done" forState:UIControlStateNormal];
[self.btnDone setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self.btnDone setTitleShadowColor:[UIColor colorWithWhite:0 alpha:.2f] forState:UIControlStateNormal];
self.btnDone.titleLabel.shadowOffset = (CGSize){0,-1};
[self.btnDone setBackgroundImage:btnImgDone forState:UIControlStateNormal];
[self.btnDone setBackgroundImage:[UIImage imageNamed:@"btn_small_active.png"] forState:UIControlStateHighlighted];  

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.btnDone];

在此处输入图像描述

4

4 回答 4

8

我有同样的问题,我发现解决它的唯一方法是使用setImageEdgeInsets:.

如果要将按钮向右移动(例如:5 pts 或 10 px),请在按钮声明中添加以下行:

UIEdgeInsets buttonEdges = UIEdgeInsetsMake(0, 5, 0, -5);
[self.btnDone setImageEdgeInsets:buttonEdges];

如果你想支持 iOS6 和 iOS7,你可以这样做:

CGFloat xOffset;

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0f)
{
    // for ios7
    xOffset = 5.0f;
}
else
{
    // ios6
    xOffset = 2.0f;
}

UIEdgeInsets buttonEdges = UIEdgeInsetsMake(0, xOffset, 0, - xOffset);
[self.btnDone setImageEdgeInsets:buttonEdges];

希望能帮助到你 !

于 2013-09-30T08:35:07.317 回答
6

尝试在该按钮后放置一个固定空格:

// Fixed space
UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[fixedSpace setWidth:20.0];

self.navigationItem.rightBarButtonItems = @[fixedSpace, yourButton];

我使用的整个代码是:

// Bar button
UIButton *loadButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 15, 34)];
VVdLoadImage *loadImage = [[VVdLoadImage alloc] initWithFrame:CGRectMake(0, 0, 15, 34)];
loadImage.backgroundColor = [UIColor clearColor];
loadImage.userInteractionEnabled = NO;
[loadButton addSubview:loadImage];

[loadButton addTarget:self action:@selector(loadCards) forControlEvents:UIControlEventTouchDown];
UIBarButtonItem *loadBarButton = [[UIBarButtonItem alloc] initWithCustomView:loadButton];

UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[fixedSpace setWidth:20.0];

self.navigationItem.rightBarButtonItems = @[fixedSpace, loadButton];
于 2013-09-30T09:07:12.163 回答
1

见我的。

func addLogOutButtonToNavigationBar(triggerToMethodName: String)
{
    let button: UIButton = UIButton()
    button.setImage(UIImage(named: "logOff.png"), forState: .Normal)
    button.frame = CGRectMake(20, 0, 30, 25)
    button.contentEdgeInsets = UIEdgeInsets.init(top: 0, left: 10, bottom: 0, right: -10)

    button .addTarget(self, action:Selector(triggerToMethodName), forControlEvents: UIControlEvents.TouchUpInside)
    let rightItem:UIBarButtonItem = UIBarButtonItem()
    rightItem.customView = button
    self.navigationItem.rightBarButtonItem = rightItem
}
于 2015-11-19T08:08:44.913 回答
0

如果你仍然不能让它工作,出于某种原因,这对我有用。someButton 是我作为 UIBarButtonItem 的 customView 添加的 UIButton

someButton.contentEdgeInsets = (UIEdgeInsets){.right=-20};
于 2015-02-17T14:27:00.470 回答