当键盘出现快速4.2时,UICollectionViewCell更改高度

时间:2019-03-14 19:48:53

标签: ios swift uicollectionview uicollectionviewcell uicollectionviewlayout

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

我试图使布局无效,将translatesAutoresizingMaskIntoConstraints设置为false,然后将内容偏移量直接设置为零。这些选项都不能解决问题。

下面是我的代码:

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 个答案:

答案 0 :(得分:0)

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

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?

相关问题