ios - 是否可以在 ViewController 文件之外设置约束和对象?

标签 ios swift xcode

我是不使用 Storyboard的编程新手。我想要做的是移动负责初始化 UIView 和约束的代码,从那里我也想把它们放在另一个文件中。

理想情况下,我想接一个电话

view.addSubview(loginControllerObjects(frame: view.frame))

从我的 viewDidLoad() 调用单独的文件并在适当的位置设置对象,以使我的 ViewController 保持整洁。

我已经解决了大部分问题,但似乎我的函数 setUpInputContainer() 给我“以 NSException 类型的未捕获异常终止”错误。建议这是由于它在不同 View 层次结构中的 anchor 引用项。有谁知道如何解决这个问题?

应用委托(delegate)

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    window = UIWindow(frame: UIScreen.main.bounds)

    let homeViewController = ViewController()
    window?.rootViewController = UINavigationController(rootViewController: homeViewController)
    window!.makeKeyAndVisible()

    return true

}

View Controller

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationController?.isNavigationBarHidden = true
    view.addSubview(loginControllerContraints(frame: view.frame))
}

}

extension UIColor {
    convenience init(r: CGFloat, g: CGFloat, b: CGFloat) {
        self.init(red: r/255, green: g/255, blue: b/255, alpha: 1)
    }
}

LoginControllerContraints

class loginControllerContraints: UIView {

override init(frame: CGRect) {
    super.init(frame: frame)

    backgroundColor = UIColor(r: 61, g: 91, b: 151)
    setUpInputContainer()
    addSubview(inputsContainerView)

}

let inputsContainerView: UIView = {
    let view = UIView()
    let red = UIColor.red
    view.backgroundColor = UIColor.white
    view.translatesAutoresizingMaskIntoConstraints = false
    view.layer.cornerRadius = 5
    view.layer.backgroundColor = red.cgColor
    view.layer.masksToBounds = true
    return view
}()

var inputsContainerViewHeightAnchor: NSLayoutConstraint?

func setUpInputContainer() {
    //Needs x, y, height, and width constraints for INPUTCONTAINER
    inputsContainerView.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
    inputsContainerView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
    inputsContainerView.widthAnchor.constraint(equalTo: widthAnchor, constant: -24).isActive = true
    inputsContainerViewHeightAnchor = inputsContainerView.heightAnchor.constraint(equalToConstant: 150)
    inputsContainerViewHeightAnchor?.isActive = true


}

//Required do not touch
required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}


}

最佳答案

问题是您在将 inputsContainerView 添加到 loginControllerContraints 之前调用了 setUpInputContainer

要修复它,请在添加 inputsContainerView 的任何约束之前将 inputsContainerView 添加到 loginControllerContraints 。将 loginControllerContraintsinit 方法替换为以下代码。

override init(frame: CGRect) {
    super.init(frame: frame)

    backgroundColor = UIColor(r: 61, g: 91, b: 151)
    addSubview(inputsContainerView)
    setUpInputContainer()
}

关于ios - 是否可以在 ViewController 文件之外设置约束和对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47107361/

相关文章:

iOS swift 4 OAuth

ios - 禁用 UIAlertController 框架外的触摸以取消

swift - 为什么我的搜索栏仅在我使用时才停留在顶部?

ios - Double 值无法转换为 Int64,因为它是无限的或 NaN

ios - 如何重置 Root View Controller

ios - 线程正在执行行,但无效

ios - 应用程序屏幕周围的黑框是什么?

ios - 如何在 swift header 中传递 OAuth。 Twitter Api(获取趋势标签)

ios - UIPopOverPresentationController 在presentationTransitionWillBegin 上崩溃

ios - 从另一个文件中的 SKScene 中删除 UIView