Ios 来自不同视图控制器的Swift访问约束发现为零
您好,我正在尝试从第二个视图控制器访问我的约束到初始视图控制器,但它总是给出一个错误,我从PageViewController类访问我的约束,以自动调整第二个页面的高度 展开可选值时发现无 Sh_Khan的答案是有效的,但当我想回到pageviewcontroller的第一页时,它并没有回到原来的大小。这是我试过的Ios 来自不同视图控制器的Swift访问约束发现为零,ios,swift,swift3,Ios,Swift,Swift3,您好,我正在尝试从第二个视图控制器访问我的约束到初始视图控制器,但它总是给出一个错误,我从PageViewController类访问我的约束,以自动调整第二个页面的高度 展开可选值时发现无 Sh_Khan的答案是有效的,但当我想回到pageviewcontroller的第一页时,它并没有回到原来的大小。这是我试过的 func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendi
func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) {
let pageContentViewController = pageViewController.viewControllers![0]
var page = orderedViewControllers.index(of: pageContentViewController)!
if(page == 1) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "MainNeedPage") as! NeedDetailsController
NeedDetailsController.status.viewstat = "true"
NeedDetailsController.status.bottomContainerHeightCon = CGFloat(50)
NeedDetailsController.status.containerHeightCon = CGFloat(30)
vc.view.layoutIfNeeded()
} else if(page == 0) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "MainNeedPage") as! NeedDetailsController;
NeedDetailsController.status.viewstat = "false"
NeedDetailsController.status.bottomContainerHeightCon = CGFloat(50)
NeedDetailsController.status.containerHeightCon = CGFloat(30)
vc.view.layoutSubviews()
}
}
在我的初始控制器上
override func viewDidLayoutSubviews() {
print("statusx: \(NeedDetailsController.status.viewstat)")
if(NeedDetailsController.status.viewstat == "true"){
print("statusy: \(NeedDetailsController.status.viewstat)")
NeedDetailsController.status.viewstat = "false"
self.bottomContainerHeight.constant = 0
self.containerHeight.constant = 0
UIView.animate(withDuration: 1) {
self.view.layoutIfNeeded()
}
} else if(NeedDetailsController.status.viewstat == "false") {
print("statusz: \(NeedDetailsController.status.viewstat)")
setupview()
NeedDetailsController.status.viewstat = "old"
self.bottomContainerHeight.constant = 50
self.containerHeight.constant = 30
UIView.animate(withDuration: 1) {
self.view.layoutIfNeeded()
}
}
}
无法访问其视图控制器未加载的属性
vc.bottomContainerHeight.constant = CGFloat(50)
vc.containerHeight.constant = CGFloat(30)
尝试向该类添加两个变量
var bottomContainerHeightCon:CGFloat!
var containerHeightCon:CGFloat!
让他们这样
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "MainNeedPage") as! NeedDetailsController;
vc.bottomContainerHeightCon = CGFloat(50)
vc.containerHeightCon = CGFloat(30)
和在ViewDidLayoutSubView中
将变量应用于约束
override func viewDidLayoutSubviews
{
if(once){
once = false
self.bottomContainerHeight = bottomContainerHeightCon
self.containerHeight = containerHeightCon
self.view.layoutIfNeeded()
}
}
您无法使用
vc
执行任何操作,因为未显示,其所有约束都未初始化。如果您可以帮助我,请查看我的编辑。谢谢。您好,这解决了错误,但我的pageviewcontroller所在的容器视图仍然没有根据给定的高度变量更改其高度。您好,我编辑了我的帖子,遇到了一个新问题。不知道发生了什么事情Git似乎与bundle identifier MainNeedPage完全相同,我正在尝试将其还原为原始大小,例如,我向右滑动底部容器,容器将增大其大小,向左滑动后,底部容器和容器的高度应减小。如果您明白我的意思,请设置NeedDetailsController.status.viewstat=“false”。在第页中为true而不是false=0
override func viewDidLayoutSubviews
{
if(once){
once = false
self.bottomContainerHeight = bottomContainerHeightCon
self.containerHeight = containerHeightCon
self.view.layoutIfNeeded()
}
}