1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

iOS開発メモ

Last updated at Posted at 2020-02-15

UICollectionView In UITableViewCell の高さ調整メモ

collectionView.isScrollEnabled = false にしておく。

    // viewDidLayoutSubviewsの初回判定用のフラグ
    var isFirstLayoutSubviews = true

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        guard isFirstLayoutSubviews else { return }
        // ここで調整
        collectionView.reloadData()
        tableView.reloadData()
        isFirstLayoutSubviews = false
    }

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        // TableViewCellの高さをCollectionViewのContentSizeの高さにする
        return collectionView.collectionViewLayout.collectionViewContentSize.height
    }

UITextView in UITableViewCel の高さ変更メモ

class ViewController: UITextViewDelegate {
    
    var currentKeyboardHeight: CGFloat = 0

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        addObservers(showSelector: #selector(showKeyboard), hideSelector: #selector(hideKeyboard))
    }

    @objc func showKeyboard(_ notification: Notification) {
        currentKeyboardHeight = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue.height ?? 0
    }

    func textViewDidChange(_ textView: UITextView) {
        // TextViewの中身によってcellの高さを必要に応じて変えている
        let oldHeight = textView.bounds.height
        let newHeight = textView.sizeThatFits(CGSize(width: textView.bounds.width, height: CGFloat.greatestFiniteMagnitude)).height
        
        if oldHeight != newHeight {
            let cellRect = tableView.rectForRow(at: IndexPath(row: 4, section: 0))
            let memoTextViewTopLine: CGFloat = tableView.convert(cellRect, to: self.view).minY
            let newBottomLine = memoTextViewTopLine + newHeight
            let keyboardTopLine: CGFloat = view.bounds.height - currentKeyboardHeight
            
            if newBottomLine >= keyboardTopLine {
                // キーボードに隠れる高さになったらスクロールするようにして回避
                textView.isScrollEnabled = true
            } else if textView.isScrollEnabled {
                // テキストを削除して、隠れない高さになった直後は高さがずれるのでtableView.beginUpdates()とかしない
                textView.isScrollEnabled = false
            } else {
                // それ以外はスクロールを無効化してセルの高さを変更する
                textView.isScrollEnabled = false
                tableView.beginUpdates()
                tableView.endUpdates()
            }
        }
    }
    
    func textViewDidEndEditing(_ textView: UITextView) {
        tableView.contentOffset.y = 0
    }
}

編集時に初期値いれて、中身に応じて高さを合わせるときは、viewDidLoad()の中でこれでいけた。
tableView.performBatchUpdates(nil, completion: nil)を呼ぶと表示が崩れた。

if let memo = edit?.memo {
    memoTextView.text = memo
    memoTextView.layoutIfNeeded()
}
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?