如何從導航欄上的按鈕刪除光澤/閃耀效果? 如果我通過使用自定義圖像自定義導航欄,按鈕不受影響,我可以從中刪除效果(線條和上光),或爲整個按鈕定義十六進制顏色代碼,甚至爲它們定義一個自定義圖像太?從UINavigationBar中的按鈕刪除閃耀效果
回答
我剛剛完成了這個過程。基本上,您需要創建自定義的可拉伸圖像並將其用作按鈕的背景以消除光芒。替換UINavigationController中的後退按鈕有點困難。爲此,我使用了UINavigationControllerDelegate來用我的自定義按鈕替換默認的後退按鈕。
下面是一些代碼:
創建的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
添加按鈕,您的UINavigationBar的
UIBarButtonItem* logoutButton = [UIBarButtonItem customBarButtonWithTitle:@"Logout" target:self selector:@selector(logout)]; self.navigationItem.rightBarButtonItem = logoutButton;
如果你也想更換的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; } }
這裏是拉伸hable圖片我使用:
- 後退按鈕:追問:
- 常規按鈕:追問:
你。是。所以。 FEAKING。真棒!!!!!我喜歡你! – Eugene
@Justin Gallagher 賈斯汀謝謝你謝謝你謝謝你! –
如何將此代碼添加到項目中以使用它? –
你必須使用圖像沒有任何光澤效果的自定義按鈕,通過它你可以擺脫導航欄中按鈕的黃金效果。
爲了改變後退按鈕沒有必要實施委託方法uinavigationcontroller。
您只需在設置完所需的後退按鈕後將hidesBAckButton屬性設置爲YES,就像@Justin Gallacher完美解釋的那樣。
self.navigationItem.leftBarButtonItem = [UIBarButtonItem customBackButtonWithTitle:@"Back" target:self.navigationController selector:@selector(popViewControllerAnimated:)];
self.navigationItem.hidesBackButton = YES;
這甚至不是必需的,設置leftBarButtonItem屬性將覆蓋後退按鈕 – Daniel
- 1. 如何從按鈕中刪除效果?
- 2. 對Passbook圖標的閃耀效果
- 3. 如何獲得UIButtonTypeInfoLight的閃耀效果?
- 4. 閃光或閃光效果的按鈕
- 5. 刪除UINavigationBar後退按鈕標題
- 6. 引導刪除按鈕,點擊效果
- 7. 使用Flex Builder刪除圖標中的「閃耀」
- 8. 我無法刪除Internet Explorer 9中按鈕的按下效果。
- 9. 從視圖中刪除UINavigationBar
- 10. 從LinearLayout中刪除按鈕
- 11. 從LinearLayout中刪除按鈕
- 12. 從UIScrollView中刪除按鈕
- 13. 模擬在懸停事件上執行的按鈕上的閃耀效果,但與其他事件
- 14. 如何從CSS按鈕動畫中刪除閃爍?
- 15. 刪除的UIImageView從按鈕
- 16. 刪除3D按下效果Internet Explorer按鈕
- 17. 如何刪除按下按鈕(推入)效果
- 18. OpenGL聚光燈從後面閃耀
- 19. 從舞臺閃光燈AS3刪除按鈕
- 20. 如何刪除菜單項(WPF)的「按鈕」效果
- 21. 如何刪除按鈕上的默認引導效果?
- 22. 刪除WPF按鈕的鼠標懸停效果
- 23. 刪除按鈕,從電網
- 24. 從插件刪除按鈕
- 25. 當我按下按鈕時從MySQL表中刪除行刪除
- 26. 如何從UIViewController中刪除UINavigationBar?
- 27. 按下按鈕的效果
- 28. 刪除UINavigationBar後退按鈕額外填充?
- 29. 內存警告消除UINavigationBar按鈕
- 30. 從文本框中刪除timepicker效果
@Rudy - 如果您認爲該問題與其他問題重複,請投票結束,而不是編輯問題的正文。 –
這是[iOS:如何用一種顏色替換搜索欄背景閃耀/發光]的可能副本(http://stackoverflow.com/questions/6696200/ios-how-to-replace-search-bar-background-閃耀着單色) –