2011-05-05 35 views
3

我想顯示像iBooks中的那樣的tableview搜索欄。我如何減少搜索欄的寬度以及如何在沒有任何背景顏色的情況下顯示它。如何iBooks喜歡在桌上的搜索UI

另外,如何在頁面顯示時最初隱藏搜索框。

enter image description here

回答

6

我能想到的兩個選項:

通過繼承的UITextField創建自己的

  • 組邊框風格UITextBorderStyleNone
  • 設置leftView放大鏡圖像
  • 將背景設置爲透明PNG與只顯示如果uiimages stretchableImageWithLeftCapWidth
  • 利用輪邊界:這樣背景圖像將看起來不錯topCapHeight方法時的UISearchBar拉伸

勾搭,不建議

您可以訪問

for (UIView *aSubview in [searchbar subviews]) { 
      // this will remove the toolbar around the searchbar in iOS 4 
    if ([aSubview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) 
    { 
     [aSubview removeFromSuperview]; 
    }  

    // get a grip on the underlying textfield... 
    if ([aSubview isKindOfClass:NSClassFromString(@"UISearchBarTextField")]) 
    { 
     UITextField *aTextfield = (UITextField *)aSubview; 

     // this won't do anything at it is already none, just experimenting 
     aTextfield.borderStyle = UITextBorderStyleNone; 

     // this will remove the whole border as it is a custom background 
     aTextfield.background = nil; 

     // this will remove the magnifying glass 
     aTextfield.leftView = nil; 

     // play around even more :) 
    }  
} 

您可以通過

隱藏搜索欄:在這樣一個UISearchBar子視圖
  • 隱藏屬性設置爲YES
  • 設置可見區域以外的幀或中心屬性某處
  • 組alpha屬性爲0.0
  • 僅在需要時將所述搜索欄到視圖
+0

謝謝尼克。這對我幫助很大。 我不想直接隱藏搜索欄。相反,它應該位於表格視圖的頂部,並且只有在表格向下滾動時才顯示。 – 2011-05-07 05:54:30

+0

我認爲你可以通過將搜索欄放在tableview scrollviews的內容區域,然後使用contentInset來初始化它來達到這種效果。 – 2011-05-07 08:46:34