Is there a way in Swift to match my button width with my textfield inside of my alertView?

52 Views Asked by At
returnAlert?.addTextField(configurationHandler: { [self] (textField) in
//            //Dropdown Start here
      let customDiscounTypes:[String] = ["\(PaymentCardType.MasterCard)", "\(PaymentCardType.VISA)", "\(PaymentCardType.AmericanExpress)", "\(PaymentCardType.DinnersClub)", "\(PaymentCardType.JBC)", "Others"]

      let button = UIButton(type: .system)
      button.translatesAutoresizingMaskIntoConstraints = false

      button.heightAnchor.constraint(equalToConstant: 44).isActive = true
      button.widthAnchor.constraint(equalToConstant: 250).isActive = true
      
      button.setTitleColor(.lightGray, for: .normal)
      button.titleLabel?.font = .systemFont(ofSize: 18, weight: .light)
      
      button.setTitle(" Card Type", for: .normal)
      button.setTitleColor(.lightGray, for: .normal)
      button.contentHorizontalAlignment = .left
      button.backgroundColor = .black
      button.titleLabel?.font = .systemFont(ofSize: 18, weight: .light)
      
      if #available(iOS 14.0, *) {
          button.showsMenuAsPrimaryAction = true
          button.menu = UIMenu(children: customDiscounTypes.map({ item in
              UIAction(title: String(item)) { action in
                  textField?.text = String(item)
                  let a: String = textField!.text!
                  button.setTitle("  \(a)", for: .normal)
                  button.setTitleColor(.black, for: .normal)
                  print(a)
                  if a == "Others" {
                      (self.returnAlert?.textFields?[4] as? UITextField)?.isHidden = false
                      (self.returnAlert?.textFields?[4] as? UITextField)?.text = ""
                      (self.returnAlert?.textFields?[4] as? UITextField)?.placeholder = "Specify"
                  } else if a != "Others"{
                      (self.returnAlert?.textFields?[4] as? UITextField)?.isHidden = true
                  }

              }
          }))
      } else {
          // Fallback on earlier versions
      }
      textField?.leftView = button
      textField?.rightViewMode = .always
  })

The color black is my button

1

There are 1 best solutions below

0
Ahmed Mohiy On

if you wanna add a button to the textfield and you want that button to take the whole width of the textfield you don't add that button to the leftview nor the rightview of the textfield you add it like a subview and give it a constraints like this

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    
    let textfield = UITextField()
    textfield.placeholder = "text"
    textfield.layer.borderWidth = 1
    textfield.layer.borderColor = UIColor.red.cgColor
    
    let button = UIButton()
    button.setTitle("Button", for: .normal)
    
    
    textfield.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(textfield)
    NSLayoutConstraint.activate([
        textfield.centerXAnchor.constraint(equalTo: view.centerXAnchor),
        textfield.centerYAnchor.constraint(equalTo: view.centerYAnchor),
        textfield.heightAnchor.constraint(equalToConstant: 44),
        textfield.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.8)
    ])
    
    button.backgroundColor = .green
    textfield.addSubview(button)
    button.translatesAutoresizingMaskIntoConstraints = false
    NSLayoutConstraint.activate([
        button.topAnchor.constraint(equalTo: textfield.topAnchor),
        button.rightAnchor.constraint(equalTo: textfield.rightAnchor),
        button.bottomAnchor.constraint(equalTo: textfield.bottomAnchor),
        button.leftAnchor.constraint(equalTo: textfield.leftAnchor),
    ])
    
    
}