2017-10-17 122 views
0

它不会出现可能限制添加到UITableView无法约束的UITableView到安全区域

我想补充的“顶部对齐到:安全区”约束表视图,因为你可以看到,细胞不要似乎是安全区域内:

如何约束表视图留在安全区的范围之内?

+0

检查'automaticallyAdjustsScrollViewInsets'和'tableView.contentInsetA djustmentBehavior' peperties。前者应设置为TRUE;后者比'.never'以外的东西。 –

+0

,你需要一个版本检查'如果#available(iOS的11.0,*){tableView.contentInsetAdjustmentBehavior = .scrollableAxes}' –

回答

1

似乎你使用UITableViewController。这样你就没有办法改变tableView的默认约束。只有一件事可以做,就是添加一个导航控制器。 (点击你的UITableViewController比在你的显示器顶部:编辑器 - >嵌入 - >导航控制器)

另一种方法是将UITableView添加到UIViewController。在这种情况下,你可以添加任何你想要的约束。

请看下面的代码。这可以帮助你弄清楚接下来应该做什么。

import UIKit 

class ViewController: UIViewController,UITableViewDataSource, UITableViewDelegate { 

    var data: [String] = ["Cell 1", "Cell 2", "Cell"] 
    var colors: [UIColor] = [.red, .green, .blue] 

    @IBOutlet weak var tableView: UITableView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     tableView.delegate = self 
     tableView.dataSource = self 
     tableView.reloadData() 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "id", for: indexPath) 
     cell.textLabel?.text = data[indexPath.row] 
     cell.contentView.backgroundColor = colors[indexPath.row] 

     return cell 
    } 

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

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

} 

最后,您会在下面的截图中看到类似内容。

P.S.不要忘记给你的原型单元添加标识符。在我的例子标识符为“ID”

TableView

这里是所有的Xcode项目的屏幕截图。

Xcode project

0

我的解决办法是改变UITableViewController,到UIViewController,和一个UITableView添加到视图,然后添加顶部,底部,左,右限制。

注意,你会得到一个崩溃,直到您更改类从UIViewController继承,而不是UITableViewController

@interface MyViewController : UITableViewController 

@interface MyViewController : UIViewController 

你显然还需要重新链接的UITableView,和连接数据源和委托,如果你不这样做,编程