iosswiftuicollectionviewuikitconstraints

How to ignore the safe area when using keyboardLayoutGuide?


I have a full screen list and want to use the new keyboardLayoutGuide constraint, but by default it uses the safe area inset. How can I disable this so my list goes all the way to the bottom of the screen when the keyboard is not shown?

NSLayoutConstraint.activate([
   collectionView.topAnchor.constraint(equalTo: view.topAnchor),
   collectionView.bottomAnchor.constraint(equalTo: view.keyboardLayoutGuide.topAnchor),
   collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
   collectionView.trailingAnchor.constraint(equalTo:  view.trailingAnchor)
])

enter image description here


Solution

  • usesBottomSafeArea is a new property introduced in iOS 17 to address this. Watch Keep up with the keyboard for more info.

    view.keyboardLayoutGuide.usesBottomSafeArea = false
            
    NSLayoutConstraint.activate([
       collectionView.topAnchor.constraint(equalTo: view.topAnchor),
       collectionView.bottomAnchor.constraint(equalTo: view.keyboardLayoutGuide.topAnchor),
       collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
       collectionView.trailingAnchor.constraint(equalTo:  view.trailingAnchor)
    ])