2015-06-13 48 views
4

我有一個導航控制器內的UITableViewController,帶有搜索欄。這是我加入viewDidLoad中搜索欄:如何修復表格視圖的搜索欄與狀態欄重疊

let resultsController = SearchTableViewController() 
resultsController.people = people 
searchController = UISearchController(searchResultsController: resultsController) 
let searchBar = searchController.searchBar 
searchBar.placeholder = "Search a person" 
searchBar.sizeToFit() 
tableView.tableHeaderView = searchBar 
searchController.searchResultsUpdater = resultsController 

這是結果:

enter image description here

我試圖編輯在故事板表視圖中添加約束,以進一步使它頂視圖的邊距,但我不能添加約束,可能是因爲表視圖是在UITableViewController中。

+0

最佳解決方案。創建新的uiview,離開頂部20像素並在該視圖中添加uisearchbar,並將整個視圖指定爲tableheaderview,您的問題將得到解決。 –

+1

我不覺得你已經在uinavigationcontroller中嵌入了你的uitableviewcontroller。從故事板可以通過選擇視圖控制器並轉到編輯器>嵌入>導航控制器 – agy

+0

它已經在導航控制器中,我可以從故事板文件中清楚地看到它:https://onedrive.live.com/redir?resid = 768cf89e7cf13325!4438&authkey =!AKP0Wbsec7-Zo-g&v = 3&ithint = photo%2cpng –

回答

7

的答案,我認爲你需要這個代碼。

在你viewDidLoad方法添加以下代碼:

self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0) 

和你的tableview將是這樣的:

enter image description here

編輯:

可以forcely滾動表與此代碼:

tableView.scrollToRowAtIndexPath(NSIndexPath(index: 0), atScrollPosition: UITableViewScrollPosition.Top, animated: false) 
+0

這種方式當我啓動應用程序時,我仍然可以看到表格視圖和狀態欄重疊,但是如果我滾動它,它會被修復。觀看此視頻:https://onedrive.live.com/redir?resid=768cf89e7cf13325!4440&authkey=!ANbvmk8nT8NLO5I&ithint=video%2cmov –

+1

@RamyAlZuhouri嘗試添加tableView.scrollToRowAtIndexPath(NSIndexPath(index:0),atScrollPosition:UITableViewScrollPosition.Top,動畫:假) –

1

我明白你在迅速編寫代碼,但是這是你將如何隱藏狀態欄在的ObjectiveC

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 

    if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) { 
     // iOS 7 
     [self prefersStatusBarHidden]; 
     [self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)]; 
    } else { 
     // iOS 6 
     [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide]; 
    } 
} 

// Add this Method 
- (BOOL)prefersStatusBarHidden 
{ 
    return YES; 
} 

下面是迅速How do I hide the status bar in a Swift iOS app?

0

儘量把下面的代碼在didFinishLaunching的AppDelegate

斯威夫特 -

var version = (UIDevice.currentDevice().systemVersion as NSString).floatValue 

    if (version >= 7) { 
     application.setStatusBarStyle( UIStatusBarStyle.LightContent, animated: true); 
     window?.clipsToBounds = true; 
     window?.frame = CGRectMake(0,20,self.window!.frame.size.width,self.window!.frame.size.height-20); 
    } 

在Objective-C -

if (UIDevice.currentDevice.systemVersion] floatValue] >= 7) { 
     [application setStatusBarStyle:UIStatusBarStyleLightContent]; 
     self.window.clipsToBounds =YES; 
     self.window.frame = CGRectMake(0,20,self.window.frame.size.width,self.window.frame.size.height-20); 
    } 

,並添加下面的方法在你的的ViewController講座

的Objective-C-

-(UIStatusBarStyle)preferredStatusBarStyle{ 
    return UIStatusBarStyleLightContent; 
} 

swift-

override func preferredStatusBarStyle() -> UIStatusBarStyle { 
    return UIStatusBarStyle.LightContent 
} 
+0

它有點作品,但現在有一個黑色的差距,而不是狀態欄:https://onedrive.live.com/redir?resid=768cf89e7cf13325!4439&authkey=!AI6RsI-Nc4DSBwY&v=3&ithint=photo%2cpng –

+0

Hi @RamyAlZuhouri,請嘗試編輯代碼。它一定會奏效。 – NehaK

0

添加UISearchControllerDelegate;

然後

-(void)willDismissSearchController:(UISearchController *)searchController{ 
_tableView.contentInset = UIEdgeInsetsMake(20, 0, 44, 0);}