首页 > 解决方案 > 如何正确地向我的 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
        }
    }
}

标签: swiftxcodeconstraints

解决方案


您需要移除底部锚点

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

推荐阅读