当键盘出现时 UICollectionViewCell 改变高度 swift 4.2

UICollectionViewCell changes height when keyboard appears swift 4.2

我一直在开发一个应用程序,它有一个像主屏幕一样工作的 UICollectionView,而 UICollectionViewCells 充当不同的页面(水平滚动)。我在每个单元格上添加了一个文本字段进行编辑,但是当我单击文本字段时,出现键盘时单元格高度会扩展。当键盘隐藏时,单元格高度保持扩展。我一直在寻找这个问题的答案,但我还没有找到有效的解决方案。

我试过使布局无效,将translatesAutoresizingMaskIntoConstraints设置为false,并直接将内容偏移量设置为零。 None 这些选项已解决问题。

下面是我的代码:

private let reuseIdentifier = ["Cell1", "Cell2", "Cell3", "Cell4", "Cell5"]

let navi_btn_array: [UIButton] = [navi_home_btn, navi_gavel_btn, navi_orders_btn, navi_profile_btn, navi_lightning_btn]
var collectionView: UICollectionView = {
    var layout = UICollectionViewFlowLayout();
    layout.sectionInset = UIEdgeInsets.zero
    var cv = UICollectionView(frame: .zero, collectionViewLayout: layout);
    cv.autoresizesSubviews = false
    cv.contentInset = UIEdgeInsets.zero
    cv.translatesAutoresizingMaskIntoConstraints = false;
    return cv;
}();

class MainCVC: UICollectionViewController,UICollectionViewDelegateFlowLayout, CLLocationManagerDelegate, UITextFieldDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()



        collectionView?.register(SimpleDispensaryPage_Cell.self, forCellWithReuseIdentifier: reuseIdentifier[0])
        collectionView?.delegate = self
        collectionView?.dataSource = self
        collectionView?.isPagingEnabled = true

        let nc:NotificationCenter = NotificationCenter.default
        nc.addObserver(self, selector: #selector(keyboardDidShow(notification:)), name: NSNotification.Name.UIKeyboardDidShow, object: nil)
        nc.addObserver(self, selector: #selector(keyboardDidHide(notification:)), name: NSNotification.Name.UIKeyboardDidHide, object: nil)
    }

    @objc func keyboardDidShow(notification: Notification){

        collectionViewLayout.invalidateLayout()
        collectionView?.contentOffset.y = 0


    }
    @objc func keyboardDidHide(notification: Notification){

        collectionViewLayout.invalidateLayout()
        collectionView?.contentOffset.y = 0

    }



    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 5

    }
    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1

    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {


            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier[0], for: indexPath)
            cell.backgroundColor = Custom_Colors.color_pine.withAlphaComponent(0.5)
            var txt_fld = UITextField()
            txt_fld.frame = CGRect(x: 0, y: 50, width: 100, height: 50)
            cell_label.text = "Placeholder #"+String(indexPath.item)
            cell_label.textAlignment = .center
            cell.addSubview(cell_label)
            return cell
        }
    }

}

我在运行时也遇到了这个错误:

2019-03-14 11:41:20.770799-0700 app[57379:5390785] the item height must be less than the height of the UICollectionView minus the section insets top and bottom values, minus the content insets top and bottom values.
2019-03-14 11:41:20.771043-0700 app[57379:5390785] The relevant UICollectionViewFlowLayout instance is <UICollectionViewFlowLayout: 0x7fda15c13f40>, and it is attached to <UICollectionView: 0x7fda1706f000; frame = (0 0; 375 730.8); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x600000447bc0>; layer = <CALayer: 0x600000437720>; contentOffset: {8, -38.333333333333336}; contentSize: {1891, 579}; adjustedContentInset: {0, 0, 151.79999999999995, 0}> collection view layout: <UICollectionViewFlowLayout: 0x7fda15c13f40>.
2019-03-14 11:41:20.771245-0700 app[57379:5390785] Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.

你这里的设置有些奇怪。我建议进行一些更改:

1 - 永远不要在 collectionView(_, cellForItemAt ...) 中添加子视图。这将导致每次重复使用单元格时进行多次添加。子视图应由单元格添加(最好在创建时添加)。

2 - 删除 cv.autoresizesSubviews = falsecv.translatesAutoresizingMaskIntoConstraints = false.

3 - 如果您想要固定大小的单元格,您可以设置 layout.delegate = self 并实施以下方法:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  return CGSize(width: screenWidth, height: screenHeight)
}

4 - 如果您真的只想要几个页面,为什么不使用 UIPageViewController?