This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
I am currently trying to fix an issue in my chat view where when I tap on the TextField to type a comment, the entire view gets shifted upwards. While doing some digging, I saw that adding
.ignoreSafeArea(.keyboard)
was a popular fix, yet it doesn't work for me. I've tried adding it as a modifier to contentView in the body view, tried adding it to the ScrollViewReader, to all the VStacks and HStack, and nothing. The code associated with the view is as follows:
struct TakesChatView: View {
  Â
  @ObservedObject var viewModel: TakeCommentViewModel
  @FocusState var focus: Bool
  @State private var selectedMedia: [PhotosPickerItem] = []
  @State private var selectedImageData: [Data] = []
  @State private var selectedGIFData: [Data] = []
  @State private var selectedVideoData: [Data] = []
  Â
  var body: some View {
    contentView
      .navigationTitle(viewModel.take.title)
      .navigationBarTitleDisplayMode(.inline)
      .background(Color.containerBackground)
      .onChange(of: viewModel.focus) { focus in
        self.focus = focus
      }
      .onChange(of: selectedMedia) { _ in
        loadMedia()
      }
  }
  Â
  var contentView: some View {
    VStack {
      ScrollViewReader { scroll in
        ScrollView {
          VStack(spacing: 12) {
            ForEach(viewModel.comments, id: \.self) { comment in
              TakeCommentView(
                comment: comment,
                viewModel: self.viewModel
              )
              .padding(.horizontal, 10)
              .id(comment.documentID)
            }
          }
          .onChange(of: viewModel.commentAdded) { id in
            scroll.scrollTo(id, anchor: .bottom)
            viewModel.commentAdded = nil
          }
        }
        .scrollDismissesKeyboard(.immediately)
      }
      .ignoresSafeArea(.keyboard)
      Â
      VStack {
        if !selectedImageData.isEmpty {
          ScrollView(.horizontal) {
            HStack {
              ForEach(Array(selectedImageData.enumerated()), id: \.element) { index, data in
                ZStack(alignment: .topTrailing) {
                  Image(uiImage: UIImage(data: data)!)
                    .resizable()
                    .scaledToFill()
                    .frame(width: 200, height: 200)
                    .clipped()
                  Â
                  Button(action: {
                    selectedImageData.remove(at: index)
                  }) {
                    Image(systemName: "xmark.circle.fill")
                      .foregroundColor(.white)
                      .background(Color.black.opacity(0.6))
                      .clipShape(Circle())
                  }
                  .padding(5)
                }
              }
            }
            .padding(.horizontal, 10)
          }
        }
        Â
        if !selectedGIFData.isEmpty {
          ScrollView(.horizontal) {
            HStack {
              ForEach(Array(selectedGIFData.enumerated()), id: \.element) { index, data in
                ZStack(alignment: .topTrailing) {
                  WebView(gifData: data)
                    .frame(width: 200, height: 100)
                  Â
                  Button(action: {
                    selectedGIFData.remove(at: index)
                  }) {
                    Image(systemName: "xmark.circle.fill")
                      .foregroundColor(.white)
                      .background(Color.black.opacity(0.6))
                      .clipShape(Circle())
                  }
                  .padding(5)
                }
              }
            }
            .padding(.horizontal, 10)
          }
        }
        Â
        if !selectedVideoData.isEmpty {
          ScrollView(.horizontal) {
            HStack {
              ForEach(Array(selectedVideoData.enumerated()), id: \.element) { index, data in
                ZStack(alignment: .topTrailing) {
                  VideoPreviewPlayer(videoData: data)
                  Â
                  Button(action: {
                    selectedVideoData.remove(at: index)
                  }) {
                    Image(systemName: "xmark.circle.fill")
                      .foregroundColor(.white)
                      .background(Color.black.opacity(0.6))
                      .clipShape(Circle())
                  }
                  .padding(5)
                }
              }
            }
            .padding(.horizontal, 10)
          }
        }
        Â
        LazyVStack(alignment: .leading) {
          if viewModel.searchingUser {
            ScrollView {
              SearchUserView(text: $viewModel.searchText, user: $viewModel.mentionedUser, invert: true)
                .padding(5)
            }
          }
        }
        Â
        HStack {
          TextField("Type your comment", text: $viewModel.textToPost, axis: .vertical)
            .keyboardType(.twitter)
            .padding([.leading, .vertical], 6)
            .focused($focus)
            .onChange(of: viewModel.textToPost) { newValue in
              if newValue.last == "@" {
                withAnimation {
                  viewModel.searchingUser = true
                }
              } else if newValue.last == " " {
                withAnimation {
                  viewModel.searchingUser = false
                }
              }
              Â
              if viewModel.searchingUser {
                viewModel.searchText = String(newValue.last ?? "a")
              }
            }
          Â
          Â
          PhotosPicker(selection: $selectedMedia, matching: .any(of: [.images, .videos]), photoLibrary: .shared()) {
            ComposeType.media.image
              .frame(width: 40, height: 40, alignment: .center)
          }
          .onAppear {
            selectedMedia.removeAll()
            selectedImageData.removeAll()
            selectedGIFData.removeAll()
            selectedVideoData.removeAll()
          }
          Â
          postButton
        }
        .border(.lightGray, width: 1, cornerRadius: 10)
        .padding([.bottom, .horizontal], 10)
      }
    }
  }
}
What am I missing to have the modifier work as intended so the view stops shifting upwards?
Edit: Simplified the code a bit to make it more readable and take less space.
Subreddit
Post Details
- Posted
- 3 months ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/SwiftUI/com...