我尝试使用3个子视图构建控制器,可以通过分段控制进行切换。问题是,不是3个控制器中的2个的内容,而是看到白色屏幕。
代码如下
@IBAction func segmentedControllSelected(sender: UISegmentedControl) {
switch sender.selectedSegmentIndex
{
case 0:
self.parentView.addSubview(subViewOne!)
subViewOne?.frame = subViewOne!.superview!.bounds
subViewOne?.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
subViewTwo?.removeFromSuperview()
subViewThree?.removeFromSuperview()
case 1:
self.parentView.addSubview(subViewTwo!)
subViewTwo?.frame = subViewTwo!.superview!.bounds
subViewTwo?.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
subViewOne?.removeFromSuperview()
subViewThree?.removeFromSuperview()
case 2:
self.parentView.addSubview(subViewThree!)
subViewThree?.frame = subViewThree!.superview!.bounds
subViewThree?.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
print("parentView frame is \(parentView.frame.height)")
print("scrollView contentSize is \(scrollView.contentSize.height)")
print("subViewThree frame is \(subViewThree?.frame.height)")
subViewThree?.frame.size.height = 2000
print("subViewThree frame is \(subViewThree?.frame.height)")
subViewOne?.removeFromSuperview()
subViewTwo?.removeFromSuperview()
default:
break;
}
}
override func viewDidLoad() {
super.viewDidLoad()
segmentedControllPositionY = segmentedControll.frame.origin.y
scrollView.delegate = self
gameInfoMainViewController = UIStoryboard(name: "GameInfo", bundle: nil).instantiateViewControllerWithIdentifier("GameInfoMainViewController") as? GameInfoMainViewController
gameInfoMembersViewController = UIStoryboard(name: "GameInfo", bundle: nil).instantiateViewControllerWithIdentifier("GameInfoMembersViewController") as? GameInfoMembersViewController
gameInfoTeamsViewController = UIStoryboard(name: "GameInfo", bundle: nil).instantiateViewControllerWithIdentifier("GameInfoTeamsViewController") as? GameInfoTeamsViewController
gameInfoTeamsViewController!.delegate = self
self.addChildViewController(gameInfoMainViewController!)
self.addChildViewController(gameInfoMembersViewController!)
self.addChildViewController(gameInfoTeamsViewController!)
subViewOne = gameInfoMainViewController!.view
subViewTwo = gameInfoMembersViewController!.view
subViewThree = gameInfoTeamsViewController!.view
}
override func viewWillAppear(animated: Bool) {
self.parentView.addSubview(subViewOne!)
subViewOne!.frame = subViewOne!.superview!.bounds
func scrollViewDidScroll(scrollView: UIScrollView) {
if scrollView.contentOffset.y > segmentedControllPositionY! - 40 {
segmentedControllTopConstraint.constant = scrollView.contentOffset.y + 40
} else {
segmentedControllTopConstraint.constant = segmentedControllPositionY!
}
}
func passHeight(height:CGFloat) { // delegate method which is called in controller with tableview and passes height of tableview
tableViewHeight = height
parentView.frame.size.height = tableViewHeight!
parentView.layoutIfNeeded()
scrollView.contentSize.height = tableViewHeight! + segmentedControll!.frame.height + headerView.frame.size.height
}
这里是子视图控制器的外观
这是它在模拟器中的外观(白色屏幕,没有标签)
如果我删除
subViewTwo?.frame = subViewTwo!.superview!.bounds
subViewTwo?.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
我最终得到了这个