问题描述
我目前正在开发一个应用程序,该应用程序在Vertical StackView内有两个UILabel,在Vertical StackView内有两个UITextField,并且在一个Horizontal StackView内都具有这两个Vertical StackViews:
我有约束条件。当该应用程序在较大的设备(例如iPhone 11)上运行时,如您在此处看到的那样,它看起来很完美:
但是,如果我切换到较小的设备(例如iPhone 8),则可以看到线条像这样紧紧抓住了手机的边缘:
我为TextField做下划线的方法是使用我创建的名为StyledTextField的类。看起来像这样:
class StyledTextField: UITextField {
var bottomLine = CALayer()
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder: NSCoder) {
super.init(coder: coder)
styleTextField()
}
private func styleTextField() {
font = UIFont(name: "Quicksand",size: UIFont.labelFontSize)
// Create the bottom line
bottomLine.frame = CGRect(x: 0,y: frame.height - 2,width: frame.width,height: 2)
bottomLine.backgroundColor = Environment.Colours.primary.cgColor
// Remove border on text field
borderStyle = .none
// Add the line to the text field
layer.addSublayer(bottomLine)
}
func makeUnderlineLight() {
bottomLine.backgroundColor = Environment.Colours.primaryAssisted.cgColor
}
}
在情节提要中,我将UITextField类分配为“ StyledTextField”的类。
此外,在处理UITextField的Controller上的viewDidLoad
函数中,我调用了setUpUI()
函数,该函数执行以下操作:
func setUpUI() {
title = "Add Task"
taskNameTextField.attributedplaceholder = NSAttributedString(string: "Name",attributes: [NSAttributedString.Key.foregroundColor: Environment.Colours.lightGray])
moreInfoTextField.attributedplaceholder = NSAttributedString(string: "(Optional)",attributes: [NSAttributedString.Key.foregroundColor: Environment.Colours.lightGray])
setUpDatePicker()
moreInfoTextField.makeUnderlineLight()
if isEditing() {
showTaskToEdit()
}
view.backgroundColor = Environment.Colours.secondary
}
如您所见,我在其中调用了makeUnderlineLight()
StyledTextField函数。
谢谢!