Ios 是否可以在ViewController文件之外设置约束和对象?
我不熟悉不使用故事板的编程。我试图做的是移动负责初始化UIView和约束的代码,从那里我也想把它们放在另一个文件中 理想情况下,我想要一个电话 view.addSubview(loginControllerObjects(frame:view.frame)) 从my viewDidLoad()调用该单独的文件,并在适当的位置设置对象,以保持我的ViewController不混乱 我已经解决了大部分问题,但似乎我的函数setUpInputContainer()给出了“以NSException类型的未捕获异常终止”错误。建议使用its,因为其锚定了不同视图层次结构中的引用项。有人知道如何回避这个问题吗 应用程序代理Ios 是否可以在ViewController文件之外设置约束和对象?,ios,swift,xcode,Ios,Swift,Xcode,我不熟悉不使用故事板的编程。我试图做的是移动负责初始化UIView和约束的代码,从那里我也想把它们放在另一个文件中 理想情况下,我想要一个电话 view.addSubview(loginControllerObjects(frame:view.frame)) 从my viewDidLoad()调用该单独的文件,并在适当的位置设置对象,以保持我的ViewController不混乱 我已经解决了大部分问题,但似乎我的函数setUpInputContainer()给出了“以NSException类型的
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
}
视图控制器
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)
}
}
LoginControllerContracts
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
添加到LoginControllerContrains
之前调用setUpInputContainer
要修复此问题,请在添加inputsContainerView
的任何约束之前,将inputsContainerView
添加到LoginControllerContracts
。将logincontrollercontracts
的init
方法替换为以下代码
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = UIColor(r: 61, g: 91, b: 151)
addSubview(inputsContainerView)
setUpInputContainer()
}
问题是在将
inputsContainerView
添加到LoginControllerContrains
之前调用setUpInputContainer
要修复此问题,请在添加inputsContainerView
的任何约束之前,将inputsContainerView
添加到LoginControllerContracts
。将logincontrollercontracts
的init
方法替换为以下代码
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = UIColor(r: 61, g: 91, b: 151)
addSubview(inputsContainerView)
setUpInputContainer()
}
哇,真不敢相信我错过了。感谢您的明确回复。欢迎@ChandlerLong;)哇,真不敢相信我错过了。感谢您的明确回复。欢迎@ChandlerLong;)