2011-09-05 56 views
9

如何從導航欄上的按鈕刪除光澤/閃耀效果? 如果我通過使用自定義圖像自定義導航欄,按鈕不受影響,我可以從中刪除效果(線條和上光),或爲整個按鈕定義十六進制顏色代碼,甚至爲它們定義一個自定義圖像太?從UINavigationBar中的按鈕刪除閃耀效果

+1

@Rudy - 如果您認爲該問題與其他問題重複,請投票結束,而不是編輯問題的正文。 –

+0

這是[iOS:如何用一種顏色替換搜索欄背景閃耀/發光]的可能副本(http://stackoverflow.com/questions/6696200/ios-how-to-replace-search-bar-background-閃耀着單色) –

回答

18

我剛剛完成了這個過程。基本上,您需要創建自定義的可拉伸圖像並將其用作按鈕的背景以消除光芒。替換UINavigationController中的後退按鈕有點困難。爲此,我使用了UINavigationControllerDelegate來用我的自定義按鈕替換默認的後退按鈕。

下面是一些代碼:

  1. 創建的UIBarButtonItem上一個類別創建您的自定義按鈕。這是我的。我使用這個類來定義包括經常欄按鈕和後退按鈕:

    @interface UIBarButtonItem (UIBarButtonItem_customBackground) 
    
    + (id) customBarButtonWithTitle:(NSString *)title target:(id)target selector:(SEL)selector; 
    + (id) customBackButtonWithTitle:(NSString *)title target:(id)target selector:(SEL)selector; 
    
    @end 
    
    @implementation UIBarButtonItem (UIBarButtonItem_customBackground) 
    
    + (id) customButtonWithImageNamed:(NSString *)imageName selectedImageNamed:(NSString *)selectedImageName leftCapWidth:(CGFloat)leftCapWidth edgeInsets:(UIEdgeInsets)edgeInsets title:(NSString *)title target:(id)target selector:(SEL)selector { 
        UIButton* customButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
        [customButton addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside]; 
        customButton.titleLabel.font = [UIFont boldSystemFontOfSize:12.0f]; 
        customButton.titleLabel.shadowColor = [UIColor colorWithRed:0.0f/255.0f green:0.0f/255.0f blue:0.0f/255.0f alpha:0.25f]; 
        customButton.titleLabel.shadowOffset = CGSizeMake(0.0f, -1.0f); 
        customButton.titleLabel.lineBreakMode = UILineBreakModeTailTruncation; 
        customButton.titleEdgeInsets = edgeInsets; 
        UIImage* navButtonBackgroundImage = [[UIImage imageNamed:imageName] stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:0.0f]; 
        UIImage* navButtonPressedBackgroundImage = [[UIImage imageNamed:selectedImageName] stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:0.0f]; 
        [customButton setBackgroundImage:navButtonBackgroundImage forState:UIControlStateNormal]; 
        [customButton setTitle:title forState:UIControlStateNormal]; 
        [customButton setBackgroundImage:navButtonPressedBackgroundImage forState:UIControlStateHighlighted]; 
        [customButton setBackgroundImage:navButtonPressedBackgroundImage forState:UIControlStateSelected]; 
    
        CGSize size = CGSizeMake(30.0f, 30.0f); 
        if (title != nil) { 
         size = [[NSString stringWithString:title] sizeWithFont:customButton.titleLabel.font]; 
        } 
        customButton.frame = CGRectMake(0.0f, 0.0f, size.width + 20.0f, 30.0f); 
        customButton.layer.shouldRasterize = YES; 
        customButton.layer.rasterizationScale = [[UIScreen mainScreen] scale]; 
        return [[[UIBarButtonItem alloc] initWithCustomView:customButton] autorelease]; 
    } 
    
    + (id) customBarButtonWithTitle:(NSString *)title target:(id)target selector:(SEL)selector { 
        return [self customButtonWithImageNamed:@"navButtonBG.png" 
           selectedImageNamed:@"navButtonPressedBG.png" 
             leftCapWidth:6.0f 
             edgeInsets:UIEdgeInsetsMake(0.0f, 5.0f, 0.0f, 5.0f) 
               title:title 
              target:target 
              selector:selector]; 
    } 
    
    + (id) customBackButtonWithTitle:(NSString *)title target:(id)target selector:(SEL)selector {  
        return [self customButtonWithImageNamed:@"backButtonBG.png" 
           selectedImageNamed:@"backButtonPressedBG.png" 
             leftCapWidth:12.0f 
             edgeInsets:UIEdgeInsetsMake(0.0f, 11.0f, 0.0f, 5.0f) 
               title:title 
              target:target 
              selector:selector]; 
    } 
    
    @end 
    
  2. 添加按鈕,您的UINavigationBar的

    UIBarButtonItem* logoutButton = [UIBarButtonItem customBarButtonWithTitle:@"Logout" target:self selector:@selector(logout)]; 
    self.navigationItem.rightBarButtonItem = logoutButton; 
    
  3. 如果你也想更換的UINavigationController的後退按鈕,設置一個UINavigationControllerDelegate並執行willShowViewController方法,如下所示:

    - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated { 
        if([navigationController.viewControllers count ] > 1) { 
         UIViewController* backViewController = [navigationController.viewControllers objectAtIndex:(navigationController.viewControllers.count - 2)]; 
         NSString* backText = backViewController.title; 
         UIBarButtonItem* newBackButton = [UIBarButtonItem customBackButtonWithTitle:backText target:navigationController selector:@selector(popViewControllerAnimated:)]; 
         viewController.navigationItem.leftBarButtonItem = newBackButton; 
         viewController.navigationItem.hidesBackButton = YES; 
        } 
    } 
    
  4. 這裏是拉伸hable圖片我使用:

    • 後退按鈕:back button追問:enter image description here
    • 常規按鈕:enter image description here追問:enter image description here
+0

你。是。所以。 FEAKING。真棒!!!!!我喜歡你! – Eugene

+0

@Justin Gallagher 賈斯汀謝謝你謝謝你謝謝你! –

+0

如何將此代碼添加到項目中以使用它? –

0

你必須使用圖像沒有任何光澤效果的自定義按鈕,通過它你可以擺脫導航欄中按鈕的黃金效果。

2

爲了改變後退按鈕沒有必要實施委託方法uinavigationcontroller。

您只需在設置完所需的後退按鈕後將hidesBAckButton屬性設置爲YES,就像@Justin Gallacher完美解釋的那樣。

self.navigationItem.leftBarButtonItem = [UIBarButtonItem customBackButtonWithTitle:@"Back" target:self.navigationController selector:@selector(popViewControllerAnimated:)]; 
self.navigationItem.hidesBackButton = YES; 
+0

這甚至不是必需的,設置leftBarButtonItem屬性將覆蓋後退按鈕 – Daniel