swift - 如何正确地向我的 UITextField 添加约束,其中包含使用 SWIFT 的 UILabel?
问题描述
一般来说,我对编程比较陌生,我才刚刚开始学习约束和自动布局以及如何正确设置。目前,我想弄清楚的是如何使最上面的 UITextfield 与 UILabel 里面(那个说 Weight(lbs): 70.0)与我为它配置的规范对齐(见下面的代码...)。
当我指定宽度和长度锚点的大小时,我总是得到一个忽略我的尺寸规格和锚点到整个屏幕的视图(见下图)。
任何帮助将不胜感激。
import UIKit
import RealmSwift
class ThirdViewController: UIViewController {
let realm = try! Realm()
var stats : Results<WeightSetsReps>?
var weightTextField = UITextField()
var weightLabel = UILabel()
var repsTextField = UITextField()
var repsLabel = UILabel()
var timerImage = UIImageView()
var selectedExercise : Exercises? {
didSet{
loadWsr()
}
}
//MARK: - ViewDidLoad()
override func viewDidLoad() {
super.viewDidLoad()
// timeClock()
navConAcc()
labelConfig()
setTextFieldConstraints()
// setImageViewConstraints()
}
//MARK: - UILabel
func labelConfig(){
weightTextField.placeholder = "Total weight..."
weightTextField.layer.borderWidth = 1
weightTextField.backgroundColor = .white
weightTextField.layer.cornerRadius = 25
weightTextField.layer.borderColor = UIColor.lightGray.cgColor
weightLabel.text = " Weight (lbs): "
weightLabel.textColor = .black
weightTextField.leftView = weightLabel
weightTextField.leftViewMode = .always
repsTextField.placeholder = "Number of Reps..."
repsTextField.layer.borderWidth = 1
repsTextField.backgroundColor = .white
repsTextField.layer.cornerRadius = 25
repsTextField.layer.borderColor = UIColor.lightGray.cgColor
repsLabel.text = " Repetitions: "
repsLabel.textColor = .black
repsTextField.leftView = repsLabel
repsTextField.leftViewMode = .always
[weightTextField, repsTextField].forEach{view.addSubview($0)}
}
//MARK: - TextField Constrainst
func setTextFieldConstraints(){
weightTextField.anchor(top: view.safeAreaLayoutGuide.topAnchor, leading: view.leadingAnchor, bottom: view.safeAreaLayoutGuide.bottomAnchor, trailing: view.trailingAnchor, size: .init(width: 20, height: 20))
repsTextField.anchor(top: view.safeAreaLayoutGuide.topAnchor, leading: view.leadingAnchor, bottom: view.safeAreaLayoutGuide.bottomAnchor, trailing: view.trailingAnchor, size: .init(width: 20, height: 20))
}
//MARK: - ImageView Constraints
// func setImageViewConstraints(){
//
// timerImage.anchor(top: view.safeAreaLayoutGuide.topAnchor, leading: view.leadingAnchor, bottom: view.safeAreaLayoutGuide.bottomAnchor, trailing: view.trailingAnchor, padding: .init(top: 40, left: 40, bottom: 450, right: 50))
//
// }
//MARK: - Navigation Bar Setup
func navConAcc(){
navigationItem.title = selectedExercise?.exerciseName
navigationController?.navigationBar.prefersLargeTitles = true
}
//MARK: - Stopwatch
// func timeClock(){
// let image1 = UIImage(named: "stopwatch")
// timerImage = UIImageView(image: image1)
// timerImage.contentMode = .scaleAspectFit
// self.view.addSubview(timerImage)
// }
//MARK: - Load Data
func loadWsr() {
stats = selectedExercise?.wsr.sorted(byKeyPath: "sets", ascending: true)
}
//MARK: - Save Data
func save(wsr : WeightSetsReps){
do {
try realm.write {
realm.add(wsr)
}
} catch {
print("Error saving wsr data \(error)")
}
}
}
extension UIView {
func anchor(top: NSLayoutYAxisAnchor?, leading: NSLayoutXAxisAnchor?, bottom: NSLayoutYAxisAnchor?, trailing: NSLayoutXAxisAnchor?, padding: UIEdgeInsets = .zero, size: CGSize = .zero){
translatesAutoresizingMaskIntoConstraints = false
if let top = top {
topAnchor.constraint(equalTo: top, constant: padding.top).isActive = true
}
if let leading = leading {
leadingAnchor.constraint(equalTo: leading, constant: padding.left).isActive = true
}
if let bottom = bottom {
bottomAnchor.constraint(equalTo: bottom, constant: padding.bottom).isActive = true
}
if let trailing = trailing {
trailingAnchor.constraint(equalTo: trailing, constant: padding.right).isActive = true
}
if size.width != 0 {
widthAnchor.constraint(equalToConstant: size.width).isActive = true
}
if 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))
推荐阅读
- html - gmail中CSS定位的问题,
- java - 我应该如何存储地图
> 数据进入数据库表? - android - 如何从适配器更改文本视图的颜色,textView的背景在drawable中定义
- javascript - Jquery Ajax Post Not Carrying Over Variables
- c# - Crazy behavior on method call: Method calls A () but calls property Z
- javascript - 调整函数大小并更改我的类的 css 值(在 javascript 中)
- android - 在布局中添加垂直按钮
- git - Create new Branch from Master branch but not full source code
- postscript - 为什么在递归 Postscript 过程中使用保存/恢复?
- javascript - Spotify 网络播放 SDK