如何修复当前页面不显示真? [迅速]
Posted
技术标签:
【中文标题】如何修复当前页面不显示真? [迅速]【英文标题】:How to fix current page does not display true? [Swift] 【发布时间】:2016-06-06 04:24:09 【问题描述】:为什么我在页面控件中的当前页面没有显示正确的输出?
第 1 页和第 2 页显示在一个点上?图片在这里: http://i.stack.imgur.com/498ap.png, http://i.stack.imgur.com/41kdg.png
最后一页是第 6 页显示在第 5 点,不是最后一个点吗?图片:http://i.stack.imgur.com/NP9u1.png
我的代码在这里:
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var pageControl: UIPageControl!
let totalPages = 6
let sampleBGColors: Array<UIColor> = [UIColor.redColor(), UIColor.yellowColor(), UIColor.greenColor(), UIColor.magentaColor(), UIColor.orangeColor(), UIColor.lightGrayColor()] @IBOutlet weak var scrollView: UIScrollView!
override func viewDidAppear(animated: Bool)
super.viewDidAppear(animated)
configureScrollView()
configurePageControl()
func configureScrollView()
// Enable paging.
scrollView.pagingEnabled = true
// Set the following flag values.
scrollView.showsHorizontalScrollIndicator = false
scrollView.showsVerticalScrollIndicator = false
scrollView.scrollsToTop = false
// Set the scrollview content size.
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * CGFloat(totalPages), scrollView.frame.size.height)
// Set self as the delegate of the scrollview.
scrollView.delegate = self
// Load the TestView view from the TestView.xib file and configure it properly.
for i in 0 ..< totalPages
// Load the TestView view.
let testView = NSBundle.mainBundle().loadNibNamed("TestView", owner: self, options: nil)[0] as! UIView
// Set its frame and the background color.
testView.frame = CGRectMake(CGFloat(i) * scrollView.frame.size.width, scrollView.frame.origin.y, scrollView.frame.size.width, scrollView.frame.size.height)
testView.backgroundColor = sampleBGColors[i]
// Set the proper message to the test view's label.
let label = testView.viewWithTag(1) as! UILabel
label.text = "Page #\(i + 1)"
// Add the test view as a subview to the scrollview.
scrollView.addSubview(testView)
func configurePageControl()
// Set the total pages to the page control.
pageControl.numberOfPages = totalPages
// Set the initial page.
pageControl.currentPage = 0
// MARK: UIScrollViewDelegate method implementation
func scrollViewDidScroll(scrollView: UIScrollView)
// Calculate the new page index depending on the content offset.
let currentPage = floor(scrollView.contentOffset.x / UIScreen.mainScreen().bounds.size.width);
// Set the new page index to the page control.
pageControl.currentPage = Int(currentPage)
// MARK: IBAction method implementation
@IBAction func changePage(sender: AnyObject)
// Calculate the frame that should scroll to based on the page control current page.
var newFrame = scrollView.frame
newFrame.origin.x = newFrame.size.width * CGFloat(pageControl.currentPage)
scrollView.scrollRectToVisible(newFrame, animated: true)
请帮帮我!谢谢。
对不起,我的英语不好。
【问题讨论】:
页面控件从第0页开始,页数=6,数组索引5为灰色,需要修改numberOfPage 【参考方案1】:更改 UIScrollViewDelegate 的实现 scrollViewDidEndScrollingAnimation 和 scrollViewDidEndDecelerating 中的 pageControl.currentPage,我改进了使用 scrollView 的宽度而不是屏幕宽度的计算:
// MARK: UIScrollViewDelegate method implementation
func scrollViewDidEndScrollingAnimation(scrollView: UIScrollView)
// Calculate the new page index depending on the content offset.
let currentPage = floor(scrollView.contentOffset.x / scrollView.bounds.size.width)
// Set the new page index to the page control.
pageControl.currentPage = Int(currentPage)
func scrollViewDidEndDecelerating(scrollView: UIScrollView)
scrollViewDidEndScrollingAnimation(scrollView)
【讨论】:
以上是关于如何修复当前页面不显示真? [迅速]的主要内容,如果未能解决你的问题,请参考以下文章
SpringMVCMacBook IDEA2020.2版本如何修复HelloSpringMVC项目运行时页面显示404的问题