20.1 C
Canberra
Tuesday, March 3, 2026

Bug for Animating in opposition to FocusState of TextField


Right here is the view, which has an "Type Button" and "Cancel" Button on trailing fringe of an textfield. When it’s centered, I would like it to animate the buttons ("Type Button" and "Cancel") transferring out and in.

struct TextFieldFocusAnimation: View {
    @FocusState personal var isFocused: Bool
    
    var physique: some View {
        HStack {
            TextField("", textual content: .fixed(""))
                .centered($isFocused)
                .border(.purple)
            
            if !isFocused {
                Button("Type Button") {
                    print("Type clicked")
                }
                .transition(.transfer(edge: .main))
            } else {
                Button("Cancel") {
                    isFocused = false
                }
                .transition(.transfer(edge: .trailing))
            }
        }
        .padding()
        .transformEffect(.identification) // That is to isolate the geometry
        .animation(.snappy, worth: isFocused)
    }
}

The ensuing animation seems to be like this:

Bug for Animating in opposition to FocusState of TextField

Be aware, how the buttons are popping in, as a substitute of sliding.

I can inform the transitions do usually behave when coded like this too:

An GIF that shows "Sort Button" and "Cancel" Button sliding in-out on clicking "Toggle" Button

Right here, I used @State.

struct NormalPopInAnimation: View {
    @State personal var isFocused: Bool = false
    
    var physique: some View {
        
        VStack {
            HStack {
                TextField("", textual content: .fixed(""))
                    .border(.purple)
                
                if !isFocused {
                    Button("Type Button") {
                        print("Type clicked")
                    }
                    .transition(.transfer(edge: .main))
                } else {
                    Button("Cancel") {
                        isFocused = false
                    }
                    .transition(.transfer(edge: .trailing))
                }
            }
            .padding()
            .transformEffect(.identification)
            
            Button("Toggle") {
                isFocused.toggle()
            }
        }
        .animation(.snappy, worth: isFocused)
        .body(maxWidth: .infinity, maxHeight: .infinity)
        .ignoresSafeArea()
        
    }
}

I’ve already regarded into Animating modifications to @FocusState SwiftUI , I’m in search of the way to repair this with out having to introduce one other state or not less than know the explanation why this does not work the best way I feel it ought to.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

[td_block_social_counter facebook="tagdiv" twitter="tagdivofficial" youtube="tagdiv" style="style8 td-social-boxed td-social-font-icons" tdc_css="eyJhbGwiOnsibWFyZ2luLWJvdHRvbSI6IjM4IiwiZGlzcGxheSI6IiJ9LCJwb3J0cmFpdCI6eyJtYXJnaW4tYm90dG9tIjoiMzAiLCJkaXNwbGF5IjoiIn0sInBvcnRyYWl0X21heF93aWR0aCI6MTAxOCwicG9ydHJhaXRfbWluX3dpZHRoIjo3Njh9" custom_title="Stay Connected" block_template_id="td_block_template_8" f_header_font_family="712" f_header_font_transform="uppercase" f_header_font_weight="500" f_header_font_size="17" border_color="#dd3333"]
- Advertisement -spot_img

Latest Articles