Ios 添加文本字段时无法激活带有锚定的约束

Ios 添加文本字段时无法激活带有锚定的约束,ios,swift,constraints,nslayoutconstraint,appdelegate,Ios,Swift,Constraints,Nslayoutconstraint,Appdelegate,不久前,我开始通过故事板创建屏幕 最近我被说服以编程的方式来做 目前我遇到以下错误: 由于未捕获的异常“NSGenericeException”而终止应用程序,原因:“无法使用锚激活约束,并且因为它们没有共同的祖先。”。约束或其锚定是否引用不同视图层次中的项?这是违法的。” 我创建了一个函数,用于在Extensions.swift中锚定我的元素: import UIKit extension UIView { func anchor(top: NSLayoutYAxisAnchor?

不久前,我开始通过故事板创建屏幕

最近我被说服以编程的方式来做

目前我遇到以下错误:

由于未捕获的异常“NSGenericeException”而终止应用程序,原因:“无法使用锚激活约束,并且因为它们没有共同的祖先。”。约束或其锚定是否引用不同视图层次中的项?这是违法的。”

我创建了一个函数,用于在Extensions.swift中锚定我的元素:

import UIKit

extension UIView {

    func anchor(top: NSLayoutYAxisAnchor?, left: NSLayoutXAxisAnchor?, bottom: NSLayoutYAxisAnchor?, right: NSLayoutXAxisAnchor?, paddingTop: CGFloat, paddingLeft: CGFloat, paddingBottom: CGFloat, paddingRight: CGFloat, width: CGFloat, height: CGFloat) {

        translatesAutoresizingMaskIntoConstraints = false

        if let top = top {
            self.topAnchor.constraint(equalTo: top, constant: paddingTop).isActive = true
        }

        if let left = left {
            self.leftAnchor.constraint(equalTo: left, constant: paddingLeft).isActive = true
        }

        if let bottom = bottom {
            self.bottomAnchor.constraint(equalTo: bottom, constant: -paddingBottom).isActive = true
        }

        if let right = right {
            self.rightAnchor.constraint(equalTo: right, constant: -paddingRight).isActive = true
        }

        if width != 0 {
            widthAnchor.constraint(equalToConstant: width).isActive = true
        }

        if height != 0 {
            heightAnchor.constraint(equalToConstant: height).isActive = true
        }


    }
}
此外,我还删除了Info.plist中的应用程序场景清单

我希望我的项目能够支持从13.0开始的iOS版本。 然而,我不想使用新的框架SwiftUI

是否有需要注意的特定事项,例如场景代理


提前谢谢你

您需要更改此设置

var emailTextField: UITextField {

    let tf = UITextField()
    tf.placeholder = "email";
    tf.backgroundColor = UIColor(white: 0, alpha: 0.03)
    tf.borderStyle = .roundedRect
    tf.font = UIFont.systemFont(ofSize: 14)

    return tf
}
进入这个

var emailTextField: UITextField = {

    let tf = UITextField()
    tf.placeholder = "email";
    tf.backgroundColor = UIColor(white: 0, alpha: 0.03)
    tf.borderStyle = .roundedRect
    tf.font = UIFont.systemFont(ofSize: 14)

    return tf
}()

在当前版本中,emailTextField是computed属性,因此,每次访问它时,编译器都会创建一个新实例,它的工作方式类似于函数,而不是变量。

Textfield在添加其约束时无法获取其父视图。下面的代码适用于我。我将当前视图作为参数传递给anchor方法,并将emailTextField作为子视图添加到其中

class ViewController: UIViewController {

  var emailTextField:UITextField = {
     let tf = UITextField()
     tf.placeholder = "email";
     tf.backgroundColor = UIColor(white: 0, alpha: 0.03)
     tf.borderStyle = .roundedRect
     tf.font = UIFont.systemFont(ofSize: 14)
     return tf
  }()

  override func viewDidLoad() {
      super.viewDidLoad()

      view.backgroundColor = .white
      emailTextField.anchor(top: view.safeAreaLayoutGuide.topAnchor, left: view.leftAnchor, bottom: nil, right: view.rightAnchor, paddingTop: 40, paddingLeft: 20, paddingBottom: 0, paddingRight: 20, width: 0, height: 40, view:view)
   }


 }


extension UIView {

     func anchor(top: NSLayoutYAxisAnchor?, left: NSLayoutXAxisAnchor?, bottom: NSLayoutYAxisAnchor?, right: NSLayoutXAxisAnchor?, paddingTop: CGFloat, paddingLeft: CGFloat, paddingBottom: CGFloat, paddingRight: CGFloat, width: CGFloat, height: CGFloat, view:UIView) {

        translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(self)
        if let top = top {
           self.topAnchor.constraint(equalTo: top, constant: paddingTop).isActive = true
        }

        if let left = left {
             self.leftAnchor.constraint(equalTo: left, constant: paddingLeft).isActive = true
        }

         if let bottom = bottom {
             self.bottomAnchor.constraint(equalTo: bottom, constant: -paddingBottom).isActive = true
         }

         if let right = right {
            self.rightAnchor.constraint(equalTo: right, constant: -paddingRight).isActive = true
        }

         if width != 0 {
            widthAnchor.constraint(equalToConstant: width).isActive = true
         }

         if height != 0 {
             heightAnchor.constraint(equalToConstant: height).isActive = true
         }
      }
}

谢谢你!但是:现在只显示一个黑色屏幕而不是白色背景,文本字段检查其帧高度可以为0,并且不要忘记将背景设置为UITextField本身。
class ViewController: UIViewController {

  var emailTextField:UITextField = {
     let tf = UITextField()
     tf.placeholder = "email";
     tf.backgroundColor = UIColor(white: 0, alpha: 0.03)
     tf.borderStyle = .roundedRect
     tf.font = UIFont.systemFont(ofSize: 14)
     return tf
  }()

  override func viewDidLoad() {
      super.viewDidLoad()

      view.backgroundColor = .white
      emailTextField.anchor(top: view.safeAreaLayoutGuide.topAnchor, left: view.leftAnchor, bottom: nil, right: view.rightAnchor, paddingTop: 40, paddingLeft: 20, paddingBottom: 0, paddingRight: 20, width: 0, height: 40, view:view)
   }


 }


extension UIView {

     func anchor(top: NSLayoutYAxisAnchor?, left: NSLayoutXAxisAnchor?, bottom: NSLayoutYAxisAnchor?, right: NSLayoutXAxisAnchor?, paddingTop: CGFloat, paddingLeft: CGFloat, paddingBottom: CGFloat, paddingRight: CGFloat, width: CGFloat, height: CGFloat, view:UIView) {

        translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(self)
        if let top = top {
           self.topAnchor.constraint(equalTo: top, constant: paddingTop).isActive = true
        }

        if let left = left {
             self.leftAnchor.constraint(equalTo: left, constant: paddingLeft).isActive = true
        }

         if let bottom = bottom {
             self.bottomAnchor.constraint(equalTo: bottom, constant: -paddingBottom).isActive = true
         }

         if let right = right {
            self.rightAnchor.constraint(equalTo: right, constant: -paddingRight).isActive = true
        }

         if width != 0 {
            widthAnchor.constraint(equalToConstant: width).isActive = true
         }

         if height != 0 {
             heightAnchor.constraint(equalToConstant: height).isActive = true
         }
      }
}