Keyboard push my cell out of the screen

I don't know why but when the Keyboard appears it push up my cell out of the screen. I'm working in a Collection View, laying my views programmatically. This the error I get when that happens:

The behavior of the UICollectionViewFlowLayout is not defined because: 2017-08-21 13:09:19.710 audibleAPP[19038:1975381] 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.

And this the Code what I have

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)

I didn't put all the Code because is too much, I tried to put where I Think the error is.

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

Upvotes: 1

Views: 789

Answers (3)

Mark
Mark

Reputation: 621

If the other solutions don't work for you. I have a new method under Swift 4. A solution that I've encountered on another StackOverFlow thread despite it receiving 0 votes on one occasion.

I won't repost the entire cost again but if you follow my answer in that thread you can find a simple UICollectionView sample that isn't affected by the keyboard. The main premise is to place a UICollectionView as a subview of a ViewController which will be the Root View Controller.

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

Upvotes: 0

Bhavesh Patel
Bhavesh Patel

Reputation: 135

Step 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)
}

Step 2:

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

Step 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()
    }
}

Hope this will help you !!!!

Upvotes: 1

Abdelahad Darwish
Abdelahad Darwish

Reputation: 6067

The solution is to set automaticallyAdjustsScrollViewInsets to False somewhere in your UICollectionViewController subclass, such as in viewDidLoad:

    self.automaticallyAdjustsScrollViewInsets = False

Upvotes: 0

Related Questions