【问题标题】:CALayer border colour and width are not changing?CALayer边框颜色和宽度没有变化?
【发布时间】:2017-05-25 01:02:11
【问题描述】:

我有 5 个文本字段,我的要求是最初必须设置 0.2 边框宽度,之后点击编辑按钮时,我必须将边框宽度从 0.2 更改为 0.8,然后再次点击提交按钮,我必须将边框宽度从0.8 到 0.2。

问题是边框颜色和宽度没有改变。下面是我的代码:

class EditProductCell: UITableViewCell {


  override func awakeFromNib() {
    super.awakeFromNib()

    //Create Border Line.
    createBorderLine(0.2,UIColor.lightGray)
  }

//Create Border Line on Text Field
  func createBorderLine(_ width : CGFloat, _ color : UIColor)//_ width : CGFloat, _ color : UIColor)
{
    setBottomBorder(textField: InvoiceDate, width: width,color : color)
    setBottomBorder(textField: InvoiceNumber, width: width,color : color)
    setBottomBorder(textField: modelNumber, width: width,color : color)
    setBottomBorder(textField: productName, width: width,color : color)
    setBottomBorder(textField: serialNumber, width: width,color : color)

    self.layoutSubviews()
  } 
}    

这是另一个类:

class EditProductView: BaseViewController {

  //TableView
  @IBOutlet var tableView: UITableView!

  //View Did Load
  override func viewDidLoad() {
      super.viewDidLoad()

      //self.view.backgroundColor = hexStringToUIColor(hex: "#CCCCCC")
      tableView.delegate = self
      tableView.dataSource = self

      //Button Submit
      self.btnSubmit.isHidden = true

      tableView.bounces = false
      tableView.alwaysBounceVertical = false
      hideKeyboardWhenTappedAround()

  }

  //Button Submit
  @IBAction func btnSubmitAction(_ sender: Any) {
      self.btnSubmit.isHidden = true

      let index : NSIndexPath = NSIndexPath(row: 0, section: 0)
      let tCell : EditProductCell = self.tableView.cellForRow(at: index as IndexPath) as! EditProductCell

      //Change border line
      tCell.createBorderLine(0.2, UIColor.lightGray)

      tCell.btnEdit.isHidden = false

      dismissKeyboard()
   }

  func btnEditAction()
  {
      btnSubmit.isHidden = false
      let index : NSIndexPath = NSIndexPath(row: 0, section: 0)
      let tCell : EditProductCell = self.tableView.cellForRow(at: index as IndexPath) as! EditProductCell

      tCell.btnEdit.isHidden = true

      //Create border line
      tCell.createBorderLine(0.8, UIColor.black)

      dismissKeyboard() 
  }
}    

这里是在单独的类中设置底边框的方法:

//Set Bottom border line.
func setBottomBorder(textField: UITextField, width: CGFloat,color : UIColor) {

  let border = CALayer()
  border.name = "BottomBorder"
  border.borderColor = color.cgColor
  border.frame = CGRect(x: 0, y: textField.frame.size.height - width,
                      width: textField.frame.size.width, height: width)
  border.borderWidth = width
  textField.borderStyle = UITextBorderStyle.none
  textField.layer.addSublayer(border)
  textField.layer.masksToBounds = true
}  

您可以在我的代码中看到按钮提交和按钮编辑。我正在更改边界线的颜色和宽度。颜色和宽度不是。

我也尝试了一些东西,但无法改变宽度。

override func layoutSublayers(of layer: CALayer) {
    if (layer == self.layer)
    {
        layer.borderWidth = 0.3
    }
}    

//MARK:- TextField Delegate
extension EditProductCell : UITextFieldDelegate
{
  func textFieldDidBeginEditing(_ textField: UITextField) {
      if let sublayers = textField.layer.sublayers {
          for layer: CALayer in sublayers  {
              if layer.name == "BottomBorder" {
                  layer.removeFromSuperlayer()
              }
          }
      }

      setBottomBorder(textField: textField, width: 0.8, color: hexStringToUIColor(hex: "#55ACEE"))
  }

  //TextField Did End Editing
  func textFieldDidEndEditing(_ textField: UITextField) {
      if let sublayers = textField.layer.sublayers {
          for layer: CALayer in sublayers  {
              if layer.name == "BottomBorder" {
                  layer.removeFromSuperlayer()
              }
          }
      }

      setBottomBorder(textField: textField, width: 0.8,color : UIColor.black)
      textField.resignFirstResponder()
  }

  //TextField Return Key
  func textFieldShouldReturn(_ textField: UITextField) -> Bool {

      // Try to find next responder
      if let nextField = textField.superview?.viewWithTag(textField.tag + 1) as? UITextField
      {
          nextField.becomeFirstResponder()
      }
      else
      {
          textField.resignFirstResponder()
      }

      return false
  }
}

【问题讨论】:

标签: ios swift uitextfield width border


【解决方案1】:

斯威夫特 4

shape.strokeColor = UIColor.lightGray.cgColor
shape.lineWidth = 1.0

【讨论】:

    【解决方案2】:

    如果您尝试使用大于 1 的数字,则您的代码可以正常工作。我认为边框宽度需要大于1。

    【讨论】:

    • 对不起,我没有得到你。我到底要做什么?谢谢
    • 您是否尝试使用从 1 到 2 的边框宽度?
    • 是的。不用找了。同样的问题。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-24
    • 1970-01-01
    • 1970-01-01
    • 2014-03-22
    • 2012-10-15
    • 2012-11-13
    • 1970-01-01
    相关资源
    最近更新 更多