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

问题描述 投票:0回答:1

我一直在开发一个具有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.
ios swift uicollectionview uicollectionviewcell uicollectionviewlayout
1个回答
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?

© www.soinside.com 2019 - 2024. All rights reserved.