Childview不会出现在父视图中

时间:2016-07-07 08:23:25

标签: ios

我尝试使用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
}

这里是子视图控制器的外观

enter image description here

这是它在模拟器中的外观(白色屏幕,没有标签)

enter image description here

如果我删除

subViewTwo?.frame = subViewTwo!.superview!.bounds
subViewTwo?.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]

我最终得到了这个

enter image description here

1 个答案:

答案 0 :(得分:1)

希望这能提供一些帮助,我认为使用容器视图更容易。用你的可切换视图填充它,这将通过设置activeViewController来完成,这将删除非活动视图&显示填充containerView框架的其他视图

Button

以下是要使用的UI当用户从段视图中选择项目时,您应该更改活动视图以加载另一个视图 enter image description here