divadretlaw / CustomAlert

🔔 Custom Alert for SwiftUI
MIT License
195 stars 18 forks source link

Unable to get focus for a text field in iOS 17 Simulators #9

Closed SandorUK closed 1 year ago

SandorUK commented 1 year ago

Hi David (or anyone contributing to this repo),

Since upgrading to Xcode 15 I have noticed an issue, but couldn't figure out the underlying cause. I wonder if you experienced anything similar?

Environment

Repro Steps

  1. Create a project with SwiftUI view, add a button to trigger boolean state variable to display the view.
  2. Add multiline text view (see code snippet below) to the custom alert.
  3. Run XCUITest or just manually trigger custom alert display, make sure keyboard is setup correctly in simulator (can be tested by creating a plain TextField view and tapping into it, you should be able to type in it).
  4. Attempt to tap into multiline text field and type text. It is unresponsive to both user interactions and .tap() or .typeText(..) XCUITest methods.

Notes

  1. Same code worked in simulator and UI tests on iOS 16 simulators with no issues.

Sample alert snippet:

.customAlert(isPresented: $isInsideCommenting) {
            // MARK: Inside Commenting on Action
            Text("Action Comment")
                .font(.subheadline)
                .padding(.bottom, 4)
            getMultilineTextField("Comment for \(viewModel.action.name ?? "")", $insideComment)
        } actions: {
            /// Inside Commenting Popup Buttons
            MultiButton {
                Button("Cancel", role: .cancel) {
                    insideComment = viewModel.comment
                    isInsideCommenting = false
                }
                Button("OK") {
                    let sanitisedComment = insideComment.trimmingCharacters(in: .whitespacesAndNewlines)
                    if sanitisedComment.isEmpty {
                        viewModel.deleteComment()
                    } else {
                        viewModel.addComment(insideComment)
                    }
                    commentRecorded?()
                    isInsideCommenting = false
                }
            }
        }

Sample multiline text field constructor method:

@ViewBuilder
    func getMultilineTextField(_ title: String,
                               _ stringValue: Binding<String>) -> some View {
        if #available(iOS 16.0, *) {
            TextField(title, text: stringValue, axis: .vertical)
                .lineLimit(5, reservesSpace: true)
                .textFieldStyle(.roundedBorder)
                .multilineTextAlignment(.leading)
                .accessibilityLabel("TextFieldInAlert")
        } else {
            // Fallback on earlier versions
            TextField(text: stringValue) {
                Text(title).multilineTextAlignment(.leading)
            }.accessibilityLabel("TextFieldInAlert")
        }
    }
divadretlaw commented 1 year ago

Hi SandorUK, yes, I can confirm this issue, thank you for reporting it, I will investigate and hopefully fix it soon,

divadretlaw commented 1 year ago

The issue was using the disabled modifier to disable the scrolling if the alert does not need to be scrollable. This worked fine until iOS 17, but since iOS 16, the scrollDisabled(_:) modifier should be used anyway, so this one will be used on iOS 16 and above and the issue should be fixed in 2.5.0.

Please let me know if you still experience issues after upgrading to the latest version.

SandorUK commented 1 year ago

Tested Fix

@divadretlaw I can confirm upgrading to 2.5.0 is the solution.

Thank you!

Please provide a link or a crypto wallet to send you a coffee!

image.

divadretlaw commented 1 year ago

Thank you for the offer, but there is no need. Thank you for using this project, even in case its only for trying it out 👍🏻