Jan L
Jan L

Reputation: 253

Swift: UITextView unselectable inside UIStackView

I know that this question exists a few times, however, none of the answers have helped me fix this issue.

I have a scrollview that has a few textfields inside of it. It all works fine, except for the textfield. Those are not selectable, no matter what I do. I have many views with TextFields which I have implemented in the same manner, and those work. I am out of ideas.

My ScrollView

let scrollView = UIScrollView()
scrollView.alwaysBounceVertical = true
scrollView.translatesAutoresizingMaskIntoConstraints = false
scrollView.showsHorizontalScrollIndicator = false

Inside the Scrollview is first a ContentView, which might be useless but thats how I have always done it. This just spreads out to the ScrollView. Inside the ContentView is a StackView (vertical), which keeps all of the Content inside it. Inside the StackView is, for example, the following Text Field Stack (A Title, a TextField and an Error Label

My TextField Stack

//First Name Field
let firstNameStack = UIStackView()
firstNameStack.axis = .vertical
firstNameStack.spacing = 5
firstNameStack.translatesAutoresizingMaskIntoConstraints = false
    
firstNameTextFieldTitle.text = "firstName"
firstNameTextFieldTitle.textColor = colors.justWhite
firstNameTextFieldTitle.font = UIFont(name: "Montserrat-SemiBold", size: 14)
firstNameTextFieldTitle.textAlignment = .left
firstNameTextFieldTitle.translatesAutoresizingMaskIntoConstraints = false
    
firstNameTextField.textColor = colors.justWhite
firstNameTextField.backgroundColor = colors.lightyLightGray
firstNameTextField.attributedPlaceholder = NSAttributedString(string: "first name".localized, attributes: [NSAttributedString.Key.foregroundColor: UIColor.white]) //ID12
firstNameTextField.font =  UIFont.init(name: "montserrat", size: 18)
firstNameTextField.autocorrectionType = .no
firstNameTextField.textAlignment = .left
firstNameTextField.layer.cornerRadius = 5
firstNameTextField.clipsToBounds = true
firstNameTextField.setLeftPaddingPoints(10)
firstNameTextField.setRightPaddingPoints(10)
firstNameTextField.keyboardType = .default
firstNameTextField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
firstNameTextField.translatesAutoresizingMaskIntoConstraints = false
firstNameTextField.delegate = self
    
firstNameTextFieldError.textColor = colors.errorRed
firstNameTextFieldError.textAlignment = .left
firstNameTextFieldError.font =  UIFont(name: "Montserrat-SemiBold", size: 14)
firstNameTextFieldError.translatesAutoresizingMaskIntoConstraints = false

firstNameStack.addArrangedSubview(firstNameTextFieldTitle)
firstNameStack.addArrangedSubview(firstNameTextField)
firstNameStack.addArrangedSubview(firstNameTextFieldError)

The View lays out perfectly. No issue there. I can open the Keyboard using becomeFirstResponder(), but tapping on them does nothing. I feel stupid for overseeing something very simple, for sure.

I have added the delegate to close the keyboard when dismissing and just to be sure, I added

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool{
    return true
}

What's the issue here? Looking forward to any tips.

Upvotes: 0

Views: 62

Answers (1)

Jan L
Jan L

Reputation: 253

I have solved the issue, it was indeed my own stupidity. For anyone as blind as myself:

Be careful when adding Views to a StackView. I used StackView.addSubview(View) instead of StackView.addArrangedSubview(View), because I was doing it quickly using auto-complete. This seems to have broken the StackView and thus the touch recognizers.

Upvotes: 1

Related Questions