Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios TableView中Swift文本字段的底部边框宽度_Ios_Swift_Tableview_Border_Textfield - Fatal编程技术网

Ios TableView中Swift文本字段的底部边框宽度

Ios TableView中Swift文本字段的底部边框宽度,ios,swift,tableview,border,textfield,Ios,Swift,Tableview,Border,Textfield,我构建了一个静态表视图,其中的行数比屏幕上的多,因此用户必须滚动才能看到所有单元格 每个单元格都有一个textfield,其中包含以下类以添加底部边框: class TextFieldWithBottomBorder: UITextField { let border = CALayer() let width = CGFloat(1.0) func addBottomBorder(color: UIColor){ self.border.borderColor

我构建了一个静态表视图,其中的行数比屏幕上的多,因此用户必须滚动才能看到所有单元格

每个单元格都有一个textfield,其中包含以下类以添加底部边框:

class TextFieldWithBottomBorder: UITextField {
   let border = CALayer()
   let width = CGFloat(1.0)

   func addBottomBorder(color: UIColor){
       self.border.borderColor = color.cgColor
       self.border.frame = CGRect(x: 0, y: self.frame.size.height - width, width:  self.frame.size.width, height:self.frame.size.height)

       self.border.borderWidth = self.width
       self.layer.addSublayer(self.border)
       self.layer.masksToBounds = true
   }

   func changeBorderColor(color: UIColor){
       self.border.borderColor = color.cgColor
   }
}
我从服务器e接收到一些数据后调用该方法。g

self.firstnameTextField.text = firstNameFromDB
self.firstnameTextField.addBottomBorder(color: .blue)
对于当前显示的每个单元格,这都可以正常工作。但是不在当前视图中的单元格比文本字段短。 看到这个屏幕截图,对于Vorname,意味着firstName一切看起来都很好,但是对于email、密码等,边框很短。

将此类用于底线文本字段

@IBDesignable class BottomTextField: UITextField {
    var lineView = UIView()

 @IBInspectable var lineViewBgColor:UIColor = UIColor.gray{
        didSet {
            if !isFirstResponder {
                lineView.backgroundColor = lineViewBgColor
            }
        }
    }
required init?(coder aDecoder:NSCoder) {
        super.init(coder:aDecoder)!
        setup()
    }

    override init(frame:CGRect) {
        super.init(frame:frame)
        setup()
    }

    // MARK:- Private Methods
    private func setup() {
        lineView.frame = CGRect(x:CGFloat(0), y:self.frame.size.height-2, width:self.frame.size.width, height:CGFloat(1))
        lineView.backgroundColor = lineViewBgColor

        self.addSubview(lineView)
    }

}

调用addBottomBorder后,UITextField的大小似乎正在调整,因此该行使用的UIView现在不够宽。很难说为什么在没有看到更多代码的情况下会出现这种情况,但是有几种方法可以用来克服它

1切换到UIView而不是CALayer,并使用自动布局将视图保持在校正位置

2替代布局子视图以更新底线的框架

对您来说最简单的可能是选项2,尽管我会选择选项1,它看起来是这样的:

override func layoutSubviews() {
    super.layoutSubviews()
    self.border.frame = CGRect(x: 0, y: self.frame.size.height - width, width:  self.frame.size.width, height:self.frame.size.height)
}

现在,每当文本字段的帧/大小发生变化时,边框线的帧/大小都会相应地更新。

虽然这可能是一种更好的方法,但它会遇到相同的问题,因为底线的帧仅在创建时设置,因此,如果文本字段发生变化,底线将不会反映框架改变