Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
2
Help us understand the problem. What is going on with this article?
@tetsuhama

iOS開発メモ

More than 1 year has passed since last update.

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()
}
2
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
tetsuhama
ツール系のアプリをつくってます。

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
2
Help us understand the problem. What is going on with this article?