2013-10-03 40 views
7

不知道爲什麼,我的iOS 7搜索欄留下了一個合適的空間。在iOS 6中沒關係。iOS 7 UISearchBar右間距

我知道它與段索引有關,因爲如果我刪除它,空間消失,但​​我不知道如何解決它。有什麼想法嗎?

enter image description here

+0

是第一個tableviewcell的Searchbar子視圖嗎? –

+0

不,是UITableView – Odrakir

+0

的子視圖,請向我展示您將searchbar添加爲tableview的子視圖的代碼! –

回答

6

創建UISearchBar,直到出現一個更好的答案,我只是手動更改搜索欄這樣的框架:

- (void)viewDidLayoutSubviews { 
    [super viewDidLayoutSubviews];   

    CGRect barFrame = self.searchBar.frame; 
    barFrame.size.width = self.view.bounds.size.width; 
    self.searchBar.frame = barFrame; 

} 
+0

我在下面發佈了正確的答案,如果有幫助 – MahmoudA

+0

這很好用。謝謝! – OlivaresF

+0

我有問題一樣的問題。你的代碼幫助,但它的一點點..比我滾動(在調用viewDidLayoutSubviews方法)的搜索欄寬度= 320和文本文件在它~300。但是,如果我點擊textfiled,它會集中和點擊取消 - searcbar寬度變成320並在其中textfiled將約290.解決它,我們應該多1次的時間更改SearchBr框架manualy –

0

我也是在我的應用程序連接一個UISearcBar,並沒有什麼不對,甚至有我的應用程序支持旋轉也。

你能嘗試刪除並重新在storyboard/xib

+0

同樣的事情發生。您是否像我一樣在表視圖右側有段索引? – Odrakir

+0

不,我不。嘗試在'autolayout'中設置'SearchBar''width constraints'。 –

1

因爲表視圖始終葉15px右側的索引視圖部分,因此您應該在重新加載表格視圖後調整大小的海德吧

第一張:

self.tblData.sectionIndexBackgroundColor = [UIColor clearColor]; //(iOS >= 7 only) 

作弊時間:

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate 
{ 

    [self performSelector:@selector(resizeSearchBar) withObject:nil afterDelay:0.01]; 
} 
- (void) resizeSearchBar 
{ 
    CGRect frame = self.searchBar.frame; 
    if (frame.size.width < self.tblData.frame.size.width) { 
     frame.size.width = self.tblData.frame.size.width; 
    } 
    self.searchBar.frame = frame; 
} 
- (void) reloadTableData // call it anytime you want to reload table view 
{ 
    [self.tblData reloadData]; 
    [self performSelector:@selector(resizeSearchBar) withObject:nil afterDelay:0.01]; 
} 

推薦 不要騙我一樣,只是做了簡單的方法:

self.searchBar.searchBarStyle = UISearchBarStyleMinimal; // iOS >= 7 only 
+0

我不認爲你需要每次滾動表格視圖時調整搜索欄的大小。 – Odrakir

+0

每次表格視圖滾動時,它都會改變你的搜索欄的大小。 –

+0

它不適合我。 – Odrakir

7

嵌入您在一個UIView,然後添加的UISearchBar作爲tableHeaderView。在故事板中以這種方式構建它爲我工作。我猜iOS調整了tableHeaderView中的UISearchBar,但留下了一個基本的UIView(並且不費心去查看它)。

您可能還需要使部分指標透明,我做到了與:

[[UITableView appearance] setSectionIndexBackgroundColor:[UIColor clearColor]]; 
[[UITableView appearance] setSectionIndexTrackingBackgroundColor:[UIColor clearColor]]; 
+0

完美編輯:此代碼僅適用於iOS 7 – user2159978

0

我添加爲頂級視圖,而不是表視圖的一個子視圖搜索欄。使用自動佈局將搜索欄固定到頂部指南,並在搜索欄和表格視圖之間使用垂直空間約束0。

2

enter image description here

添加搜索欄裏面一個UIView把儘可能的tableView的頭視圖。將桌面視圖的sectionIndexBackgroundColor設置爲清除顏色,因爲它覆蓋標題。

使用iOS 7,7.1測試;

0

方法viewDidLayoutSubviews接受的解決方案使屏幕閃爍。 相反,我所做的就是創建UISearchBar一個子類,僅僅做到這一點:

FullWidthSearchBar.h:

@interface FullWidthSearchBar : UISearchBar 
@end 

FullWidthSearchBar。L:

#import "FullWidthSearchBar.h" 
@implementation FullWidthSearchBar 

- (void)setFrame:(CGRect)frame { 
    frame.size.width = self.superview.bounds.size.width; 
    [super setFrame:frame]; 
} 

@end 

然後我分配一個類來對我的廈門國際銀行在搜索欄:

enter image description here

4

我使用SearchDisplayController時有同樣的問題與iPhone 6/6Plus。 (使用Swift)

我試圖設置搜索欄的框架,但沒有運氣,但我注意到,如果我點擊UISearchBar的textField,然後取消它,那麼它會採取適當的視圖大小。因此,我設法通過使用搜索在viewController的ViewDidLoad中調用以下代碼來解決該問題。

self.searchController.setActive(true, animated: false) 
self.searchController.setActive(false, animated: false) 
0

問題是正確的白色塊,所以如果我們改變塊顏色一樣的搜索欄背景,它看起來很正常。

只是

if (IOS7) { 
    self.tableview.backgroundColor = [UIColor colorWithPatternImage:self.searchBar.backgroundImage]; 
} 
3
self.contactsTableView.sectionIndexBackgroundColor = [UIColor clearColor]; 

的原因在於白邊是因爲索引層有一個白色背景,並在搜索欄的頂部。這應該足夠了。