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
})
1条答案
按热度按时间thigvfpy1#
如果你想在文本域中添加一个按钮,并且你想让这个按钮占据文本域的整个宽度,你不需要在文本域的左视图和右视图中添加这个按钮,你需要像添加一个子视图一样添加它,并且给予它一个如下的约束