CALayer边框颜色和宽度没有变化?

时间:2017-01-10 13:55:17

标签: ios swift uitextfield width border

我有5个textFields,我的要求是我必须先设置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
  }
}

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

快捷键4

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