16

我正在構建僅適用於iOS 7的應用程序。我正在嘗試在導航欄中設置UISearchDisplayController。嵌入導航欄時iOS 7中奇怪的UISearchDisplayController視圖偏移行爲

我的設置是這樣的:在故事板中,我在視圖控制器的視圖中添加了一個「搜索欄和搜索顯示控制器」,並將其設置爲(0,0)相對於頂部佈局指南。我將約束設置爲左,上和右。 (我玩的約束,我把它們完全刪除,沒關係)。最重要的是,我有我的表格視圖。當我將搜索欄添加到故事板中的視圖時,它會自動爲searchDisplayController和searchBar委託設置網點。代碼我有self.searchDisplayController.displaysSearchBarInNavigationBar = YES;我有兩個問題:

1)沒有任何按鈕顯示搜索欄(界面生成器 - >選擇搜索欄 - >選項:沒有選擇)搜索欄在屏幕中間:

enter image description here

如果我點擊導航欄上,它開始編輯的搜索欄:

enter image description here

還要注意的是黑暗覆蓋APPE ars從導航欄中偏移。在我看來,空間與導航欄的高度相同。就像它一直向下移動那麼多。此外,當它顯示搜索結果時,內容視圖的頂部向下移動的量相同(後面會有更多圖片),這會導致我遇到第二個問題。

2)我搞砸了一會兒,並決定檢查選項,讓它顯示取消按鈕。現在我有嵌入在正確的導航欄的搜索欄,但覆蓋仍然下移:

enter image description here

同樣,當搜索結果表視圖顯示,它是由相同的量(通知下移右側的滾動條):

enter image description here

更奇怪的是,我設置了搜索顯示控制器的實現代碼如下層上的邊界,它似乎正確:

enter image description here

我從來沒有使用過UISearchDisplayController,但我不熟悉如何設置它,但在功能上它工作正常。我已經閱讀了一些其他類似的帖子,但唯一的建議是通過調整幀和設置手動偏移來修補它。我更想知道是什麼造成了這個問題,這是一個錯誤嗎?我做錯了什麼?如果這是一個錯誤,我可以等待修復。這似乎是一個基本的事情,一千個人必須完成沒有任何問題,所以我覺得我不能正確設置它。感謝您的意見。

回答

28

我記得遇到同樣的問題,你正在觀察。可能有幾個解決方案,你可以嘗試。

  • 如果您使用的故事板 您應該單擊您已設置了您的tableview視圖控制器或TableView中控制器上,並去其屬性檢查器,然後在視圖控制器部分查找並設置擴展邊緣部分在頂部酒吧。

  • 如果你不使用故事板你可以使用viewcontrollers edgesForExtendedLayout屬性手動設置設置,並應該做的伎倆。我正在使用故事板。

+0

謝謝,工作。我仍然希望我能弄清楚爲什麼沒有取消按鈕,搜索欄不起作用,但這已足夠接近 – d370urn3ur

+0

我沒有取消按鈕就可以工作,當您不添加取消按鈕時發生了什麼。 – hackamanshu

+0

如果你看看我的問題中的第一張和第二張照片,你可以看到它。基本上,使用取消按鈕,搜索欄會正確嵌入導航欄中。沒有取消按鈕,搜索欄被移動到屏幕的中心,在表格視圖頂部 – d370urn3ur

15

在我的情況下,使用故事板,我不得不同時檢查在最熱門酒吧在不透明酒吧離開選中下底杆。

+4

不得不檢查在不透明酒吧下讓它爲我工作 –

3

在我的情況下,我實際上不得不在我的故事板中取消選中所有的擴展邊框(基本上與編程設置Extended邊緣爲UIRectEdgeNone相同),以阻止我的搜索欄抵消自身。感謝你們!

+0

同樣在這裏...奇 – Kris

+0

同樣在這裏...在2017 – Storix

0

不幸的是,上述解決方案都不適用於我,我正在使用UITableViewController

此鏈接幫助:

http://petersteinberger.com/blog/2013/fixing-uisearchdisplaycontroller-on-ios-7/

我把下面的代碼爲方便起見:

static UIView *PSPDFViewWithSuffix(UIView *view, NSString *classNameSuffix) { 
    if (!view || classNameSuffix.length == 0) return nil; 

    UIView *theView = nil; 
    for (__unsafe_unretained UIView *subview in view.subviews) { 
     if ([NSStringFromClass(subview.class) hasSuffix:classNameSuffix]) { 
      return subview; 
     }else { 
      if ((theView = PSPDFViewWithSuffix(subview, classNameSuffix))) break; 
     } 
    } 
    return theView; 
} 

- (void)correctSearchDisplayFrames { 
    // Update search bar frame. 
    CGRect superviewFrame = self.searchDisplayController.searchBar.superview.frame; 
    superviewFrame.origin.y = 0.f; 
    self.searchDisplayController.searchBar.superview.frame = superviewFrame; 

    // Strech dimming view. 
    UIView *dimmingView = PSPDFViewWithSuffix(self.view, @"DimmingView"); 
    if (dimmingView) { 
     CGRect dimmingFrame = dimmingView.superview.frame; 
     dimmingFrame.origin.y = self.searchDisplayController.searchBar.frame.size.height; 
     dimmingFrame.size.height = self.view.frame.size.height - dimmingFrame.origin.y; 
     dimmingView.superview.frame = dimmingFrame; 
    } 
} 

- (void)setAllViewsExceptSearchHidden:(BOOL)hidden animated:(BOOL)animated { 
    [UIView animateWithDuration:animated ? 0.25f : 0.f animations:^{ 
     for (UIView *view in self.tableView.subviews) { 
      if (view != self.searchDisplayController.searchResultsTableView && 
       view != self.searchDisplayController.searchBar) { 
       view.alpha = hidden ? 0.f : 1.f; 
      } 
     } 
    }]; 
} 

// This fixes UISearchBarController on iOS 7. rdar://14800556 
- (void)correctFramesForSearchDisplayControllerBeginSearch:(BOOL)beginSearch { 
    if (PSPDFIsUIKitFlatMode()) { 
     [self.navigationController setNavigationBarHidden:beginSearch animated:YES]; 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      [self correctSearchDisplayFrames]; 
     }); 
     [self setAllViewsExceptSearchHidden:beginSearch animated:YES]; 
     [UIView animateWithDuration:0.25f animations:^{ 
      self.searchDisplayController.searchResultsTableView.alpha = beginSearch ? 1.f : 0.f; 
     }]; 
    } 
} 

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller { 
    [self correctFramesForSearchDisplayControllerBeginSearch:YES]; 
} 

- (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller { 
    [self correctSearchDisplayFrames]; 
} 

- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller { 
    [self correctFramesForSearchDisplayControllerBeginSearch:NO]; 
} 

- (void)searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView { 
    // HACK: iOS 7 requires a cruel workaround to show the search table view. 
    if (PSPDFIsUIKitFlatMode()) { 
     controller.searchResultsTableView.contentInset = UIEdgeInsetsMake(self.searchDisplayController.searchBar.frame.size.height, 0.f, 0.f, 0.f); 
    } 
} 
1

我有同樣的問題。我通過在tableview下添加視圖對象來解決這個問題。

  1. 在故事板
  2. 將TableView中新的VC
  3. 將表格單元格添加新的ViewController到的TableView
  4. 作出的TableView的數據源,TableView中代表新的VC
一個連接
0

我發生了非常類似的行爲。對我而言,解決方案是在父視圖控制器的故事板設置中取消選中「頂部邊欄下的延伸邊緣」(我已關閉透明導航欄,不確定是否會影響任何內容)。如果你不使用故事板,你必須設置[UIViewController edgesForExtendedLayout]。

從Apple文檔:

該屬性僅適用於查看嵌入在容器中,例如的UINavigationController或的UITabBarController控制器。視圖控制器設置爲根視圖控制器不會對此屬性做出反應。默認值是UIRectEdgeAll。

1

definesPresentationContext =真

override func viewDidLoad() { 
     super.viewDidLoad() 

     searchController = UISearchController(searchResultsController: nil) 
     searchController.searchResultsUpdater = self 
     searchController.hidesNavigationBarDuringPresentation = false 

     searchController.dimsBackgroundDuringPresentation = true 
     searchController.searchBar.searchBarStyle = UISearchBarStyle.Prominent 
     self.tableView.tableHeaderView = searchController.searchBar 

     definesPresentationContext = true 

或看到UISearchBar presented by UISearchController in table header view animates too far when active

0

我的問題只是調整滾動視圖插入。更改爲虛假我沒有問題