KeyboardObserving icon indicating copy to clipboard operation
KeyboardObserving copied to clipboard

[Alternative method of modifying views] Switched from modifying frame to using an offset

Open ivanmkc opened this issue 5 years ago • 0 comments

Thanks for creating this Nick. I had a use case which required a slight modification that I thought might be useful for others.

Let's say we have this:

struct TestView: View {
    
    @State var text: String = ""
    
    var body: some View {
        VStack(spacing: 0) {
            Color.blue.frame(height: 300)
            
            Color.red
            
            TextField("Type something", text: $text)
                .frame(height: 100)
                .foregroundColor(Color.white)
                .background(Color.black)
        }
        .keyboardObserving()
    }
}

Using the current KeyboardObserving, the result looks like this: before

You can see it modifies the relative sizes of Blue and Red since Blue is constrained while Red is not.

When I wrote my own keyboard avoiders before SwiftUI, I found messing with the frames sometimes gave hard to debug results so I had always preferred to simply use a transformation on the underlying layer and translate everything up.

This gives the following result:

after

It may not be what your extension intends but thought it was a nice option to have.

ivanmkc avatar Nov 16 '20 11:11 ivanmkc