Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.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
如何使用SWIFT向包含UILabel的my UITextField正确添加约束?_Swift_Xcode_Constraints - Fatal编程技术网

如何使用SWIFT向包含UILabel的my UITextField正确添加约束?

如何使用SWIFT向包含UILabel的my UITextField正确添加约束?,swift,xcode,constraints,Swift,Xcode,Constraints,一般来说,我对编程比较陌生,我只是开始学习约束和自动布局以及如何正确设置。目前,我想弄清楚的是,如何使最上面的UIExtField与其中的UILabel(表示重量(lbs):70.0)对齐到我为其配置的规范(请参见下面的代码…) 当我指定宽度和长度锚定的大小时,我总是得到一个忽略整个屏幕的尺寸规格和锚定的视图(请参见下图) 任何帮助都将不胜感激 导入UIKit 导入RealmSwift 第三类虚拟控制器:UIViewController{ 让realm=try!realm() var统计:结

一般来说,我对编程比较陌生,我只是开始学习约束和自动布局以及如何正确设置。目前,我想弄清楚的是,如何使最上面的UIExtField与其中的UILabel(表示重量(lbs):70.0)对齐到我为其配置的规范(请参见下面的代码…)

当我指定宽度和长度锚定的大小时,我总是得到一个忽略整个屏幕的尺寸规格和锚定的视图(请参见下图)

任何帮助都将不胜感激

导入UIKit
导入RealmSwift
第三类虚拟控制器:UIViewController{
让realm=try!realm()
var统计:结果?
var weightextfield=UITextField()
var weightLabel=UILabel()
var repsTextField=UITextField()
var repsLabel=UILabel()
var timerImage=UIImageView()
var selectedExercise:练习{
迪塞特{
loadWsr()
}
}
//标记:-ViewDidLoad()
重写func viewDidLoad(){
super.viewDidLoad()
//时钟()
navConAcc()
labelConfig()
setTextFieldConstraints()
//setImageViewConstraints()
}
//标记:-UILabel
func labelConfig(){
weightextfield.placeholder=“总重量…”
weightTextField.layer.borderWidth=1
weightTextField.backgroundColor=.white
weightTextField.layer.cornerRadius=25
weightTextField.layer.borderColor=UIColor.lightGray.cgColor
weightlab.text=“重量(磅):”
weightLabel.textColor=.black
weightTextField.leftView=weightLabel
weightTextField.leftViewMode=.always
repsTextField.placeholder=“重复次数…”
repsTextField.layer.borderWidth=1
repsTextField.backgroundColor=.white
repsTextField.layer.cornerRadius=25
repsTextField.layer.borderColor=UIColor.lightGray.cgColor
repsLabel.text=“重复:
repsLabel.textColor=.black
repsTextField.leftView=repsLabel
repsTextField.leftViewMode=.always
[weightTextField,repsTextField].forEach{view.addSubview($0)}
}
//标记:-TextField constraint
func setTextFieldConstraints(){
weightTextField.anchor(顶部:view.safeAreaLayoutGuide.topAnchor,前导:view.leadingAnchor,底部:view.safeAreaLayoutGuide.bottomAnchor,尾随:view.trailingAnchor,大小:.init(宽度:20,高度:20))
repsTextField.anchor(顶部:view.SafeArea LayoutGuide.topAnchor,前导:view.leadingAnchor,底部:view.SafeArea LayoutGuide.bottomAnchor,尾随:view.trailingAnchor,大小:.init(宽度:20,高度:20))
}
//标记:-图像视图约束
//func setImageViewConstraints(){
//
//timerImage.anchor(顶部:view.SafeArealLayoutGuide.topAnchor,前导:view.leadingAnchor,底部:view.SafeArealLayoutGuide.bottomAnchor,尾随:view.trailingAnchor,填充:.init(顶部:40,左侧:40,底部:450,右侧:50))
//
//    }
//标记:-导航栏设置
func navConAcc(){
navigationItem.title=selectedExercise?.exerciseName
navigationController?.navigationBar.prefersLargeTitles=true
}
//马克:-秒表
//func时钟(){
//让image1=UIImage(名为“秒表”)
//timerImage=UIImageView(图像:image1)
//timerImage.contentMode=.ScaleSpectFit
//self.view.addSubview(timerImage)
//    }
//标记:-加载数据
func loadWsr(){
stats=selectedExercise?.wsr.sorted(按键路径:“集”,升序:true)
}
//标记:-保存数据
func save(wsr:weightsetreps){
做{
试试realm.write{
realm.add(wsr)
}
}抓住{
打印(“保存wsr数据时出错\(错误)”)
}
}
}
扩展UIView{
func锚定(顶部:NSLayoutYAxisAnchor?、前导:nsLayoutXaxAnchor?、底部:NSLayoutYAxisAnchor?、尾随:nsLayoutXaxAnchor?、填充:UIEdgeInsets=.0、大小:CGSize=.0){
translatesAutoresizingMaskIntoConstraints=false
如果让top=top{
约束(equalTo:top,constant:padding.top).isActive=true
}
如果让前导=前导{
leadingAnchor.constraint(相等:前导,常量:padding.left)。isActive=true
}
如果让底部=底部{
约束(equalTo:bottom,constant:padding.bottom).isActive=true
}
如果让training=training{
trailingAnchor.constraint(equalTo:trailing,constant:padding.right)。isActive=true
}
如果size.width!=0{
widthAnchor.constraint(equalToConstant:size.width).isActive=true
}
如果size.height!=0{
heightAnchor.constraint(equalToConstant:size.height).isActive=true
}
}
}

您需要移除底部锚固件

repsTextField.anchor(top: view.safeAreaLayoutGuide.topAnchor, leading: view.leadingAnchor, bottom:nil, trailing: view.trailingAnchor, size: .init(width: 20, height: 20))
repsTextField.anchor(top: view.safeAreaLayoutGuide.topAnchor, leading: view.leadingAnchor, bottom:nil, trailing: view.trailingAnchor, size: .init(width: 20, height: 20))