Swift 点击textfield并显示键盘时,视图向上移动,即使没有向上移动视图的代码

Swift 点击textfield并显示键盘时,视图向上移动,即使没有向上移动视图的代码,swift,uicollectionview,uitextfield,Swift,Uicollectionview,Uitextfield,这是一个我一直无法解决的非常奇怪的问题。 我将此UICollectionViewCell命名为DownloadUrlCell,其某些属性如下: class DownloadUrlCell: UICollectionViewCell { override init(frame: CGRect) { super.init(frame: frame) self.backgroundColor = .gray setupCellView()

这是一个我一直无法解决的非常奇怪的问题。 我将此UICollectionViewCell命名为DownloadUrlCell,其某些属性如下:

class DownloadUrlCell: UICollectionViewCell {

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = .gray
        setupCellView()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


    //MARK: properties
    let container: UIView = {
        let vi = UIView()
        vi.translatesAutoresizingMaskIntoConstraints = false
        vi.backgroundColor = .white
        vi.layer.cornerRadius = 5.0
        vi.addShadowToSelf()
        return vi
    }()

    let urlInputTF: UITextField = {
        let tf = UITextField()
        tf.translatesAutoresizingMaskIntoConstraints = false
        tf.borderStyle = .roundedRect
        tf.textColor = .black
        tf.font = UIFont.systemFont(ofSize: 13)
        return tf
    }()

    //MARK: setup methods
    func setupCellView() {
        setupContainer()
        addUrlTFAndBtn()
    }

    func setupContainer() {
        self.addSubview(container)

        container.topAnchor.constraint(equalTo: self.topAnchor, constant: 10).isActive = true
        container.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -10).isActive = true
        container.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 5).isActive = true
        container.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -5).isActive = true
    }

    func addUrlTFAndBtn() {
        //urlInputTF.delegate = self
        container.addSubview(urlInputTF)

        urlInputTF.topAnchor.constraint(equalTo: container.topAnchor, constant: 0).isActive = true
        urlInputTF.leadingAnchor.constraint(equalTo: container.leadingAnchor, constant: 0).isActive = true
        urlInputTF.trailingAnchor.constraint(equalTo: container.leadingAnchor, constant: 0).isActive = true
        urlInputTF.heightAnchor.constraint(equalToConstant: 40).isActive = true
    }
}
UICollectionViewCell DownloadUrlCell有一个容器,其中有一个UIExtField,所有代码都指向该单元格,没有其他内容

现在,当我点击文本字段时,视图会随着键盘的高度向上移动。我没有向shift view添加任何代码。我不知道为什么会发生这种情况,在stack或其他任何地方都找不到类似的问题。 我没有使用任何第三方库

Edit1:好吧,我上传了一个演示项目,也有同样的问题。textfield位于第三个单元格中,名为:DownloadUrlCell。以下是github项目的链接:

编辑2:所以在尝试了其他一些事情之后,我得出结论,这个问题只存在于UICollectionViewCell中。如果我在任何UIViewController中添加textfield,它都可以正常工作,没有问题,但是如果我在UICollectionViewCell中添加textfield,然后点击textfield,视图就会上移

下面是一个带有UICollectionViewController和一个简单单元格的简单项目。项目中没有其他内容,但问题仍然存在

Edit3:所以在四处摸索之后,我找到了这个确实有用的答案。然而,原因仍不清楚


解决方案:

对于iOS 11,有关闭这种滚动行为的属性

在setupCollectionView方法中添加以下代码以修复您的问题

    func setupCollectionView(){

        ....
        ....

        if #available(iOS 11.0, *) {
            self.collectionView?.contentInsetAdjustmentBehavior = .never
        } else {
            automaticallyAdjustsScrollViewInsets = false
        }
    }

我希望这将对您有所帮助。

它不起作用,因为您必须在setupCollectionView中添加这两行

func setupCollectionView(){
    self.collectionView?.backgroundColor = .gray
    self.collectionView?.isPagingEnabled = true
    self.collectionView?.register(HomeCell.self, forCellWithReuseIdentifier: "homecellid")
    self.collectionView?.register(VideoListCell.self, forCellWithReuseIdentifier: "secondcellid")
    self.collectionView?.register(DownloadUrlCell.self, forCellWithReuseIdentifier: "DownloadUrlCellid")
    self.collectionView?.register(FourthCell.self, forCellWithReuseIdentifier: "FourthCellid")
    self.collectionView?.contentInset = UIEdgeInsetsMake(65, 0, 0, 0)
    self.collectionView?.bounces = false
    self.collectionView?.showsHorizontalScrollIndicator = false
    self.collectionView?.showsVerticalScrollIndicator = false
    if #available(iOS 11.0, *) {
        self.collectionView?.contentInsetAdjustmentBehavior = .never
    } else {
        automaticallyAdjustsScrollViewInsets = false
    }
}
您的结果将是:


您使用过IQKeyboardManagerSwift吗?不,我没有使用任何外部库。@AfnanAhmad您可以在我们可以查看的地方共享任何演示项目吗?好的,我将尝试将演示上传到git,并尽快在此处共享。@DharmeshKheni我已添加到演示项目的链接。请检查它,并提供任何有用的反馈。我实际上是在寻找这个问题的原因。您只为ios 11提供了一个解决方案,那么以前的OS版本呢?请检查我的更新答案,对于ios 10或更早版本,您可以使用automaticallyAdjustsScrollViewInsets属性。设为false。@AfnanAhmad希望这有帮助:我确实这样做了,但问题仍然存在。然而,我找到了一个解决办法。问题是主UICollectionViewController是如何创建的。将其更改为简单的UIViewController,然后将UICollectionView属性添加到该控制器后,一切正常。感谢您的帮助: