找到你要的答案

Q:UIBarButtonItem not showing under horizontal scrolling

Q:uibarbuttonitem下不显示水平滚动条

I followed this tutorial, specifically under "Paging with UIScrollView" section to create horizontal scrolling. Currently, I have it set up so that the images cover the entire frame/screen.

I am trying to add a simple UIBarButtonItem on the navigation bar (using Storyboard) in an attempt to build a slide-out menu. However, the button does not show up. Also, the navigation bar does not show up either even when I tried self.navigationController?.navigationBarHidden = false.

My current view controller is written like so.

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

我跟着这个教程,特别是在“寻呼UIScrollView”部分创建水平滚动。目前,我已经设置,使图像覆盖整个帧/屏幕。

我试图添加导航栏上的一个简单的uibarbuttonitem(使用故事板)在试图建立一个滑动菜单。但是,按钮不显示。另外,导航栏显示不出来或者甚至当我试图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()
    }
answer1: 回答1:

The code looks fine. Your problem is likely in your storyboard. Maybe the navigation controller isn't properly linked to your View Controller.

Did you attached the nav controller to the VC using Editor > Embed In > Navigation Controller? From the top menu.

代码看起来很好。你的问题在你的故事板是可能的。可能导航控制器与您的视图控制器没有正确连接。

你要用编辑器& gt VC连接导航控制器;嵌入& gt;导航控制器?从顶部菜单。

ios  objective-c  swift  uibarbuttonitem