2017-09-21 47 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” –