Close Menu
  • Home
  • AI
  • Big Data
  • Cloud Computing
  • iOS Development
  • IoT
  • IT/ Cybersecurity
  • Tech
    • Nanotechnology
    • Green Technology
    • Apple
    • Software Development
    • Software Engineering

Subscribe to Updates

Get the latest technology news from Bigteetechhub about IT, Cybersecurity and Big Data.

    What's Hot

    The search for new bosons beyond Higgs – Physics World

    March 11, 2026

    Amazon is linking site hiccups to AI efforts

    March 11, 2026

    Captcha scam spreads online, tricking Mac users with malware

    March 11, 2026
    Facebook X (Twitter) Instagram
    Facebook X (Twitter) Instagram
    Big Tee Tech Hub
    • Home
    • AI
    • Big Data
    • Cloud Computing
    • iOS Development
    • IoT
    • IT/ Cybersecurity
    • Tech
      • Nanotechnology
      • Green Technology
      • Apple
      • Software Development
      • Software Engineering
    Big Tee Tech Hub
    Home»iOS Development»swift – Swiftui LazyVStack issue on iOS 17, 18
    iOS Development

    swift – Swiftui LazyVStack issue on iOS 17, 18

    big tee tech hubBy big tee tech hubNovember 6, 2025003 Mins Read
    Share Facebook Twitter Pinterest Copy Link LinkedIn Tumblr Email Telegram WhatsApp
    Follow Us
    Google News Flipboard
    swift – Swiftui LazyVStack issue on iOS 17, 18
    Share
    Facebook Twitter LinkedIn Pinterest Email Copy Link


    I can’t figure out why the LazyVStack won’t snap back sometimes after dismissing the keyboard. There is one thing I understood that is when size of the views inside the LazyVStack are same there won’t be any issues but when size varies this issue arises.

    Lazy is in background yellow and scrollview is in green. Just put it like that to show my issue clearly.

    enter image description here

    struct MessagesView: View {
        @State private var messages: [ChatMessage] = MockChatMessages().loadAllMessages()
        @State private var inputText: String = ""
        @Binding var showChat: Bool
        
        @State private var scrollToID: Int?     // Used for iOS 17 auto-scroll
        
        var body: some View {
            VStack(spacing: 0) {
                HeaderView()
                MessagesList(messages: messages, scrollToID: $scrollToID)
                InputBar(inputText: $inputText, onSend: sendMessage)
            }
            .background(Color.blue.opacity(0.3))
            .ignoresSafeArea(edges: .top)
            .onAppear {
                scrollToID = messages.last?.id
            }
            .onChange(of: messages.count) { _ in
                scrollToID = messages.last?.id
            }
        }
    }
    
    // MARK: - Header
    struct HeaderView: View {
        var body: some View {
            if #available(iOS 17.0, *) {
                Text("Chat")
                    .frame(width: UIScreen.main.bounds.width, height: 70)
                    .padding(.top, 20)
                    .safeAreaPadding(.top)
                    .background(Color.red.opacity(0.5))
                    .clipShape(Rectangle())
                
            } else {
                Text("Chat")
                    .frame(height: 70)
                    .background(Color.red.opacity(0.5))
                    .clipShape(Rectangle())
                    .padding(.top, 20)
            }    }
    }
    
    // MARK: - Messages List
    struct MessagesList: View {
        var messages: [ChatMessage]
        @Binding var scrollToID: Int?
        
        var body: some View {
            if #available(iOS 17.0, *) {
                ScrollView {
                    LazyVStack(spacing: 14) {
                        ForEach(messages, id: \.id) { msg in
                            MessageBubble(message: msg)
                        }
                    }
                    .padding(.vertical)
                    .background(Color.yellow.opacity(0.5))
                }
                .background(Color.green.opacity(0.5))
                .scrollIndicators(.hidden)
                .scrollPosition(id: $scrollToID, anchor: .bottom)
            } else {
                ScrollViewReader { proxy in
                    ScrollView {
                        LazyVStack(spacing: 14) {
                            ForEach(messages, id: \.id) { msg in
                                MessageBubble(message: msg)
                                    .id(msg.id)
                            }
                        }
                        .padding(.vertical)
                    }
                    .onChange(of: scrollToID) { id in
                        if let id = id {
                            withAnimation {
                                proxy.scrollTo(id, anchor: .bottom)
                            }
                        }
                    }
                }
            }
        }
    }
    
    // MARK: - Input Bar
    struct InputBar: View {
        @Binding var inputText: String
        var onSend: () -> Void
        
        var body: some View {
            HStack {
                TextField("Type your message...", text: $inputText)
                    .padding(12)
                    .background(Color.white)
                    .clipShape(RoundedRectangle(cornerRadius: 10))
                
                Button(action: onSend) {
                    Text("Send")
                        .foregroundColor(.white)
                        .padding(.vertical, 10)
                        .padding(.horizontal, 16)
                        .background(Color.blue)
                        .clipShape(RoundedRectangle(cornerRadius: 10))
                }
            }
            .padding(.horizontal)
            .padding(.bottom, 12)
            .background(Color.gray.opacity(0.15))
        }
    }
    
    // MARK: - Single Message Bubble
    struct MessageBubble: View {
        var message: ChatMessage
        
        var isRight: Bool { message.direction == .right }
        
        var body: some View {
            HStack {
                if isRight { Spacer() }
                
                Text(message.message)
                    .foregroundColor(isRight ? .white : .black)
                    .padding(.vertical, 10)
                    .padding(.horizontal, 12)
                    .background(isRight ? Color.black : Color.white)
                    .clipShape(RoundedRectangle(cornerRadius: 14))
                    .frame(maxWidth: UIScreen.main.bounds.width * 0.7, alignment: isRight ? .trailing : .leading)
                
                if !isRight { Spacer() }
            }
            .padding(.horizontal, 12)
        }
    }
    
    // MARK: - Add Message Function
    extension MessagesView {
        func sendMessage() {
            guard !inputText.isEmpty else { return }
            
            let nextID = (messages.last?.id ?? 0) + 1
            
            let msg = ChatMessage(
                id: nextID,
                direction: .right,
                message: inputText
            )
            
            messages.append(msg)
            inputText = ""
            scrollToID = msg.id
        }
    }
    



    Source link

    iOS issue LazyVStack Swift SwiftUI
    Follow on Google News Follow on Flipboard
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email Copy Link
    tonirufai
    big tee tech hub
    • Website

    Related Posts

    uikit – Why the title doesn’t follow the navigation inline state in iOS 26

    March 11, 2026

    ios – OS emoji keyboard causes UI freeze in chat TextField Flutter

    March 10, 2026

    Future Updates | Cocoanetics

    March 9, 2026
    Add A Comment
    Leave A Reply Cancel Reply

    Editors Picks

    The search for new bosons beyond Higgs – Physics World

    March 11, 2026

    Amazon is linking site hiccups to AI efforts

    March 11, 2026

    Captcha scam spreads online, tricking Mac users with malware

    March 11, 2026

    React Native vs Flutter: An overview and 6 decision factors

    March 11, 2026
    About Us
    About Us

    Welcome To big tee tech hub. Big tee tech hub is a Professional seo tools Platform. Here we will provide you only interesting content, which you will like very much. We’re dedicated to providing you the best of seo tools, with a focus on dependability and tools. We’re working to turn our passion for seo tools into a booming online website. We hope you enjoy our seo tools as much as we enjoy offering them to you.

    Don't Miss!

    The search for new bosons beyond Higgs – Physics World

    March 11, 2026

    Amazon is linking site hiccups to AI efforts

    March 11, 2026

    Subscribe to Updates

    Get the latest technology news from Bigteetechhub about IT, Cybersecurity and Big Data.

      • About Us
      • Contact Us
      • Disclaimer
      • Privacy Policy
      • Terms and Conditions
      © 2026 bigteetechhub.All Right Reserved

      Type above and press Enter to search. Press Esc to cancel.