0

I have programmatically defined a textField to stretch across the width of the screen (less 50 points as a "margin" on each side via constraints. The left (leading) side works fine but the textField is only as wide as the text rather than spanning to within 50 points of the right (trailing) side. Also having the same issue with the button at the bottom of the screenshot (attached). Surely they must be related as they are the same incorrect size?!

I believe the offending / ignored constraint is the 3rd line within the NSLayoutConstraint.activate where I set the trailingAnchor constraint to -50.

My code follows:

func configureTextField() {
  view.addSubview(usernameTextField)

  NSLayoutConstraint.activate([
    usernameTextField.topAnchor.constraint(equalTo: logoImageView.bottomAnchor, constant: 48),
    usernameTextField.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 50),
    usernameTextField.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -50),
    usernameTextField.heightAnchor.constraint(equalToConstant: 50)
  ])
}

The TextField is defined in the following Class:

import UIKit

class GFTextField: UITextField {

  override init(frame: CGRect) {
    super.init(frame: frame)
    configure()
  }
    
  required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
    
  private func configure() {
    translatesAutoresizingMaskIntoConstraints = false
        
    layer.cornerRadius          = 10
    layer.borderWidth           = 2
    layer.borderColor           = UIColor.systemGray4.cgColor
        
    textColor                   = .label
    tintColor                   = .label
    textAlignment               = .center
    font                        = UIFont.preferredFont(forTextStyle: .title2)
    adjustsFontSizeToFitWidth   = true
    minimumFontSize             = 12
        
    backgroundColor             = .tertiarySystemBackground
    autocorrectionType          = .no
    placeholder                 = "Enter a username"
  }
}

ScreenShot of the TextField (2nd element from top)

Brad
  • 1
  • 1
  • Please post a [mcve]. Right now the code is not a [mcve] because we don't know how you have created `usernameTextField`, and we don't know where `logoImageView` comes from. – Sweeper Sep 30 '20 at 06:48
  • Even with the rest of the project (I think this code comes from Sean Allen's Take Home Project Course) I can't reproduce your screenshot. So I have just replaced my code with yours and everything looks as expected. So I think your problem is something else. For example are you using a ```UITableViewController``` as your searchVC? (The screenshot looks like this...) – finebel Sep 30 '20 at 07:52
  • What a noob! I have 4 VCs and this offending one was using UITableViewController rather than UIViewController. Thanks so much! I had stared at that code for 2 hours and called myself checking word-for-word but obviously was not proofing the first line! – Brad Oct 01 '20 at 01:33
  • @finebel Today I noticed that if I typed longer text into the TextField it, as well as the button below it, grew in size together. Was this because it thought those were items in tableview cells? Thanks again! – Brad Oct 01 '20 at 01:47
  • @Sweeper Point very well taken. I was concerned about being too verbose. I will strive to do better going forward! Thanks for your help. – Brad Oct 01 '20 at 01:49

0 Answers0