在Swift中有没有一种方法可以使我的按钮宽度与alertView中的文本字段匹配?

v09wglhw  于 2023-03-07  发布在  Swift
关注(0)|答案(1)|浏览(82)
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
  })

thigvfpy

thigvfpy1#

如果你想在文本域中添加一个按钮,并且你想让这个按钮占据文本域的整个宽度,你不需要在文本域的左视图和右视图中添加这个按钮,你需要像添加一个子视图一样添加它,并且给予它一个如下的约束

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

相关问题