2011-11-22 25 views
1

我想爲我的UINavigationBar上的按鈕設置一個自定義顏色,但只有當一個特定的UIViewController顯示在其中(我知道這是可疑的UI設計,但它是一個特定的客戶需求)。如何設置自定義的UINavigationBarButton顏色

我們當前的實現使用Swizzle覆蓋所有UINavigationBar對象的drawRect。這是必要的,所以我們的自定義導航欄顏色適用於標準iOS屏幕(例如通過標準API添加新聯繫人時)。

我想要改變一個特定的UIViewController的導航欄的色調與其他的不同,但是Swizzle總是優先。

我在iOS 4.3模擬器中測試。我們支持iOS 3.x以上的客戶端(iPhone,iPod和iPad),所以我不能只使用較新的iOS 5 API來定製它。

其他的事情我已經試過,沒有成功:

  • 添加setStyle電話(按this workaround)再次使按鈕更新。

  • 我試過UINavigationButton黑客我發現here(我知道這是一個私人API,並冒着蘋果拒絕該應用的風險)。我也嘗試將該代碼放入viewWillAppear

+0

「我們支持的iOS 3.x客戶端」 –

回答

1

我認爲this question的答案會回答你的問題。我的答案使用UISegmentedControl,不需要任何私人API調用。

+0

僅供參考,我更新的問題,因爲我發現了問題的根源是作爲一個調酒在我們的代碼其他地方完成使用UISegmentedControl可能仍然是一個合適的解決方法,所以我會看看它。 –

+0

如果可能的話,您不應該在運輸應用程序中使用swizzling。 – titaniumdecoy

+0

我同意,但遺憾的是,這是我必須支持的現有代碼:-( –

1

我通過創建一個帶有自定義背景圖像的UIButton按照this Stackoverflow answer得到了這個工作。

在情況下,它可以幫助任何人,這裏是我的代碼:

#import <QuartzCore/QuartzCore.h> 

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
[button setBackgroundImage:[UIImage imageNamed:@"mybackground.png"] forState:UIControlStateNormal]; 
[button setTitle:NSLocalizedString(@"Login", @"Button (9 chars limit) - Login") forState:UIControlStateNormal]; 
button.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:12.0f]; 
[button.layer setCornerRadius:5.0f]; 
[button.layer setMasksToBounds:YES]; 
[button.layer setBorderWidth:1.0f]; 
[button.layer setBorderColor: [[UIColor grayColor] CGColor]]; 
button.frame=CGRectMake(0.0, 100.0, 60.0, 30.0); 
[button addTarget:self action:@selector(myLoginActionMethod:) forControlEvents:UIControlEventTouchUpInside]; 

self._loginButton= [[UIBarButtonItem alloc] initWithCustomView:button]; 

在一般情況下,我認爲這是最好的設置色調在UIViewController中像這樣的(雖然我的情況,這並不因工作在drawRect調酒在我的代碼在UINavigationBar在到位):

self.navigationController.navigationBar.tintColor=[UIColor blueColor]; 
相關問題