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

问题描述 投票:0回答:0

` returnAlert?.addTextField(configurationHandler: { [self] (textField) in // //下拉从这里开始 让 customDiscounTypes:[String] = ["(PaymentCardType.MasterCard)", "(PaymentCardType.VISA)", "(PaymentCardType.AmericanExpress)", "(PaymentCardType.DinnersClub)", "(PaymentCardType.JBC)", "其他“]

          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]][1]
swift xcode drop-down-menu width
© www.soinside.com 2019 - 2024. All rights reserved.