【问题标题】:UITextView inside PrototypeCell have different behavior in two iPhone simulatorsPrototypeCell 中的 UITextView 在两个 iPhone 模拟器中具有不同的行为
【发布时间】:2018-10-18 16:58:49
【问题描述】:

我有一个UITableView,有两个单元格用于用户输入信息。

第一个单元格:“取消”和“完成”两个按钮。 第二个单元格:UITextView 用于用户类型信息。

这在 iPhone 7、6s 中运行良好……但在 iPhone SE 和 5C 中屏幕不同。我使用了自动布局,在情节提要中看起来效果很好。 详细信息:我需要将 Height Constraint 设置为 TextView 才能工作。

如下图所示:

故事板:

TableView 的约束:

视图控制器:

class LiberarViewController : UIViewController, UITableViewDelegate, UITableViewDataSource, UITextViewDelegate {

    @IBOutlet weak var tableViewForm: UITableView!

    var callback : ((String)-> ())?

    override func viewDidLoad() {
        tableViewForm.dataSource = self
        tableViewForm.delegate = self
//        tableViewForm.tableFooterView = UIView()        
    }

    @objc func cancelar(_ sender: Any) {
        self.dismiss(animated: true, completion: {})
    }

    @objc func finalizar(_ sender: Any) {
        self.dismiss(animated: true, completion: {})
    }

    func textViewDidEndEditing(_ textView: UITextView) {
        print("End edigint: \(textView.text!)")
    }

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

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        if section == 1 {
            return "Digite a justificativa: "
        }
        return nil
    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        switch indexPath.section {
            case 0:
            let cell = tableView.dequeueReusableCell(withIdentifier: "CellBotoesLiberar") as! CellBotoesLiberar
            cell.selectionStyle = .none
            cell.btCancelar.addTarget(self, action: #selector(cancelar), for: .touchUpInside)
            cell.btFinalizar.addTarget(self, action: #selector(finalizar), for: .touchUpInside)
            return cell

        case 1:
            let cell = tableView.dequeueReusableCell(withIdentifier: "CellJustificativaLiberar") as! CellJustificativaLiberar
            cell.txtViewJustificativa.delegate = self
            return cell

            default: return UITableViewCell()
        }
    }
}

class CellJustificativaLiberar : UITableViewCell {
    @IBOutlet weak var txtViewJustificativa: UITextView!
}

class CellBotoesLiberar : UITableViewCell {
    @IBOutlet weak var btFinalizar: UIButton!
    @IBOutlet weak var btCancelar: UIButton!
}

【问题讨论】:

  • 你为 TableView 设置了什么约束?
  • 我已经编辑了.....

标签: ios swift xcode uitableview autolayout


【解决方案1】:

您的 TableView 对安全区域的最高约束导致了问题。添加以下约束:

  1. 水平和垂直居中到超级视图。
  2. 修复高度并修复宽度/前导和尾随到 Superview。

希望这对你有用。

【讨论】:

  • 这行得通,你可以向我解释为什么顶部约束是一个问题?而且,如果我想自动扩展视图的高度给用户放置长文本?
  • @Augusto 您已将水平居中约束设置为 tableview 并设置顶部 AutoLayout 引擎将首先尝试将您的 tableview 居中而不是保持它的高度。这导致了问题。
  • @Augusto 对于可扩展的TextView高度,您需要设置textView.textContainer.heightTracksTextView = true textView.isScrollEnabled = false,并且需要将UITextView的高度约束优先级设置为Low。
  • 如果我需要放置一个大视图的新单元格,我需要更改高度约束吗?
  • 是的,如果你想要新的单元格大视图应该在屏幕上显示而不滚动。
猜你喜欢
  • 1970-01-01
  • 2012-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-23
  • 1970-01-01
  • 2018-03-09
相关资源
最近更新 更多