2010-03-05 50 views
12

我的應用程序的表格視圖不佔用整個屏幕高度,因爲我在橫幅底部允許50px。iPhone SDK:設置UISearchDisplayController的表格視圖的大小

當我開始在搜索欄中輸入時,搜索結果表視圖變大;它會填充搜索欄和標籤欄之間的所有可用屏幕空間。這意味着最後的搜索結果被橫幅遮蓋。

如何指定UISearchDisplayController使用的表視圖的大小?沒有我能看到的界限或框架屬性。

編輯補充截圖:

這是表視圖是如何設置的IB。它比合成標籤欄短50px。

alt text http://iphone.lightwood.net/StackOverflow/gstableviewinib.png

這是怎樣的內容顯示正常。我在這裏滾動到最底部。 alt text http://iphone.lightwood.net/StackOverflow/gstableviewatbottom.png

這是搜索時顯示的方式。再次,我已經滾動到最底層。如果我禁用了橫幅廣告,我可以看到搜索顯示錶向下展開到標籤欄。

alt text http://iphone.lightwood.net/StackOverflow/gssearchviewatbottom.png

+0

另外值得注意的是,我已經嘗試了不同高度的表視圖,並且搜索顯示結果總是延伸到屏幕的底部。問題不在於搜索欄本身是〜50px,而是將所有內容都放在橫幅後面。 – 2010-03-05 23:02:53

回答

50

解決這個問題的關鍵是找出何時更改表視圖的幾何圖形。調用:

[self.searchDisplayController.searchResultsTableView setFrame:someframe]; 

創建UISearchDisplayController後是徒勞的。答案是這樣的委託方法:

-(void)searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView { 
    tableView.frame = someframe; 
} 

注意,我也嘗試過-searchDisplayController:didLoadSearchResultsTableView,但它在那裏做不好。您必須等待顯示才能調整其大小。

另請注意,如果您只是簡單地指定tableView.frame = otherTableView.frame,搜索結果表會覆蓋其相應的搜索欄,因此無法清除或取消搜索!

我的最終代碼看起來是這樣的:

-(void)searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView { 

    CGRect f = self.masterTableView.frame; // The tableView the search replaces 
    CGRect s = self.searchDisplayController.searchBar.frame; 
    CGRect newFrame = CGRectMake(f.origin.x, 
           f.origin.y + s.size.height, 
           f.size.width, 
           f.size.height - s.size.height); 

    tableView.frame = newFrame; 
} 
+0

感謝你的伴侶。這讓我瘋狂! – 2013-05-31 16:15:08

+0

如何聲明'someframe'? – user2872856 2017-03-14 01:33:44

+0

@ user2872856'tableView.frame'是一個CGRect,所以你可以這樣做:'CGRect someframe = CGRectMake(CGFloat x,CGFloat y,CGFloat width,CGFloat height);' – BigHeadCreations 2017-12-26 06:23:03

0

我不知道我完全理解你所描述的東西,它會是不錯的屏幕截圖。

這聽起來像是在發生的事情是UITableView是屏幕的大小和橫幅是重疊它的底部50像素。所有的UIView子項都有一個框架,邊界和中心屬性,它們從它們的普通UIView父項繼承。

@interface UIView(UIViewGeometry) 

// animatable. do not use frame if view is transformed since it will not correctly reflect the actual location of the view. use bounds + center instead. 
@property(nonatomic) CGRect   frame; 

// use bounds/center and not frame if non-identity transform. if bounds dimension is odd, center may be have fractional part 
@property(nonatomic) CGRect   bounds;  // default bounds is zero origin, frame size. animatable 
@property(nonatomic) CGPoint   center;  // center is center of frame. animatable 
@property(nonatomic) CGAffineTransform transform; // default is CGAffineTransformIdentity. animatable 
// ... from UIView.h 

只要你喜歡你可以操縱這些屬性,這聽起來像你只需要調整的範圍要小於屏幕50個像素,並做一些數學計算您的新中心。

+0

我添加了一些截圖。我無法爲搜索表格顯示設置幾何圖形。如果我可以找出在哪裏做的,這可能是答案......但是UISearchDisplayController的searchResultsTableView屬性是隻讀的。 – 2010-03-05 22:59:13

+0

searchResultsTableView屬性是隻讀的,但這只是意味着您無法更改控制器上的句柄,您應該可以操縱tableview本身。 – slf 2010-03-06 02:25:47

+0

http://developer.apple.com/iphone/library/samplecode/TableSearch/index.html – slf 2010-03-06 02:26:22

1

我更新的代碼,以允許更深的視圖層次結構,但半透明蓋視圖的初始幀仍然佔據在搜索欄下方的整個窗。

-(void)searchDisplayController: (UISearchDisplayController*)controller 
didShowSearchResultsTableView: (UITableView*)tableView 
{ 
    if ([controller.searchBar.superview isKindOfClass: [UITableView class]]) 
    { 
     UITableView* staticTableView = (UITableView*)controller.searchBar.superview; 

     CGRect f = [tableView.superview convertRect: staticTableView.frame fromView: staticTableView.superview]; 
     CGRect s = controller.searchBar.frame; 
     CGRect newFrame = CGRectMake(f.origin.x, 
            f.origin.y + s.size.height, 
            f.size.width, 
            f.size.height - s.size.height); 

     tableView.frame = newFrame; 
    } 
} 
相關問題