嵌入在UIView

时间:2015-12-13 23:32:08

标签: uiview uiviewcontroller uiscrollview swift2 ios9

我有一个包含两个容器(UIViews)的scrollview。一个UIView包含一个正确显示的UITableView和另一个ViewController。 viewController有一个完全填充它的图像。但是,如果我设置了显示的背景颜色,则在添加视图控制器时。

enter image description here

//
//  SideBarViewController.swift
//  Sidebar
//
//  Created by Satyajit Sarangi on 12/10/15.
//  Copyright © 2015 Satyajit Sarangi. All rights reserved.
//

import UIKit

class SideBarViewController: UIViewController {

    // MARK: Properties
    var scrollView: UIScrollView!

    let menuTableView = UITableView()
    let sideBarContainerView = UIView()
    let mainContainerView = UIView()
    let testContainerView = UITableView()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        // Create a dictionary for the views for the visual format language
        let containerViewsDict = Dictionary(dictionaryLiteral: ("sidebar_container", sideBarContainerView), ("main_container", mainContainerView))
        // let scrollViewDict = Dictionary(dictionaryLiteral: ("scrollview", scrollView))

        // Create two container


        // Add the scroll view
        scrollView = UIScrollView(frame: view.frame)

        // Set Properties for scrollview
        scrollView.scrollEnabled = true
        scrollView.pagingEnabled = true
        scrollView.contentSize = CGSizeMake(view.frame.width, view.frame.height)

        // sideBarContainerView.backgroundColor = UIColor.yellowColor()
        scrollView.addSubview(sideBarContainerView)
        scrollView.addSubview(mainContainerView)

        setupMainContainerView()
        setupSideBarTableView()


        // Add the Scroll View
        view.addSubview(scrollView)

        let horizontalConstraints = NSLayoutConstraint.constraintsWithVisualFormat("H:|[sidebar_container]-5-[main_container]|", options: NSLayoutFormatOptions(rawValue:  0), metrics: nil, views: containerViewsDict)

        let scrollview_verticalConstraints1 = NSLayoutConstraint.constraintsWithVisualFormat("V:|[sidebar_container]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: containerViewsDict)

        let scrollview_verticalConstraints2 = NSLayoutConstraint.constraintsWithVisualFormat("V:|[main_container]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: containerViewsDict)

        sideBarContainerView.translatesAutoresizingMaskIntoConstraints = false
        mainContainerView.translatesAutoresizingMaskIntoConstraints = false
        scrollView.translatesAutoresizingMaskIntoConstraints = false

        scrollView.addConstraints(horizontalConstraints)
        scrollView.addConstraints(scrollview_verticalConstraints1)
        scrollView.addConstraints(scrollview_verticalConstraints2)

        // Add constraints for the Scroll View
        // let scrollview_constraint_v = NSLayoutConstraint.constraintsWithVisualFormat("V:|[scrollview]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: scrollViewDict)

        // let scrollview_constraint_h = NSLayoutConstraint.constraintsWithVisualFormat("H:|[scrollview]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: scrollViewDict)

        // scrollView.addConstraints(scrollview_constraint_v)
        // scrollView.addConstraints(scrollview_constraint_h)
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        scrollView.frame = view.bounds
        view.frame = CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height)

        scrollView.setContentOffset(CGPoint(x: 100, y: 0), animated: true)
    }


    // MARK: Set UI Properties
    func setupSideBarTableView() {
        menuTableView.frame = CGRectMake(0, 0, 200, scrollView.frame.height)
        menuTableView.contentSize = CGSizeMake(1000, 1000)
        menuTableView.backgroundColor = UIColor.redColor()
        sideBarContainerView.addSubview(menuTableView)

        let dict = Dictionary(dictionaryLiteral: ("menu_table", menuTableView))
        let constraint_h = NSLayoutConstraint.constraintsWithVisualFormat("H:|[menu_table]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: dict)
        let constraint_v = NSLayoutConstraint.constraintsWithVisualFormat("V:|[menu_table]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: dict)

        sideBarContainerView.frame = menuTableView.frame

        sideBarContainerView.addConstraints(constraint_h)
        sideBarContainerView.addConstraints(constraint_v)
    }

    func setupMainContainerView() {
//        testContainerView.frame = CGRectMake(0, 0, 320, scrollView.frame.height)
//        testContainerView.contentSize = CGSizeMake(1000, 1000)
//        testContainerView.backgroundColor = UIColor.yellowColor()

        let testContainerView = TestViewController()
        self.addChildViewController(testContainerView)

        mainContainerView.addSubview(testContainerView.view)

//        let dict = Dictionary(dictionaryLiteral: ("test_container", testContainerView))
//        let constraint_h = NSLayoutConstraint.constraintsWithVisualFormat("H:|[test_container]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: dict)
//        let constraint_v = NSLayoutConstraint.constraintsWithVisualFormat("V:|[test_container]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: dict)

        mainContainerView.frame = testContainerView.view.frame
//        mainContainerView.addConstraints(constraint_h)
//        mainContainerView.addConstraints(constraint_v)
        testContainerView.didMoveToParentViewController(self)
    }
}

视图控制器看起来像这样......并且它的代码粘贴在下面。可以看出,故事板具有图像,viewDidLoad将其设置为黄色。但是,如果我关闭背景颜色,则不会看到图像。

enter image description here

import UIKit

class TestViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.view.backgroundColor = UIColor.yellowColor()
    }
}

如何让TestControllerView显示其中的视图?

1 个答案:

答案 0 :(得分:1)

您正在实例化TestViewController,如此:

let testContainerView = TestViewController()

那不会使用故事板。如果你想使用故事板,你可以给那个场景一个" storyboard id"在Interface Builder中,然后您可以通过编程方式执行以下操作:

let testContainerView = storyboard?.instantiateViewControllerWithIdentifier("TestViewController storyboard id here")

顺便说一句,当你遇到这些问题时,从Xcode运行应用程序然后使用视图调试器是有用的:

enter image description here

这使您可以动态检查视图层次结构(这样您就可以确认某些内容是否真的丢失,或者可能是屏幕外或其他不可见)。

enter image description here