键盘将我的单元格推出屏幕

时间:2017-08-21 17:16:25

标签: ios swift uicollectionview

我不知道为什么,但是当键盘出现时,它将我的手机推出屏幕。我正在使用Collection View,以编程方式放置我的视图。这是发生这种情况时得到的错误:

  

未定义UICollectionViewFlowLayout的行为,因为:   2017-08-21 13:09:19.710 audibleAPP [19038:1975381]项目高度必须小于UICollectionView的高度减去截面插入顶部和底部值,减去内容插入顶部和底部值。

这就是我所拥有的代码

的AppDelegate

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    window = UIWindow(frame: UIScreen.main.bounds)
    window?.makeKeyAndVisible()

    let layout: UICollectionViewFlowLayout = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        return layout
    }()

    let mainView = MainVC(collectionViewLayout: layout)
    window?.rootViewController = mainView

    return true
}

MainController

override func viewDidLoad() {
    super.viewDidLoad()

    collectionView?.backgroundColor = .blue
    collectionView?.delegate = self
    collectionView?.dataSource = self
    collectionView?.isPagingEnabled = true

    collectionView?.register(PageCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    collectionView?.register(LoginCell.self, forCellWithReuseIdentifier: loginIdentifier)

我没有把所有代码都放在一边因为太多了,我试着把它放在我认为错误的地方。

Here is a picture of the simulator, the red view is all the cell:

3 个答案:

答案 0 :(得分:1)

第1步:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    let notificationCenter = NotificationCenter.default
    notificationCenter.addObserver(self, selector: #selector(self.adjustForKeyboard(notification:)), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
}

第2步:

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    let notificationCenter = NotificationCenter.default
    notificationCenter.removeObserver(self, name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
}

第3步:

//keyboard with tableview/ collection view handling
func adjustForKeyboard(notification: Notification) {
    if let userInfo = notification.userInfo, let endFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {

        if (endFrame.origin.y) >= UIScreen.main.bounds.size.height {
            //self.keyboardHeightLayoutConstraint?.constant = 0.0
            let contentInsets: UIEdgeInsets = .zero
            self.collectionView.contentInset = contentInsets
            self.collectionView.scrollIndicatorInsets = contentInsets

        } else {
            let contentInsets: UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, endFrame.size.height+05, 0.0); //05 is padding between your textfield and keypad.
            self.collectionView.contentInset = contentInsets
            self.collectionView.scrollIndicatorInsets = contentInsets
        }
        self.collectionView.updateConstraints()
        self.collectionView.layoutIfNeeded()
    }
}

希望这会对你有所帮助!!!!

答案 1 :(得分:0)

解决方案是在UICollectionViewController子类中的某处将automaticAdjustsScrollViewInsets设置为False,例如在viewDidLoad中:

input

答案 2 :(得分:0)

如果其他解决方案不适合您。我在Swift 4下有了一个新方法。尽管它一次获得0票,但我在另一个StackOverFlow线程上遇到了一个解决方案。

我不会再次过帐全部费用,但是如果您在该线程中遵循我的回答,则可以找到不受键盘影响的简单UICollectionView示例。主要前提是将UICollectionView放置为ViewController的子视图,该ViewController将成为Root View Controller。

UICollectionView - When the keyboard appears the entire collection view is shifted with the keyboard

相关问题