How do I attach the “Bottom View” to the top of the keyboard at all times & animate it along with the interactive keyboard dismissal?

I’m creating a simple Todo App which behaves similar to every Chat App out there. The main screen consists of a List of items with a AddNewItemView on the bottom which acts as a main way to add new items to the list.

I’m using the .scrollDismissesKeyboard(.interactively) modifier on List in order to get the keyboard to dismiss when the user scrolls down. This works as expected, but poorly.

Please check the video to see the current result. (This is recorded on a simulator with slowed down animations, so it’s easier to spot the broken animations, but it works the exact same way on my device)

As you can see, there are a couple of issues that I’d like to address:

  1. As the keyboard is coming in, the List and AddNewItemView kinda get overlapped (this is a minor issue though that I could live with)
  2. If I interactively (and slowly) dismiss the keyboard, there will be a big open white space between the keyboard and the List until it completely disappears and the List animates back into place (this is my main issue)

In UIKit, I would have fixed this by listening to keyboard height changes and adjust the bottom constraint accordingly on-the-go. I’m fairly new in SwiftUI though, still getting used to the whole declarative concept, so I’m a bit more lost. How can I approach this? I want the keyboard dismissal to feel more natural.

I tried to do it by listening to UIResponder.keyboardWillShowNotification and UIResponder.keyboardWillHideNotification while mapping the UIResponder.keyboardFrameEndUserInfoKey to a local keyboardHeight variable and then adjusting the padding, but it didn’t work as the keyboardHeight was updated only when the keyboard was fully expanded or fully collapsed, there were not “mid-way” values.

In case it helps, here’s some of my main code:

Main View

 var body: some View {
    VStack(spacing: 0) {
        // Header
        TodoListHeader()
        // Content
        Group {
            if hasData {
                TodoList(
                    checkedItems: checkedItems,
                    uncheckedItems: uncheckedItems,
                    focusedItem: $focusedItem,
                    onCheckTap: { toggleCheck(item: $0) },
                    onContentTap: { focusedItem = $0 },
                    onDelete: { deleteItem(item: $0) },
                    onMove: move(from:to:)
                )
            } else {
                TodoListEmptyState()
            }
        }
        .background(Color("Grey200"))
        // Add New Item View
        AddNewItemView(addItem: addItem(text:))
    }
}

TodoList

var body: some View {
    List {
        Group {
            ForEach(uncheckedItems) { item in
                TodoView(
                    item: item,
                    onCheckTap: onCheckTap,
                    onContentTap: onContentTap) {
                        TextItem(
                            item: item,
                            focusItem: focusedItem
                        )
                    }
                    .listRowBackground(Color.clear)
                    .listRowSeparator(.hidden)
                    .listRowInsets(EdgeInsets())
                    .frame(maxWidth: .infinity)
            }
            .onDelete(perform: deleteUncheckedItem)
            .onMove(perform: onMove)
            ForEach(checkedItems) { item in
                TodoView(
                    item: item,
                    onCheckTap: onCheckTap,
                    onContentTap: onContentTap) {
                        TextItem(
                            item: item,
                            focusItem: focusedItem
                        )
                    }
                    .listRowBackground(Color.clear)
                    .listRowSeparator(.hidden)
                    .listRowInsets(EdgeInsets())
            }
            .onDelete(perform: deleteCheckedItem)
        }
    }
    .scrollDismissesKeyboard(.interactively)
    .listRowSeparator(.hidden)
    .listStyle(.plain)
    .animation(.default)
}

AddNewItemView

var body: some View {
    VStack {
        Divider()
        HStack {
            TextField(text: $newTodoText, prompt: Text("What's on your mind?"), axis: .vertical) {
                Text("Label")
            }
            .ignoresSafeArea(.keyboard, edges: .bottom)
            .submitLabel(.send)
            .onChange(of: newTodoText) {
                if newTodoText.last == "n" {
                    HapticEngine.shared.playHaptic()
                    newTodoText.removeLast()
                    addItem(newTodoText)
                    newTodoText = ""
                }
            }
            Image(systemName: "paperplane.fill")
                .onTapGesture {
                    HapticEngine.shared.playHaptic()
                    addItem(newTodoText)
                    newTodoText = ""
                }
        }
        .padding(.vertical, 8)
        .padding(.horizontal, 32)
        .padding(.bottom, 8)
    }
}

My desired result is to have a smoother behaviour, like other chat apps that have a “reply view” on the bottom.

  • On WhatsApp, the ReplyView will animate slowly along with the keyboard in case of “slow dismissal”
  • On Facebook Messenger, the keyboard can’t even be dismissed slowly. When the user starts scrolling down the list, the keyboard just instantly animates back down

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật