2016-01-21 21 views
0

我有一個草圖接口原型,可以從中獲取UI元素的座標。當我使用圖像創建欄按鈕時,它被置於導航欄上的默認位置。代碼如下:在iOS 7中獲取相對於其導航欄的UIBarButtonItem圖像位置

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"my-image"] style:UIBarButtonItemStylePlain target:self action:nil]; 

此默認位置與期望的位置不同。是否有可能找出相對於其導航欄 (white rect on this screenshot)的默認圖像位置(框架),而無需製作自定義按鈕?我需要它將圖像向左移動一點,但我不想用近似偏移量在視覺上進行操作。

+0

你可以發表scre en shot? –

+0

@KetanP,我添加了問題的截圖鏈接。 – nemissm

+0

您需要提供更多的代碼細節,並指定更好的解決方案。 – brito

回答

0

可以使用邊緣插圖
對於按鈕上的文字標題:

button.titleEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, 0.0, 5.0); 

對於按鈕,圖像:

button.imageEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, 0.0, 5.0); 


或者,如果你想水平移動唯一的按鈕:

UIBarButtonItem *spaceButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; 
[spaceButtonItem setWidth:-3]; // to move on the left 
self.navigationItem.leftBarButtonItems = @[spaceButtonItem, leftButton]; 
+0

謝謝,我知道移動欄按鈕的方法,但問題是關於獲取圖像(不是酒吧按鈕)的位置。因爲使用這種方法,我只能嘗試一些偏移值,直到位置看起來不錯。但是我怎麼能知道我把圖像放在模型中描述的同一點? – nemissm

相關問題