2015-05-08 45 views
10

是否可以在UIToolbar內調整UIBarButtonItem的標題?在UIToolbar中調整UIBarButtonItem標題的垂直位置

我試過下面的代碼行沒有成功:

UIBarButtonItem.appearance().setTitlePositionAdjustment(UIOffset(horizontal: 30, vertical: 30), forBarMetrics: UIBarMetrics.Default) 

這一個:

self.setTitlePositionAdjustment(UIOffset(horizontal: 30, vertical: 30), forBarMetrics: UIBarMetrics.Compact) 
+0

什麼沒有麥芽汁? –

+0

縱向調整標題的位置 – Antoine

+0

您是否嘗試過使用setTitleEdgeInsets來更改標題位置,嘗試使用此標題,希望這可以解決您的問題。 –

回答

4

偏移的UIBarButtonItem(文字!)內的UIToolBar垂直似乎不工作Xcode 7(Beta 6)。與該項目出口本身

UIBarButtonItem.appearance().setTitlePositionAdjustment(UIOffset(horizontal: 30, vertical: 30), forBarMetrics: UIBarMetrics.Default) 

或本地:

(SWIFT)

你說得對,according to the docs,這應該這樣做: UIBarButtonItem.appearance().setTitlePositionAdjustment

無論是在全球範圍內的AppDelegate

self.myBarItem.setTitlePositionAdjustment(UIOffset(horizontal: 30, vertical: 30), forBarMetrics: UIBarMetrics.Default) 

(Obj C)

[self.myBarItem setTitlePositionAdjustment:UIOffsetMake(30, 30) forBarMetrics: UIBarMetricsDefault]; 

這是外觀錯誤嗎?

+5

水平工作但垂直不作任何改變你知道爲什麼嗎? – pprevalon

+0

我有同樣的問題。當我將工具欄高度更改爲44時,barbuttonitems變爲垂直中心。希望它能幫助別人。 – RVJ

1

我對此也感到沮喪,所以我將子類UIBarButtonItem按預期工作。從this答案以我想出了這一點:

@interface TitleAdjustingBarButtonItem(){ 
    UIView* _parentView; 
    UIButton* _button; 
} 

@end 

@implementation TitleAdjustingBarButtonItem 

-(id) initWithTitle:(NSString *)title style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action{ 
    _button = [[UIButton alloc] init]; 
    [_button setTitle:title forState:UIControlStateNormal]; 

    [_button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside]; 

    [_button sizeToFit]; 

    _parentView = [[UIView alloc] initWithFrame:_button.bounds]; 

    [_parentView addSubview:_button]; 

    return [super initWithCustomView:_parentView]; 
} 

#pragma mark - property setters - 

-(void) setTitlePositionAdjustment:(UIOffset)adjustment forBarMetrics:(UIBarMetrics)barMetrics{ 
    [_button sizeToFit]; 
    _parentView.bounds = CGRectMake(0.0, 0.0, _button.bounds.size.width - (adjustment.horizontal * 2.0), _button.bounds.size.height - (adjustment.vertical * 2.0)); 
} 

誠然它不佔不同的酒吧指標,但是默認的指標我已經得到這個工作得很好。

0

斯威夫特3版本UITabBarItem編程方式創建(你也可以改變這個屬性網點):

let barItem = UITabBarItem(title: "Menu", 
          image: UIImage(named: "imageName"), 
          selectedImage: UIImage(named: "selectedImageName")) 
barItem.setTitlePositionAdjustment(UIOffset(horizontal: 0, vertical: -10), for: UIBarMetrics.default) 
0

我找到了一個解決方法。我試圖改變導航欄中後退按鈕標題的位置。標題似乎在其左下角固定。所以我改變了整個導航欄的位置並相應地調整了它的大小,因此它在視圖中似乎是相同的大小。

let xDisplacement: CGFloat = -25 // Adjust horizontal position 
    let yDisplacement: CGFloat = 9 // Adjust vertical position 
    let h = (self.viewIfLoaded?.frame.size.height)!/12 //Set your height in reference to the size of the view 
    let w = (self.viewIfLoaded?.frame.size.height)!//Set your width in reference to the size of the view 
//Change placement adjusting size for change. 
    self.navigationController?.navigationBar.frame = CGRect(x: xDisplacement, y: yDisplacement, width: w-xDisplacement, height: h-yDisplacement) 

在我的情況下背景是透明的,所以調整沒有真正有所作爲。我還試圖將標題移開。我認爲這是調整有用的唯一情況。

無論如何,只是一種解決方法,但我希望它有幫助。

總的noob在這裏,反饋讚賞。