使用Tab Swift

时间:2015-07-29 08:36:35

标签: ios swift uitabbarcontroller constraints uipageviewcontroller

我的工作是如此:

所以我有一个Tab栏,看起来像这样:

enter image description here

当我点击" Canteen"我希望被定向到一个页面视图控制器,我可以在不同的页面之间滑动但保持在同一个选项卡上。

我有点工作:

我的故事板设置如下:

enter image description here

正如您所看到的,上面的segue来自Tab Bar Controller。

第三个视图(Can Page Item Controller,ID:" CanItemController)用于页面视图中的所有页面。

上面的第二个视图(Page View Controller,ID:" CanPageController)用于控制Pages(duh)

第一个视图(CanteenViewController)包含所有代码并进行所有连接。一切都在这里。这个类中的代码在这里:

import UIKit

class CanteenViewController: UIViewController, UIPageViewControllerDataSource {

    // MARK: - Variables
    private var pageViewController: UIPageViewController?

    private let contentImages = ["Radar-512.png",
        "dartp.png",
        "roomp.png",
        "abnews.png",
        "canteenp.png"];

    override func viewDidLoad() {
        super.viewDidLoad()

        createPageViewController()
        setupPageControl()            
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    private func createPageViewController() {

        let pageController = self.storyboard!.instantiateViewControllerWithIdentifier("CanPageController") as! UIPageViewController
        pageController.dataSource = self

        if contentImages.count > 0 {
            let firstController = getItemController(0)!
            let startingViewControllers: NSArray = [firstController]
            pageController.setViewControllers(startingViewControllers as [AnyObject], direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)
        }

        pageViewController = pageController
        addChildViewController(pageViewController!)
        self.view.addSubview(pageViewController!.view)
        pageViewController!.didMoveToParentViewController(self)

    }

    private func setupPageControl() {

        let appearance = UIPageControl.appearance()
        appearance.pageIndicatorTintColor = UIColor.grayColor()
        appearance.currentPageIndicatorTintColor = UIColor.whiteColor()
        appearance.backgroundColor = UIColor.darkGrayColor()

    }

    // MARK: - UIPageViewControllerDataSource

    func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {

        let itemController = viewController as! CanPageItemController

        if itemController.itemIndex > 0 {
            return getItemController(itemController.itemIndex-1)
        }
        return nil
    }

    func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {

        let itemController = viewController as! CanPageItemController

        if itemController.itemIndex+1 < contentImages.count {
            return getItemController(itemController.itemIndex+1)
        }
        return nil
    }

    private func getItemController(itemIndex: Int) -> CanPageItemController? {

        if itemIndex < contentImages.count {
            let pageItemController = self.storyboard!.instantiateViewControllerWithIdentifier("CanItemController") as! CanPageItemController
            pageItemController.itemIndex = itemIndex
            pageItemController.imageName = contentImages[itemIndex]
            return pageItemController
        }
        return nil
    }

    // MARK: - Page Indicator

    func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
        return contentImages.count
    }

    func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
        return 0
    }

}

我有两个问题:

  1. 我根本看不到任何页面指示符。 这来自以下代码:

    private func setupPageControl() {
    
    let appearance = UIPageControl.appearance()
    appearance.pageIndicatorTintColor = UIColor.grayColor()
    appearance.currentPageIndicatorTintColor = UIColor.whiteColor()
    appearance.backgroundColor = UIColor.darkGrayColor()
    
    }
    

    有没有办法可以在故事板中添加页面指示符并以编程方式引用它。这样我可以添加约束并拥有更多控制权。我认为页面指示器可能隐藏在Tab栏后面。虽然约束也给我带来了问题,这导致我遇到问题2

  2. 正如您在项目控制器中看到的,我有一个UIImageView,约束都设置正确。但是当我运行应用程序时,图像会显示一秒钟(完全不成比例),然后消失。即 - 我的约束根本无法正常工作

  3. 问题

    我的方法一般只是错误吗?或者我可以做些什么改变来解决上述问题。我一直在关注一个教程(我认为Ray Wenderlich),这一切都很好,直到我尝试将它与我的Tab Bar集成。

1 个答案:

答案 0 :(得分:1)

保留以上所有内容,只需执行以下操作即可。

class CanteenViewController: UIViewController, UIScrollViewDelegate {

@IBOutlet var scrHelp: UIScrollView!
@IBOutlet var pageControl: UIPageControl!

var page = 0

let arrTitle : [String] = ["Title1", "Title2", "Title3", "Title4", "Title5"]
let arrImages : [String] = ["Radar-512.png", "dartp.png", "roomp.png", "abnews.png", "canteenp.png"];

override func viewDidLoad() {
    super.viewDidLoad()
    //self.edgesForExtendedLayout = UIRectEdge.None
    self.title = "Canteen"
    // Do any additional setup after loading the view, typically from a nib.

    self.createHelpView()

    pageControl.backgroundColor = UIColor.clearColor()
    pageControl.pageIndicatorTintColor = UIColor.lightGrayColor()
    pageControl.currentPageIndicatorTintColor = UIColor(red: 251/255, green: 108/255, blue: 108/255, alpha: 1.0)
    pageControl.tintAdjustmentMode = UIViewTintAdjustmentMode.Dimmed
    pageControl.numberOfPages = arrTitle.count
    pageControl.currentPage = 0

}

func createHelpView() {

    var x = 50
    for var i=0; i<arrTitle.count; i++  {
        var lblTitle = UILabel(frame: CGRectMake(CGFloat(x), 10, CGFloat(scrHelp.frame.width-100), 25))
        lblTitle.autoresizingMask = UIViewAutoresizing.FlexibleBottomMargin
        lblTitle.backgroundColor = UIColor.clearColor()
        lblTitle.font = UIFont.systemFontOfSize(17)
        lblTitle.textAlignment = NSTextAlignment.Center
        lblTitle.textColor = UIColor.blackColor()
        lblTitle.text = arrTitle[i]
        scrHelp.addSubview(lblTitle)

        var imgView = UIImageView(frame: CGRectMake(CGFloat(x), 50, CGFloat(scrHelp.frame.width-100), CGFloat(scrHelp.frame.height-150)))
        imgView.autoresizingMask = UIViewAutoresizing.FlexibleBottomMargin
        imgView.backgroundColor = UIColor.clearColor()
        imgView.image = UIImage(named: arrImages[i])
        imgView.contentMode = UIViewContentMode.ScaleAspectFit
        scrHelp.addSubview(imgView)

        x = x + Int(scrHelp.frame.width)
    }
    scrHelp.contentSize = CGSizeMake(CGFloat(arrTitle.count)*self.view.frame.width, 0)
}

func scrollViewDidScroll(scrollView: UIScrollView)
{
    var pageWidth = CGFloat(scrHelp.frame.width)
    var fractionalPage = scrHelp.contentOffset.x / pageWidth
    page = lround(CDouble(fractionalPage))
    pageControl.currentPage = page
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}

最后,将UIScrollViewUIPageControl添加到storyboard并设置相应的商店。