Swift UITextField 在 UIStackView 中不可编辑
Swift UITextField not editable in UIStackView
我正在学习开发应用程序。我使用了一个空的 UIStackview,我将通过代码填充它。为此,我创建了一个 Helper-Class (type327.swift)。 Class return 其中包含一个Stackview,三个Sub-Stackview。此子视图之一是带有 UITextfields 的 Stackview。我想修改 Phone 上的值,但文本字段似乎不可编辑。我做错了什么或者我的逻辑错误在哪里?
Type327-Class:
let unit: String?
let labelVCurrentText: UILabel! = {
let label = UILabel()
// label.text = "Label 1"
// label.font = .systemFont(ofSize: 14, weight: .regular)
// label.numberOfLines = 1
// label.backgroundColor = .clear
// label.textAlignment = .left
// //label.sizeToFit()
return label
}()
let labelVNomText: UILabel = {
let label = UILabel()
// label.text = "Label 1"
// label.font = .systemFont(ofSize: 14, weight: .bold)
// label.numberOfLines = 1
// label.backgroundColor = .clear
// label.textAlignment = .left
// label.sizeToFit()
return label
}()
let labelAimRelativText: UILabel = {
let label = UILabel()
// label.text = "Label 1"
// label.font = .systemFont(ofSize: 14, weight: .bold)
// label.numberOfLines = 1
// label.backgroundColor = .clear
// label.textAlignment = .left
// label.sizeToFit()
return label
}()
let labelVMaxRelativeText: UILabel = {
let label = UILabel()
// label.text = "Label 1"
// label.font = .systemFont(ofSize: 14, weight: .bold)
// label.numberOfLines = 1
// label.backgroundColor = .clear
// label.textAlignment = .left
// label.sizeToFit()
return label
}()
let labelVMinRelativeText: UILabel = {
let label = UILabel()
// label.text = "Label 1"
// label.font = .systemFont(ofSize: 14, weight: .bold)
// label.numberOfLines = 1
// label.backgroundColor = .clear
// label.textAlignment = .left
// label.sizeToFit()
return label
}()
let labelVBtwRelativeText: UILabel = {
let label = UILabel()
// label.text = "Label 1"
// label.font = .systemFont(ofSize: 14, weight: .bold)
// label.numberOfLines = 1
// label.backgroundColor = .clear
// label.textAlignment = .left
// label.sizeToFit()
return label
}()
let labelVAimAbsolutText: UILabel = {
let label = UILabel()
// label.text = "Label 1"
// label.font = .systemFont(ofSize: 14, weight: .bold)
// label.numberOfLines = 1
// label.backgroundColor = .clear
// label.textAlignment = .left
// label.sizeToFit()
return label
}()
let labelVMaxAbsolutText: UILabel = {let label = UILabel()
label.text = "Label 1"
// label.font = .systemFont(ofSize: 14, weight: .bold)
// label.numberOfLines = 1
// label.backgroundColor = .clear
// label.textAlignment = .left
// label.sizeToFit()
return label}()
let labelVMinAbsolutText: UILabel = {
let label = UILabel()
// label.text = "Label 1"
// label.font = .systemFont(ofSize: 14, weight: .bold)
// label.numberOfLines = 1
// label.backgroundColor = .clear
// label.textAlignment = .left
// label.sizeToFit()
return label
}()
let labelVBtwAbsolutText: UILabel = {
let label = UILabel()
return label
}()
let txtVCurrentValue: UITextField = {
let box = UITextField()
box.keyboardType = .decimalPad
box.isEditable
return box
}()
let txtVNomValue: UITextField = {
let box = UITextField()
box.keyboardType = .decimalPad
return box
}()
let txtAimRelativValue: UITextField = {
let box = UITextField()
box.keyboardType = .decimalPad
return box
}()
let txtVMaxRelativeValue: UITextField = {
let box = UITextField()
box.keyboardType = .decimalPad
return box
}()
let txtVMinRelativeValue: UITextField = {
let box = UITextField()
box.keyboardType = .decimalPad
return box
}()
let txtVBtwRelativeValue: UITextField = {
let box = UITextField()
box.keyboardType = .decimalPad
return box
}()
let txtVAimAbsolutValue: UITextField = {
let box = UITextField()
box.keyboardType = .decimalPad
return box
}()
let txtVMaxAbsolutValue: UITextField = {
let box = UITextField()
box.backgroundColor = .systemBlue
box.keyboardType = .decimalPad
return box
}()
let txtVMinAbsolutValue: UITextField = {
let box = UITextField()
box.keyboardType = .decimalPad
return box
}()
let txtVBtwAbsolutValue: UITextField = {
let box = UITextField()
box.keyboardType = .decimalPad
return box
}()
init(){
labelVCurrentText.text = "∆ p@Vnom"
labelVNomText.text = "Vnom"
labelAimRelativText.text = "Target value rel."
labelVMaxRelativeText.text = "VMax relativ"
labelVMinRelativeText.text = "VMin relativ"
labelVBtwRelativeText.text = "Vbtw realtiv"
labelVAimAbsolutText.text = "Target value abs."
labelVMaxAbsolutText.text = "VMax absolut"
labelVMinAbsolutText.text = "VMin absolut"
labelVBtwAbsolutText.text = "Vbtw absolut"
txtVNomValue.text = String(0)
txtVCurrentValue.text = String(0)
txtAimRelativValue.text = String(0)
txtVMaxRelativeValue.text = String(0)
txtVMinRelativeValue.text = String(0)
txtVBtwRelativeValue.text = String(0)
txtVAimAbsolutValue.text = String(0)
txtVMaxAbsolutValue.text = String(0)
txtVMinAbsolutValue.text = String(0)
txtVBtwAbsolutValue.text = String(0)
unit = "m3/h"
}
lazy var viewText: UIStackView = {
//MARK: - Textlabels for 327-View
let labelText: [UILabel] = [labelVCurrentText, labelVNomText, labelAimRelativText, labelVMaxRelativeText, labelVMinRelativeText, labelVBtwRelativeText, labelVAimAbsolutText, labelVMaxAbsolutText, labelVMinAbsolutText, labelVBtwAbsolutText]
for label in labelText {
label.textAlignment = .left
}
let stackView = UIStackView(arrangedSubviews: labelText)
stackView.axis = .vertical
stackView.spacing = 10
return stackView
}()
lazy var viewUnit: UIStackView = {
//MARK: - UnitLabels for 327-View
let labelVCurrentUnit = UILabel()
labelVCurrentUnit.text = "Pa"
let labelVNomUnit = UILabel()
labelVNomUnit.text = "m3/h"
let labelAimRelativUnit = UILabel()
labelAimRelativUnit.text = "%"
let labelVMaxRelativeUnit = UILabel()
labelVMaxRelativeUnit.text = "%"
let labelVMinRelativeUnit = UILabel()
labelVMinRelativeUnit.text = "%"
let labelVBtwRelativeUnit = UILabel()
labelVBtwRelativeUnit.text = "%"
let labelVAimAbsolutUnit = UILabel()
labelVAimAbsolutUnit.text = "m3/h"
let labelVMaxAbsolutUnit = UILabel()
labelVMaxAbsolutUnit.text = "m3/h"
let labelVMinAbsolutUnit = UILabel()
labelVMinAbsolutUnit.text = "m3/h"
let labelVBtwAbsolutUnit = UILabel()
labelVBtwAbsolutUnit.text = "m3/h"
let labelUnit: [UILabel] = [labelVCurrentUnit, labelVNomUnit, labelAimRelativUnit, labelVMaxRelativeUnit, labelVMinRelativeUnit, labelVBtwRelativeUnit, labelVAimAbsolutUnit, labelVMaxAbsolutUnit, labelVMinAbsolutUnit, labelVBtwAbsolutUnit]
for label in labelUnit {
label.textAlignment = .left
}
let stackView = UIStackView(arrangedSubviews: labelUnit)
stackView.axis = .vertical
stackView.spacing = 10
return stackView
}()
lazy var viewValue: UIStackView = {
//MARK: - Textlabels for 327-View
let boxValue: [UITextField] = [txtVCurrentValue, txtVNomValue, txtAimRelativValue, txtVMaxRelativeValue, txtVMinRelativeValue, txtVBtwRelativeValue, txtVAimAbsolutValue, txtVMaxAbsolutValue, txtVMinAbsolutValue, txtVBtwAbsolutValue]
// for label in labelValue {
// label.textAlignment = .left
// label.text = "n/a"
// }
let stackView = UIStackView(arrangedSubviews: boxValue)
stackView.axis = .vertical
stackView.spacing = 10
return stackView
}()
func setupStackViews() -> UIStackView {
let stackView = UIStackView(arrangedSubviews: [viewText, viewValue, viewUnit])
stackView.axis = .horizontal
stackView.distribution = .fillEqually
stackView.alignment = .fill
stackView.spacing = 10
stackView.translatesAutoresizingMaskIntoConstraints = false
return stackView
}
}
在Controller中调用创建的StackView:
override func viewDidLoad() {
super.viewDidLoad()
for label in viewLabels {
label.text = "n/a"
}
viewState = .disconnected
labelTitle.text = device?.name
baseActuator = BaseActuator(modbus: mod)
viewLabels = [labelProductnumber, labelActuatorIdent, labelSerialnumber, labelSoftwareVersion, labelActuatorType]
AddLabelToQueue()
lineChart.addSubview(lineChartView)
lineChartView.centerInSuperview()
lineChartView.width(to: lineChart)
lineChartView.height(to: lineChart)
setData()
refreshControll.addTarget(self, action: #selector(DidRefreshView(_:)), for: .valueChanged)
actuatorDataStackView.addSubview(type.setupStackViews())
}
你能帮我解释一下我做错了什么吗?
BR, freakontour
Screenshot iPhone
Storyboard
首先注意:当我们无法使用您的代码“as-is”时,很难提供帮助。值得回顾 Minimal Reproducible Example
第二个注意:根据你的代码和故事板的图像,它看起来是一个布局问题。
灰色视图顶部和底部的红线表示您缺少某些约束。很可能它没有高度限制。
这不一定是个问题,但您正在调用:
actuatorDataStackView.addSubview(type.setupStackViews())
没有对添加的堆栈视图设置任何约束。
如果您注意到应用程序的屏幕截图 运行ning,故事板中视图的灰色背景没有显示。如果您随后使用 Debug View Hierarchy
,您将看到灰色视图的高度为零,并且添加的堆栈视图延伸到灰色视图的边界之外。
如果 UI 元素(例如 UITextField
)超出其父视图的范围,则无法与它交互。
在故事板中,select 灰色视图并打开 Clips to Bounds
(选中复选框。
如果您随后 运行 该应用,您可能甚至看不到您添加的堆栈视图。
假设情况确实如此,请尝试像这样添加您的堆栈视图:
let v = type.setupStackViews()
v.translatesAutoresizingMaskIntoConstraints = false
actuatorDataStackView.addSubview(v)
NSLayoutConstraint.activate([
v.topAnchor.constraint(equalTo: actuatorDataStackView.topAnchor),
v.leadingAnchor.constraint(equalTo: actuatorDataStackView.leadingAnchor),
v.trailingAnchor.constraint(equalTo: actuatorDataStackView.trailingAnchor),
v.bottomAnchor.constraint(equalTo: actuatorDataStackView.bottomAnchor),
])
这将导致灰色视图增长到添加的堆栈视图的大小。
我正在学习开发应用程序。我使用了一个空的 UIStackview,我将通过代码填充它。为此,我创建了一个 Helper-Class (type327.swift)。 Class return 其中包含一个Stackview,三个Sub-Stackview。此子视图之一是带有 UITextfields 的 Stackview。我想修改 Phone 上的值,但文本字段似乎不可编辑。我做错了什么或者我的逻辑错误在哪里?
Type327-Class:
let unit: String?
let labelVCurrentText: UILabel! = {
let label = UILabel()
// label.text = "Label 1"
// label.font = .systemFont(ofSize: 14, weight: .regular)
// label.numberOfLines = 1
// label.backgroundColor = .clear
// label.textAlignment = .left
// //label.sizeToFit()
return label
}()
let labelVNomText: UILabel = {
let label = UILabel()
// label.text = "Label 1"
// label.font = .systemFont(ofSize: 14, weight: .bold)
// label.numberOfLines = 1
// label.backgroundColor = .clear
// label.textAlignment = .left
// label.sizeToFit()
return label
}()
let labelAimRelativText: UILabel = {
let label = UILabel()
// label.text = "Label 1"
// label.font = .systemFont(ofSize: 14, weight: .bold)
// label.numberOfLines = 1
// label.backgroundColor = .clear
// label.textAlignment = .left
// label.sizeToFit()
return label
}()
let labelVMaxRelativeText: UILabel = {
let label = UILabel()
// label.text = "Label 1"
// label.font = .systemFont(ofSize: 14, weight: .bold)
// label.numberOfLines = 1
// label.backgroundColor = .clear
// label.textAlignment = .left
// label.sizeToFit()
return label
}()
let labelVMinRelativeText: UILabel = {
let label = UILabel()
// label.text = "Label 1"
// label.font = .systemFont(ofSize: 14, weight: .bold)
// label.numberOfLines = 1
// label.backgroundColor = .clear
// label.textAlignment = .left
// label.sizeToFit()
return label
}()
let labelVBtwRelativeText: UILabel = {
let label = UILabel()
// label.text = "Label 1"
// label.font = .systemFont(ofSize: 14, weight: .bold)
// label.numberOfLines = 1
// label.backgroundColor = .clear
// label.textAlignment = .left
// label.sizeToFit()
return label
}()
let labelVAimAbsolutText: UILabel = {
let label = UILabel()
// label.text = "Label 1"
// label.font = .systemFont(ofSize: 14, weight: .bold)
// label.numberOfLines = 1
// label.backgroundColor = .clear
// label.textAlignment = .left
// label.sizeToFit()
return label
}()
let labelVMaxAbsolutText: UILabel = {let label = UILabel()
label.text = "Label 1"
// label.font = .systemFont(ofSize: 14, weight: .bold)
// label.numberOfLines = 1
// label.backgroundColor = .clear
// label.textAlignment = .left
// label.sizeToFit()
return label}()
let labelVMinAbsolutText: UILabel = {
let label = UILabel()
// label.text = "Label 1"
// label.font = .systemFont(ofSize: 14, weight: .bold)
// label.numberOfLines = 1
// label.backgroundColor = .clear
// label.textAlignment = .left
// label.sizeToFit()
return label
}()
let labelVBtwAbsolutText: UILabel = {
let label = UILabel()
return label
}()
let txtVCurrentValue: UITextField = {
let box = UITextField()
box.keyboardType = .decimalPad
box.isEditable
return box
}()
let txtVNomValue: UITextField = {
let box = UITextField()
box.keyboardType = .decimalPad
return box
}()
let txtAimRelativValue: UITextField = {
let box = UITextField()
box.keyboardType = .decimalPad
return box
}()
let txtVMaxRelativeValue: UITextField = {
let box = UITextField()
box.keyboardType = .decimalPad
return box
}()
let txtVMinRelativeValue: UITextField = {
let box = UITextField()
box.keyboardType = .decimalPad
return box
}()
let txtVBtwRelativeValue: UITextField = {
let box = UITextField()
box.keyboardType = .decimalPad
return box
}()
let txtVAimAbsolutValue: UITextField = {
let box = UITextField()
box.keyboardType = .decimalPad
return box
}()
let txtVMaxAbsolutValue: UITextField = {
let box = UITextField()
box.backgroundColor = .systemBlue
box.keyboardType = .decimalPad
return box
}()
let txtVMinAbsolutValue: UITextField = {
let box = UITextField()
box.keyboardType = .decimalPad
return box
}()
let txtVBtwAbsolutValue: UITextField = {
let box = UITextField()
box.keyboardType = .decimalPad
return box
}()
init(){
labelVCurrentText.text = "∆ p@Vnom"
labelVNomText.text = "Vnom"
labelAimRelativText.text = "Target value rel."
labelVMaxRelativeText.text = "VMax relativ"
labelVMinRelativeText.text = "VMin relativ"
labelVBtwRelativeText.text = "Vbtw realtiv"
labelVAimAbsolutText.text = "Target value abs."
labelVMaxAbsolutText.text = "VMax absolut"
labelVMinAbsolutText.text = "VMin absolut"
labelVBtwAbsolutText.text = "Vbtw absolut"
txtVNomValue.text = String(0)
txtVCurrentValue.text = String(0)
txtAimRelativValue.text = String(0)
txtVMaxRelativeValue.text = String(0)
txtVMinRelativeValue.text = String(0)
txtVBtwRelativeValue.text = String(0)
txtVAimAbsolutValue.text = String(0)
txtVMaxAbsolutValue.text = String(0)
txtVMinAbsolutValue.text = String(0)
txtVBtwAbsolutValue.text = String(0)
unit = "m3/h"
}
lazy var viewText: UIStackView = {
//MARK: - Textlabels for 327-View
let labelText: [UILabel] = [labelVCurrentText, labelVNomText, labelAimRelativText, labelVMaxRelativeText, labelVMinRelativeText, labelVBtwRelativeText, labelVAimAbsolutText, labelVMaxAbsolutText, labelVMinAbsolutText, labelVBtwAbsolutText]
for label in labelText {
label.textAlignment = .left
}
let stackView = UIStackView(arrangedSubviews: labelText)
stackView.axis = .vertical
stackView.spacing = 10
return stackView
}()
lazy var viewUnit: UIStackView = {
//MARK: - UnitLabels for 327-View
let labelVCurrentUnit = UILabel()
labelVCurrentUnit.text = "Pa"
let labelVNomUnit = UILabel()
labelVNomUnit.text = "m3/h"
let labelAimRelativUnit = UILabel()
labelAimRelativUnit.text = "%"
let labelVMaxRelativeUnit = UILabel()
labelVMaxRelativeUnit.text = "%"
let labelVMinRelativeUnit = UILabel()
labelVMinRelativeUnit.text = "%"
let labelVBtwRelativeUnit = UILabel()
labelVBtwRelativeUnit.text = "%"
let labelVAimAbsolutUnit = UILabel()
labelVAimAbsolutUnit.text = "m3/h"
let labelVMaxAbsolutUnit = UILabel()
labelVMaxAbsolutUnit.text = "m3/h"
let labelVMinAbsolutUnit = UILabel()
labelVMinAbsolutUnit.text = "m3/h"
let labelVBtwAbsolutUnit = UILabel()
labelVBtwAbsolutUnit.text = "m3/h"
let labelUnit: [UILabel] = [labelVCurrentUnit, labelVNomUnit, labelAimRelativUnit, labelVMaxRelativeUnit, labelVMinRelativeUnit, labelVBtwRelativeUnit, labelVAimAbsolutUnit, labelVMaxAbsolutUnit, labelVMinAbsolutUnit, labelVBtwAbsolutUnit]
for label in labelUnit {
label.textAlignment = .left
}
let stackView = UIStackView(arrangedSubviews: labelUnit)
stackView.axis = .vertical
stackView.spacing = 10
return stackView
}()
lazy var viewValue: UIStackView = {
//MARK: - Textlabels for 327-View
let boxValue: [UITextField] = [txtVCurrentValue, txtVNomValue, txtAimRelativValue, txtVMaxRelativeValue, txtVMinRelativeValue, txtVBtwRelativeValue, txtVAimAbsolutValue, txtVMaxAbsolutValue, txtVMinAbsolutValue, txtVBtwAbsolutValue]
// for label in labelValue {
// label.textAlignment = .left
// label.text = "n/a"
// }
let stackView = UIStackView(arrangedSubviews: boxValue)
stackView.axis = .vertical
stackView.spacing = 10
return stackView
}()
func setupStackViews() -> UIStackView {
let stackView = UIStackView(arrangedSubviews: [viewText, viewValue, viewUnit])
stackView.axis = .horizontal
stackView.distribution = .fillEqually
stackView.alignment = .fill
stackView.spacing = 10
stackView.translatesAutoresizingMaskIntoConstraints = false
return stackView
}
}
在Controller中调用创建的StackView:
override func viewDidLoad() {
super.viewDidLoad()
for label in viewLabels {
label.text = "n/a"
}
viewState = .disconnected
labelTitle.text = device?.name
baseActuator = BaseActuator(modbus: mod)
viewLabels = [labelProductnumber, labelActuatorIdent, labelSerialnumber, labelSoftwareVersion, labelActuatorType]
AddLabelToQueue()
lineChart.addSubview(lineChartView)
lineChartView.centerInSuperview()
lineChartView.width(to: lineChart)
lineChartView.height(to: lineChart)
setData()
refreshControll.addTarget(self, action: #selector(DidRefreshView(_:)), for: .valueChanged)
actuatorDataStackView.addSubview(type.setupStackViews())
}
你能帮我解释一下我做错了什么吗?
BR, freakontour
Screenshot iPhone
Storyboard
首先注意:当我们无法使用您的代码“as-is”时,很难提供帮助。值得回顾 Minimal Reproducible Example
第二个注意:根据你的代码和故事板的图像,它看起来是一个布局问题。
灰色视图顶部和底部的红线表示您缺少某些约束。很可能它没有高度限制。
这不一定是个问题,但您正在调用:
actuatorDataStackView.addSubview(type.setupStackViews())
没有对添加的堆栈视图设置任何约束。
如果您注意到应用程序的屏幕截图 运行ning,故事板中视图的灰色背景没有显示。如果您随后使用 Debug View Hierarchy
,您将看到灰色视图的高度为零,并且添加的堆栈视图延伸到灰色视图的边界之外。
如果 UI 元素(例如 UITextField
)超出其父视图的范围,则无法与它交互。
在故事板中,select 灰色视图并打开 Clips to Bounds
(选中复选框。
如果您随后 运行 该应用,您可能甚至看不到您添加的堆栈视图。
假设情况确实如此,请尝试像这样添加您的堆栈视图:
let v = type.setupStackViews()
v.translatesAutoresizingMaskIntoConstraints = false
actuatorDataStackView.addSubview(v)
NSLayoutConstraint.activate([
v.topAnchor.constraint(equalTo: actuatorDataStackView.topAnchor),
v.leadingAnchor.constraint(equalTo: actuatorDataStackView.leadingAnchor),
v.trailingAnchor.constraint(equalTo: actuatorDataStackView.trailingAnchor),
v.bottomAnchor.constraint(equalTo: actuatorDataStackView.bottomAnchor),
])
这将导致灰色视图增长到添加的堆栈视图的大小。