2012-02-22 192 views
1

我想將UIToolBar放入UINavigationBarUIToolBar背景透明

UIToolbar* tempFontSizeToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(kPaginationToolBarOriginX,kPaginationToolBarOriginY,kPaginationToolBarWidth,kPaginationToolBarHeight)]; 

    tempFontSizeToolBar.backgroundColor = [UIColor clearColor]; 

    // create the array to hold the buttons, which then gets added to the toolbar 

    NSMutableArray* buttons = [[NSMutableArray alloc] init]; 
    [tempFontSizeToolBar setTranslucent:YES]; 
    UIBarButtonItem *fontSizeBarButtonItem; 

    fontSizeBarButtonItem = [[UIBarButtonItem alloc] 
         initWithImage:[UIImage imageNamed:KpreviousPageIcon] style:UIBarButtonItemStylePlain target:self action:@selector(movePreviousPage:)]; 

    [buttons addObject:fontSizeBarButtonItem]; 

    [fontSizeBarButtonItem release];fontSizeBarButtonItem = nil; 

    fontSizeBarButtonItem = [[UIBarButtonItem alloc] 
         initWithImage:[UIImage imageNamed:KnextpageIcon] style:UIBarButtonItemStylePlain target:self action:@selector(moveNextPage:)]; 

    [buttons addObject:fontSizeBarButtonItem]; 

    [fontSizeBarButtonItem release];fontSizeBarButtonItem = nil; 

// stick the buttons in the toolbar 
    [tempFontSizeToolBar setItems:buttons animated:NO]; 

    [buttons release];buttons = nil; 

    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithCustomView:tempFontSizeToolBar]; 

    self.navigationItem.rightBarButtonItem = rightBarItem; 

UIToolBar的背景色是默認的藍色。 但我需要的工具欄應該是清晰的顏色,以便NavigationBar的背景圖像也應該出現在該工具欄中。

請給我建議。

回答

3

不確定你在這裏之後,但你的代碼是混亂的,我認爲你可以得到你想要的東西沒有很多努力。無論如何,一個按鈕項目不應該有一個工具欄作爲其自定義視圖。

如果你的目標是在UINavigationBar左邊有一個'prev'按鈕和一個'next'按鈕,那麼你可以將它們設置爲UINavigationItem的leftBarButtonItemrightBarButtonItem。沒有數組必要。如果你的目標是讓'prev'和'next'彼此相鄰並且在UINavigationBar的右邊,然後把它們('next'first)放在一個數組中,然後使用UINavigationItem的setRightBarButtonItems:animated:

在任何情況下都不需要UIToolbar。您可以按照Apple的文檔here將UIToolbar與UINavigationController耦合。它彈出在屏幕的底部,可能不是你想要的,但你可以設置它的色調或背景圖像。如果您必須在頂部有工具欄,您可以創建一個並手動將其放在那裏,而不是太難。

祝你好運!

+0

我把工具欄上只有導航欄的頂部。我想要的是工具欄應該是透明的,這樣工具欄中的按鈕就像它直接放置在導航欄中一樣。 – Bharathi 2012-02-22 07:48:38

+0

我已經嘗試過這些方法。 setRightBarButtonItems:animated:is only only ios 5 .its crashing when running it on version 4. – Bharathi 2012-02-22 07:55:18

+0

那麼你要做的是模仿'setRightBarButtonItems:animated:'在iOS 5中? – QED 2012-02-22 16:08:10

5

爲了使工具欄透明,使用以下命令:

const float colorMask[6] = {222, 255, 222, 255, 222, 255}; 
UIImage *img = [[UIImage alloc] init]; 
UIImage *maskedImage = [UIImage imageWithCGImage: CGImageCreateWithMaskingColors(img.CGImage, colorMask)]; 

[self.toolbar setBackgroundImage:maskedImage forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault]; 
+0

這模擬了我在iOS 6中遇到的不同問題的正確行爲。[我在這個主題上開始的線程在這裏。](http://stackoverflow.com/questions/16618410/uitoolbar-buttons-with-uiimage-不完全透明) – 2013-05-17 22:35:30

2

集toolbarStyle -1這樣

tools.barStyle = -1; // clear background 
+0

這幾乎適用於作爲UIViewController根視圖的MKMapView之上的UIToolbar。剩下的問題是工具欄的細線仍然被繪製。這是修復:http://stackoverflow.com/questions/19110883/remove-uitoolbar-hairline-in-ios-7 – Barry 2015-07-28 23:38:32