带有 ViewControllers 作为页面的类似 Twitter 的 UIScrollView

Twitter-like UIScrollView with ViewControllers as pages

Video of the issue!

我所说的类似 Twitter 的 UIScrollView 的例子:

我基本上可以正常工作,但我有一个明显的小问题,我不知道它是从哪里来的。我已经检查了我的两个视图控制器的所有约束和值,但有些地方不对劲。

简而言之,

创建 NavBar 然后用两个 ViewController 并排填充它的代码:

override func viewDidLoad() {
    super.viewDidLoad()

    var navBar: UINavigationBar = UINavigationBar(frame: CGRectMake(0, 0, self.view.bounds.width, 64))

    navBar.barTintColor = UIColor.blackColor()
    navBar.translucent = false

    //Creating some shorthand for these values
    var wBounds = self.view.bounds.width
    var hBounds = self.view.bounds.height

    // This houses all of the UIViews / content
    scrollView = UIScrollView()
    scrollView.backgroundColor = UIColor.clearColor()
    scrollView.frame = self.view.frame
    scrollView.pagingEnabled = true
    scrollView.showsHorizontalScrollIndicator = false
    scrollView.delegate = self
    scrollView.bounces = false
    self.view.addSubview(scrollView)

    self.scrollView.contentSize = CGSize(width: self.view.bounds.size.width * 2, height: hBounds)

    //Putting a subview in the navigationbar to hold the titles and page dots
    navbarView = UIView()

    //Paging control is added to a subview in the uinavigationcontroller
    pageControl = UIPageControl()
    pageControl.frame = CGRect(x: 0, y: 35, width: 0, height: 0)
    pageControl.pageIndicatorTintColor = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 0.3)
    pageControl.currentPageIndicatorTintColor = UIColor.whiteColor()
    pageControl.numberOfPages = 2
    pageControl.currentPage = 0
    self.navbarView.addSubview(pageControl)


    //Titles for the nav controller (also added to a subview in the uinavigationcontroller)
    //Setting size for the titles. FYI changing width will break the paging fades/movement
    navTitleLabel1 = UILabel()
    navTitleLabel1.frame = CGRect(x: 0, y: 8, width: wBounds, height: 20)
    navTitleLabel1.textColor = UIColor.whiteColor()
    navTitleLabel1.textAlignment = NSTextAlignment.Center
    navTitleLabel1.text = "Title 1"
    self.navbarView.addSubview(navTitleLabel1)

    navTitleLabel2 = UILabel()
    navTitleLabel2.alpha = 0.0
    navTitleLabel2.frame = CGRect(x: 100, y: 8, width: wBounds, height: 20)
    navTitleLabel2.textColor = UIColor.whiteColor()
    navTitleLabel2.textAlignment = NSTextAlignment.Center
    navTitleLabel2.text = "Title 2"
    self.navbarView.addSubview(navTitleLabel2)

    //Views for the scrolling view
    //This is where the content of your views goes (or you can subclass these and add them to ScrollView)

    feedViewController = storyboard?.instantiateViewControllerWithIdentifier("FeedController") as FeedViewController

    view1 = feedViewController.view

    addChildViewController(feedViewController)
    feedViewController.didMoveToParentViewController(self)

    view1.frame = CGRectMake(0, 0, wBounds, hBounds)
    self.scrollView.addSubview(view1)
    self.scrollView.bringSubviewToFront(view1)

    //Notice the x position increases per number of views

    secondViewController = storyboard?.instantiateViewControllerWithIdentifier("SecondController") as SecondViewController

    view2 = secondViewController.view

    addChildViewController(secondViewController)
    secondViewController.didMoveToParentViewController(self)

    view2.frame = CGRectMake(wBounds, 0, wBounds, hBounds)
    self.scrollView.addSubview(view2)
    self.scrollView.bringSubviewToFront(view2)

    navBar.addSubview(navbarView)
    self.view.addSubview(navBar)
}

我查看了我的情节提要,ViewController 在约束方面似乎完全相同。

我知道这是个问题,因为两个 ViewController 都由 UITableViews 填充。当我滚动 SecondViewController 时,它工作得很好。当我滚动 FeedViewController 时,顶部有一个白色的小 space,我似乎无法摆脱它,它显示文本在那里被截断了。我已经坚持了很长时间,如果需要任何其他信息,我很乐意提供。

编辑:包含问题的视频。如果可以的话,我现在就悬赏这个问题。我不明白原因

更新:交换两个 ViewController 位置后,我注意到问题不在于任何一个 ViewController。问题在于第 1 页设置得较低。交换时,原来的 SecondViewController 也经历了相同的行为

所以,我认为实施此 运行 的每个人都会在某个时候遇到这个问题。问题不在于第一个 ViewController。只需将约束从顶部调整为 44。问题出在第二个 ViewController 上,当您了解它们的工作原理时,这就不是什么大问题了。从技术上讲,它偏向一边,因此它的顶部约束不遵守导航栏,所以你有一个约束 - 20。这取决于你最初如何放置你的约束,可以给你这个看似问题。

但基本上,任何人和每个人在实施时都会 运行 关注这个问题。

TL;DR:为了使一切无缝,您的第二个、第三个、第四个、第五个等页面视图控制器需要一个约束 + 20 个您的第一个视图控制器。在我的设置中,我对第一个视图控制器使用 44 的约束,因此对第二个视图控制器使用 64