gpt4 book ai didi

ios - CALayer 的边框颜色和宽度都没有变化?

转载 作者:行者123 更新时间:2023-11-28 08:20:09 40 4
gpt4 key购买 nike

我有 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
}
}

最佳答案

swift 4

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

关于ios - CALayer 的边框颜色和宽度都没有变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41570534/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com