2013-09-21 56 views
11

由於某些原因,導航欄中的右側按鈕距離右側爲16px。我想使邊距更小。什麼是正確的方法來做到這一點?iOS7中的navigationItem.rightBarButtonItem保證金

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]; 

enter image description here

+0

您在此期間是否找到答案? –

回答

8

我有同樣的問題,我發現修復它是使用setImageEdgeInsets:的唯一途徑。

如果你想你的按鈕移動到右側(例如:5分或10像素),請將以下行添加到按鈕聲明:

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]; 

希望它有幫助!

+0

但是,如果我使用backgroundImage按鈕,這會工作嗎?因爲背景會被擠壓。 –

+0

我的錯誤,我沒有看到你正在使用'setBackgroundImage:forState:','setImageEdgeInsets:'爲我工作,但我使用'setImage:forState:'(它不會擠壓它)。 –

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]; 
+0

我試過了,但結果只是我的按鈕空間不足。保留右邊的餘量。 –

+0

我已經用setWidth的不同值再次對它進行了測試,它適用於我。 – RFG

+0

當我使用沒有背景圖像的UIButton時,它可以工作。你有沒有背景圖片和按鈕標題文字? –

0

如果您仍然無法得到它的工作,出於某種原因這對我有效。 someButton是我添加的customView我的UIBarButtonItem

someButton.contentEdgeInsets = (UIEdgeInsets){.right=-20}; 
1

見礦的UIButton。

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 
}