2017-09-21 44 views
4

使用斯威夫特3和Xcode 9我使用的是大UINavigationBar鑑於:iOS11如何同步大導航欄崩潰與滾動

if #available(iOS 11.0, *) { 

    navigationBar?.prefersLargeTitles = true 

    UINavigationBar.appearance().largeTitleTextAttributes = [ 
     NSForegroundColorAttributeName: Colors.black 
    ] 

} 

當我滾動UITableView,酒吧崩潰得太快,其創建多餘的空格:

前:

enter image description here

後:

enter image description here

當我觸摸UITableView酒吧崩潰。

的的tableView具有以下屬性:

let rect = CGRect(

    x: 0, 
    y: UIApplication.shared.statusBarView?.frame.height ?? 20, 
    width: UIScreen.main.bounds.width, 
    height: UIScreen.main.bounds.height 

) 

let tableView = UITableView(frame: rect) 

tableView的頂部邊界是self.navigationController?.navigationBar.frame.height ?? 44

另外,tableView設置爲:

if #available(iOS 11.0, *) { 
    self.contentInsetAdjustmentBehavior = .never 
} 

酒吧是半透明的,並且我希望保持這一點。我錯過了什麼?非常感謝幫助。

+0

嘗試在viewDidLoad self.automaticallyAdjustsScrollViewInsets = false –

+0

根據其他研究,這是折舊tableViews:'self.contentInsetAdjustmentBehavior = .never'。也沒有效果 –

+0

要清楚:我有以前,只是現在試圖設置'self.automaticallyAdjustsScrollViewInsets = false'。沒有不同。相同的行爲 –

回答

0

我得到它的工作使用:

if #available(iOS 11.0, *) { 
    self.contentInsetAdjustmentBehavior = .always 
} 

而且

let tableViewFrame = CGRect(
    x: 0, 
    y: UIApplication.shared.statusBarFrame.height ?? 20, 
    width: UIScreen.main.bounds.width, 
    height: UIScreen.main.bounds.height 
) 

並沒有進一步的TableView頂部插圖。

1

我不知道它對你有沒有用。但它的代碼正在爲我工​​作。

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating { 
    var numbers: [String] = [] 

    let rect = CGRect(
     x: 0, 
     y: UIApplication.shared.statusBarFrame.height ?? 20, 
     width: UIScreen.main.bounds.width, 
     height: UIScreen.main.bounds.height 
    ) 

    lazy var tableView: UITableView = { 
     let tV = UITableView() 
     tV.delegate = self 
     tV.dataSource = self 
     tV.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: "cell") 
     return tV 
    }() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     numbers = generateNumbers() 
     self.view.backgroundColor = UIColor.white 
     title = "Numbers" 

     self.navigationController?.navigationBar.prefersLargeTitles = true 

     let search = UISearchController(searchResultsController: nil) 
     search.hidesNavigationBarDuringPresentation = false 
     search.searchResultsUpdater = self 
     search.definesPresentationContext = true 
     self.navigationItem.searchController = search 
     tableView.frame = rect 

     self.view.addSubview(tableView) 
    } 

    func generateNumbers() -> [String] { 
     var numbers = [String]() 
     for var i in 1..<100 { 
      numbers.append(String(i)) 
     } 
     return numbers 
    } 

    func numberOfSections(in tableView: UITableView) -> Int { 
     return 1 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return numbers.count 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) 
     cell.textLabel?.text = self.numbers[indexPath.row] 
     return cell 
    } 

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
     tableView.deselectRow(at: indexPath, animated: true) 
    } 

    func updateSearchResults(for searchController: UISearchController) { 
     if let text = searchController.searchBar.text, !text.isEmpty { 
      numbers = self.numbers.filter({ (number) -> Bool in 
       return number.contains(text) 
      }) 
     } else { 
      numbers = generateNumbers() 
     } 
     self.table.reloadData() 
    } 
} 
+0

試圖使用你的實現,並與我的比較,我想出了,那'tableView.contentInsetAdjustmentBehavior'是造成這個問題。離開它有助於tableview行爲正常,但它凍結我的應用程序,每當我嘗試瀏覽應用程序... –

+1

我懂了它的工作!將0的矩形放在0上,並在.always上使用「tableView.contentInsetAdjustmentBehavior」 –