类似Twitter的UIScrollView,将ViewControllers作为页面

时间:2015-02-06 06:10:52

标签: ios uitableview swift uiscrollview

Video of the issue!

我的意思是类似Twitter的UIScrollView:

Twitter Image

我基本上有它工作,但我有这个小的明显问题,我不知道它来自哪里。我已经检查了我的两个视图控制器的所有约束和值,但有些东西已经关闭。

简而言之,

创建NavBar的代码,然后将两个ViewControllers并排填充:

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)
}

我查看了我的故事板,两个ViewControllers在约束方面看起来都相同。

我知道这是一个问题,因为两个ViewControllers都由UITableViews填充。当我滚动浏览SecondViewController时,它完美地工作。当我滚动FeedViewController时,顶部有一个小的空白区域,我似乎无法摆脱它,它显示文本在那里切断。我已经坚持了很长时间,如果还有其他需要的信息,我很乐意提供。

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

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

1 个答案:

答案 0 :(得分:0)

所以,我认为实现此问题的每个人都会遇到这个问题。问题不在于第一个ViewController。只需将约束从顶部调整为44。问题出在第二个ViewController上,当你理解它们是如何工作时,它不是一个问题。从技术上讲,它偏向一边,因此它的顶部约束不会粘附到导航栏上,所以你所拥有的是一个约束--20。根据你最初设置约束的方式,它可以给你这个看似问题。 / p>

但基本上,任何人都会遇到这个问题。

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