iOS 11 Swift 4将ChildViewController添加到UISCrollView不会占用全高

时间:2018-05-28 20:30:10

标签: swift uiscrollview autolayout ios11 childviewcontroller

我正试图为我的应用做一个类似于布局的喜欢。

我有一个带有scrollview的主viewcontroller。我添加了3个viewcontrollers作为scrollview的子项。

在iPhone 5C上的iOS 10上一切正常,但在iPhone 6S的iOS 11上,子视图控制器没有达到全高

橙色部分是子视图控制器,蓝色部分是滚动视图

这是我的主要VC代码:

    let trendView = TrendViewController(nibName: "TrendViewController", bundle: Bundle.main)
    let homeView = HomeViewController(nibName: "HomeViewController", bundle: Bundle.main)
    let searchView = SearchViewController(nibName: "SearchViewController", bundle: Bundle.main)

    trendView.view.translatesAutoresizingMaskIntoConstraints = false
    homeView.view.translatesAutoresizingMaskIntoConstraints = false
    searchView.view.translatesAutoresizingMaskIntoConstraints = false

    self.addChildViewController(trendView)
    self.mainScrollView.addSubview(trendView.view)

    trendView.didMove(toParentViewController: self)

    self.addChildViewController(homeView)
    self.mainScrollView.addSubview(homeView.view)
    homeView.didMove(toParentViewController: self)

    self.addChildViewController(searchView)
    self.mainScrollView.addSubview(searchView.view)
    searchView.didMove(toParentViewController: self)

    var homeFrame = homeView.view.frame
    homeFrame.origin.x = self.view.frame.width
    homeView.view.frame = homeFrame

    var searchFrame = searchView.view.frame
    searchFrame.origin.x = self.view.frame.width * 2
    searchView.view.frame = searchFrame

    self.mainScrollView.contentSize = CGSize(width: self.view.frame.width * 3, height: self.view.frame.height)
    self.mainScrollView.contentOffset = CGPoint(x: self.view.frame.width, y: 0)

我加载了一个没有安全区域的旧项目,我没有问题...不知道是否存在此问题的链接

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

我知道我没有指定任何高度,但在我发现的所有例子中,他们从未为子vc视图指定高度。

我刚刚添加了这些内容:

trendView.view.leadingAnchor.constraint(equalTo: self.mainScrollView.leadingAnchor).isActive = true
        trendView.view.topAnchor.constraint(equalTo: self.mainScrollView.topAnchor).isActive = true
        trendView.view.widthAnchor.constraint(equalTo: self.view.widthAnchor).isActive = true
        trendView.view.heightAnchor.constraint(equalTo: self.view.heightAnchor).isActive = true

        homeView.view.leadingAnchor.constraint(equalTo: trendView.view.trailingAnchor).isActive = true
        homeView.view.topAnchor.constraint(equalTo: self.mainScrollView.topAnchor).isActive = true
        homeView.view.widthAnchor.constraint(equalTo: self.view.widthAnchor).isActive = true
        homeView.view.heightAnchor.constraint(equalTo: self.view.heightAnchor).isActive = true

        searchView.view.leadingAnchor.constraint(equalTo: homeView.view.trailingAnchor).isActive = true
        searchView.view.topAnchor.constraint(equalTo: self.mainScrollView.topAnchor).isActive = true
        searchView.view.widthAnchor.constraint(equalTo: self.view.widthAnchor).isActive = true
        searchView.view.heightAnchor.constraint(equalTo: self.view.heightAnchor).isActive = true

现在它有效:) 感谢