2013-07-31 33 views
7

我想在工具欄的頂部和中心顯示按鈕。我設置了UIBarButtonItem的圖像,但沒有給出如下圖所示的所需結果。如何在工具欄中心顯示按鈕?

我能夠添加菜單,找到我

如圖PIC不能添加請求按鈕

請求按鈕是一個圖像,當我把它的形象的UIBarButtonItem它被拉伸

請幫助

這是我做了什麼。 enter image description here

但我想這樣做。因爲您可以看到,在我的解決方案中,「請求」按鈕位於工具欄下方,下方的「請求」按鈕位於頂部。 enter image description here

+1

需要代碼.... – IronManGill

+0

這裏你要在中心顯示這兩個按鈕,爲此只需添加'UIBarButtonItem'類型爲'UIBarButtonSystemItemFlexibleSpace',以便用按鈕調整兩側並將這些按鈕設置爲中心就可以看到我的答案.. :) –

回答

18

只是FlexibleSpace添加UIBarButtonItem像波紋管這兩個按鈕的兩側..

UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 

見有兩個按鈕的所有代碼..

UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 

UIBarButtonItem *locateMe = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStylePlain target:self action:@selector(locateMe:)]; 

UIBarButtonItem *request = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStylePlain target:self action:@selector(request:)]; 

UIBarButtonItem *menu = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStylePlain target:self action:@selector(menu:)]; 

NSArray *toolbarItems = [NSArray arrayWithObjects:locateMe,flexibleSpace,request , flexibleSpace, menu]; 

[toolBar setItems:toolbarItems animated:NO]; 

XIB您還可以設置UIBarButtonSystemItemFlexibleSpace如下圖所示..

UPDATE:你想補充一點,請求按鈕與UIToolBar該圖像,然後只是把那個UIImageView和按鈕後,添加UIToolBarUIView像波紋管..

[self.view bringSubviewToFront:imgView]; 
[self.view bringSubviewToFront:btnRequest]; 
+0

只需編輯我的問題。 – Azhar

+0

@NG_SE我知道你只有兩個按鈕在工具欄上嗎?所以我寫你想要的確切代碼:) –

+0

@NG_SE也發佈代碼,您添加這些欄按鈕.. –

3

從筆尖使用靈活UIToolbar對象上的空間項目。像波紋管圖片: -

enter image description here

添加中心按鈕更大然後工具欄海特然後檢查該波紋管演示,並使用其類別完成這個任務,只需將您的圖像到演示和改變形象的名字CenterButtonIconPaw.png檢查: -

https://github.com/el-eleven/EEToolbarCenterButton

使用你的形象我這樣做,只要你想它的外觀像類似於您需要: -

enter image description here

+0

謝謝,這是我所需要的。 – Geoff