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
})

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