我跟着this tutorial,特别是在“使用UIScrollView进行分页”部分下创建水平滚动。目前,我已将其设置为使图像覆盖整个画面/屏幕。
我正在尝试在导航栏上添加一个简单的UIBarButtonItem
(使用Storyboard),以尝试构建一个滑出式菜单。但是,该按钮不会显示。此外,即使我尝试self.navigationController?.navigationBarHidden = false
,导航栏也不会显示。
我当前的视图控制器是这样编写的。
class HomeViewController: UIViewController, UIScrollViewDelegate {
@IBOutlet weak var pagedScrollView: UIScrollView!
@IBOutlet weak var pageControl: UIPageControl!
@IBOutlet weak var hamburger: UIBarButtonItem! // the button
var pageImages: [UIImage] = []
var pageViews: [UIImageView?] = []
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.toolbarHidden = false
hamburger.target = self.revealViewController()
hamburger.action = Selector("revealToggle:")
}
override func prefersStatusBarHidden() -> Bool {
return true
}
override func viewDidAppear(animated: Bool) {
let weatherData = WeatherDataLibrary().library
pageImages = [
UIImage(named: weatherData[0]["image"] as! String!)!,
UIImage(named: weatherData[1]["image"] as! String!)!,
UIImage(named: weatherData[2]["image"] as! String!)!,
UIImage(named: weatherData[3]["image"] as! String!)!,
UIImage(named: weatherData[4]["image"] as! String!)!,
UIImage(named: weatherData[5]["image"] as! String!)!,
]
let pageCount = weatherData.count
pageControl.currentPage = 0
pageControl.numberOfPages = pageCount
for _ in 0..<pageCount {
pageViews.append(nil)
}
let pagesScrollViewSize = pagedScrollView.frame.size
pagedScrollView.contentSize = CGSize(width: pagesScrollViewSize.width * CGFloat(pageImages.count),
height: pagesScrollViewSize.height)
loadVisiblePages()
}
func loadPage(page: Int) {
if page < 0 || page >= pageImages.count {
// If it's outside the range of what you have to display, then do nothing
return
}
// 1
if let pageView = pageViews[page] {
// Do nothing. The view is already loaded.
} else {
// 2
var frame = pagedScrollView.bounds
frame.origin.x = frame.size.width * CGFloat(page)
frame.origin.y = 0.0
// 3
let newPageView = UIImageView(image: pageImages[page])
newPageView.contentMode = .ScaleToFill
newPageView.frame = frame
pagedScrollView.addSubview(newPageView)
// 4
pageViews[page] = newPageView
}
}
func purgePage(page: Int) {
if page < 0 || page >= pageImages.count {
// If it's outside the range of what you have to display, then do nothing
return
}
// Remove a page from the scroll view and reset the container array
if let pageView = pageViews[page] {
pageView.removeFromSuperview()
pageViews[page] = nil
}
}
func loadVisiblePages() {
// First, determine which page is currently visible
let pageWidth = pagedScrollView.frame.size.width
let numberOfImages = pageImages.count
let page = Int(floor((pagedScrollView.contentOffset.x * CGFloat(numberOfImages) + pageWidth) / (pageWidth * CGFloat(numberOfImages))))
// Update the page control
pageControl.currentPage = page
// Work out which pages you want to load
let firstPage = 0
let lastPage = numberOfImages
// Purge anything before the first page
for var index = 0; index < firstPage; ++index {
purgePage(index)
}
// Load pages in our range
for index in firstPage...lastPage {
loadPage(index)
}
// Purge anything after the last page
for var index = lastPage+1; index < pageImages.count; ++index {
purgePage(index)
}
}
func scrollViewDidScroll(pagedScrollView: UIScrollView!) {
// Load the pages that are now on screen
loadVisiblePages()
}