1

I've created a UIStackView programmatically and inside placed UITextViews.
when the number of the UITextViews is more than 5 the boxes start overflowing off the screen. Here's a screenshot of the situation:

enter image description here

And here's the code :

let stackview = UIStackView(arrangedSubviews: letterBoxes)
stackview.axis = .horizontal
stackview.spacing = 10
stackview.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(stackview)
stackview.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
stackview.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true

what am I missing? thank you

koen
  • 5,383
  • 7
  • 50
  • 89
Neta yam
  • 69
  • 5

1 Answers1

1

Also add .leading and .trailing constraints like this

view.addConstraint(NSLayoutConstraint(item: stackview, attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailing, multiplier: 1, constant: 0))            
view.addConstraint(NSLayoutConstraint(item: stackview, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1, constant: 0))

so the complete code looks like:

let stackview = UIStackView(arrangedSubviews: letterBoxes)
stackview.axis = .horizontal
stackview.spacing = 10
stackview.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(stackview)
stackview.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
stackview.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true

view.addConstraint(NSLayoutConstraint(item: stackview, attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailing, multiplier: 1, constant: 0))
view.addConstraint(NSLayoutConstraint(item: stackview, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1, constant: 0))
Keshu R.
  • 5,045
  • 1
  • 18
  • 38
  • thanks! what would be the best way to get each character the user typed and validate it against the correct answer [ i also have a hint button which auto-complete 1 character every time user request a hint so I'm not sure what would be the best approach for this] – Neta yam Jan 29 '20 at 22:17
  • @NetaYamin please ask it as a new question and accept this one if it helped. :) .. Hint: You can use `textFieldDidChange` and inside this compare your `textField.text` with thd correct answer. – Keshu R. Jan 30 '20 at 05:00