1. 程式人生 > >swift 導航條分段+滑動效果 用第三方PageTitleView

swift 導航條分段+滑動效果 用第三方PageTitleView

private lazy var pageTitleView: MFPageTitleView = {
        let config = MFPageTitleViewConfig()
        config.titleColor = colorWithRGB(r: 43, g: 43, b: 43)
        config.titleSelectedColor = colorWithRGB(r: 211, g: 0, b: 0)
        config.titleFont = UIFont.systemFont(ofSize: 14, weight: .regular)
        config.indicatorColor = colorWithRGB(r: 211, g: 0, b: 0)
        let pageTitleView = MFPageTitleView(frame: CGRect(x: 0, y: navHeight, width: SCREEN_WIDTH, height: 41), titles: ["推薦", "快訊", "深度", "試點對話", "行情分析"], config: config)
        pageTitleView.pageTitleViewDelegate = self as? MFPageTitleViewDelegate
        return pageTitleView
    }()
    
    private lazy var pageContentView: MFPageContentView = {
        var childControllers = [UIViewController]()
        for _ in 0..<5 {
            let vc = UIViewController()
            let red: CGFloat = CGFloat(arc4random() % 256)
            let green: CGFloat = CGFloat(arc4random() % 256)
            let blue: CGFloat = CGFloat(arc4random() % 256)
            vc.view.backgroundColor = UIColor(red: red / 255.0, green: green / 255.0, blue: blue / 255.0, alpha: 1.0)
            childControllers.append(vc)
        }
        
        let pageContentViewY = pageTitleView.frame.maxY
        let pageContentView = MFPageContentView(frame: CGRect(x: 0, y: pageContentViewY, width: SCREEN_WIDTH, height: SCREEN_HEIGHT-pageContentViewY), parentVC: self, childVCs: childControllers)
        pageContentView.pageContentViewDelegate = self as? MFPageContentViewDelegate
        return pageContentView
    }()
    //之前程式碼不寫在viewdidload
    //在viewdidload裡寫
    self.view.addSubview(pageTitleView)
     self.view.addSubview(pageContentView)