0

I need to expand textfield "Date of departure" of the first segment to the full width and hide nearby textfield "Back".

I tried to do as here How to make a full-width UITextField in Swift but textfield width didn't change.

startDate.frame.size.width = UIScreen.main.bounds.width

AutoLayout of textfield

enter image description here

Screens of textfield behavior

enter image description here

enter image description here

Doromi
  • 173
  • 1
  • 1
  • 5

1 Answers1

1

First I think that there are unneeded constraints on the text field:

  • "Trailing Space to: End Date" is duplicated.
  • If you are setting constraints for leading and trailing, there is no need to add another constraint for the width "Equal Width to: End Date".
  • Same logic as above, since you are already added a fixed height constraint, there is no need to add "Equal Height to: End Date".

So, after fixing (removing the extra constraints), what you should do is to add IBOutlets in the view controller for the leading and the trailing constraints, then you can edit their constant value to 0.0.

class ViewController: UIViewController {

    //...

    @IBOutlet weak var dateTextFieldLeading: NSLayoutConstraint!
    @IBOutlet weak var dateTextFieldTrailing: NSLayoutConstraint!

    // call this method when needed
    func expandDateTextFieldWidth() {
        dateTextFieldLeading.constant = 0.0
        dateTextFieldTrailing.constant = 0.0
    }

    //...
}
Ahmad F
  • 30,560
  • 17
  • 97
  • 143
  • Thank you for the help. I'm bad at AutoLayout. Could I please ask about this code? I removed unneeded constraints, connected these IBOutlets with my date textfield. Where should I call this function? Also textfield can't call this func. – Doromi Dec 12 '19 at 08:58
  • 1
    @Doromi I assume that you will need to call this method when the date text field becomes the first responder, https://stackoverflow.com/questions/5683376/textfielddidbeginediting-for-more-than-one-textfield. – Ahmad F Dec 12 '19 at 11:36