Ios 类似Twitter的UIScrollView,以ViewController作为页面
我所说的类似Twitter的UIScrollView的示例: 我基本上能让它工作,但我有一个小问题,我不知道它是从哪里来的。我已经检查了我的两个视图控制器的所有约束和值,但是有些东西关闭了 总之, 创建导航栏,然后并排使用两个ViewController填充导航栏的代码:Ios 类似Twitter的UIScrollView,以ViewController作为页面,ios,uitableview,swift,uiscrollview,Ios,Uitableview,Swift,Uiscrollview,我所说的类似Twitter的UIScrollView的示例: 我基本上能让它工作,但我有一个小问题,我不知道它是从哪里来的。我已经检查了我的两个视图控制器的所有约束和值,但是有些东西关闭了 总之, 创建导航栏,然后并排使用两个ViewController填充导航栏的代码: override func viewDidLoad() { super.viewDidLoad() var navBar: UINavigationBar = UINavigationBar(frame: C
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)
}
我看过我的故事板,两个视图控制器在约束方面似乎是相同的
我知道这是一个问题,因为两个视图控制器都由UITableView填充。当我在SecondViewController中滚动时,它工作得非常好。当我在FeedViewController中滚动时,顶部有一个小的空白,我似乎无法消除,它显示文本在那里被截断。我已经在这个问题上纠缠了很长时间,如果还有其他需要的信息,我很乐意提供
编辑:包含该问题的视频。如果可以,我现在就回答这个问题。我不明白原因
更新:在交换了两个ViewController位置后,我注意到问题不在于两个ViewController。问题在于将第1页设置得更低。交换时,原来的SecondViewController也经历了相同的行为,因此,我认为每个实现此功能的人都会在某个时候遇到此问题。问题不在于第一个ViewController。只需将约束从顶部调整为44即可。问题在于第二个ViewController,当您了解它们的工作原理时,问题就不那么严重了。从技术上讲,它是偏侧的,因此它的顶部约束不会附着在导航栏上,因此您拥有的是约束-20。这取决于您最初放置约束的方式,可能会给您带来这个表面上的问题 但基本上,任何人和每个人都会在实施时遇到这个问题
TL;DR:为了使一切都无缝,您的第二、第三、第四、第五等页面视图控制器需要第一个视图控制器的约束+20。在我的设置中,第一个视图控制器使用44的约束,第二个视图控制器使用64的约束也许我不完全理解如何将子视图添加到视图中。我想我总是可以将左侧的约束上移20,但我认为我不必这样做。这是所有这一切令人费解的部分