2013-04-29 33 views
2

當用戶點擊搜索文本字段時,我想將鍵盤的顏色更改爲黑色。更改UISearchBar中的鍵盤顏色與外觀

我試圖用 UITextField *textField = [UITextField appearance]; [textField setKeyboardAppearance:UIKeyboardAppearanceAlert];

來實現它,但我的構建失敗,此消息

終止應用程序由於未捕獲的異常 'NSInvalidArgumentException',原因是:「 - [UISearchBarTextField _UIAppearance_setKeyboardAppearance:]:無法識別的選擇發送到實例0x8485260'

請你幫助我嗎? 非常感謝

回答

4

使用此代碼...

for(UIView *searchTextfield in yourSearchBar.subviews) 
    { 
     if([searchTextfield isKindOfClass: [UITextField class]]){ 
      [(UITextField *)searchTextfield setKeyboardAppearance: UIKeyboardAppearanceAlert]; 
     } 
    } 

它同樣喜歡我的另一個答案在我替換的按鈕圖片看.. image-for-cancel-button-of-uisearchbar

+1

謝謝你的回答,你讓我的一天:)。 – 2013-04-29 09:52:53

1

給予好評,以帕拉斯喬希的回答,和iOS7/8更新。 UIView現在被包裝在一個圖層中,所以您需要重複一次。

for(UIView *searchTextfield in self.searchBar.subviews) 
    { 
     for(UIView *searchTextfield2 in searchTextfield.subviews) { 
      if([searchTextfield2 isKindOfClass: [UITextField class]]){ 
       [(UITextField *)searchTextfield2 setKeyboardAppearance: UIKeyboardAppearanceDark]; 
      } 
     } 


    } 

標準免責聲明。這是「正式」被Apple所詬病的,但是因爲你只是使用public api來遍歷UIViews,所以你「在技術上」沒問題。

相關問題