无法使用带有自定义单元的 BaseButtonBarPagerTabStripViewController 连接 XLPagertabStrip 插座

Posted

技术标签:

【中文标题】无法使用带有自定义单元的 BaseButtonBarPagerTabStripViewController 连接 XLPagertabStrip 插座【英文标题】:Unable to connect XLPagertabStrip outlets using BaseButtonBarPagerTabStripViewController with custom cell 【发布时间】:2017-04-06 08:35:48 【问题描述】:

我想使用 XLPagerTabStrip 创建自定义选项卡,我正在使用带有自定义 collectionview 单元格的 BaseButtonBarPagerTabStripViewController。但是我遇到的问题是,我无法从课堂上连接 Buttonbarview 和 containerview,请您帮帮我。贝娄是我的课

    import Foundation
import XLPagerTabStrip

class YoutubeExampleViewController: BaseButtonBarPagerTabStripViewController<YoutubeIconCell> 

    let redColor = UIColor(red: 221/255.0, green: 0/255.0, blue: 19/255.0, alpha: 1.0)
    let unselectedIconColor = UIColor(red: 73/255.0, green: 8/255.0, blue: 10/255.0, alpha: 1.0)

    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) 
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    

    required init?(coder aDecoder: NSCoder) 
        super.init(coder: aDecoder)

        buttonBarItemSpec = ButtonBarItemSpec.nibFile(nibName: "YoutubeIconCell", bundle: Bundle(for: YoutubeIconCell.self), width:  (cell: IndicatorInfo) -> CGFloat in
                return 55.0
        )
    

    override func viewDidLoad() 
        // change selected bar color

        settings.style.buttonBarBackgroundColor = redColor
        settings.style.buttonBarItemBackgroundColor = .clear
        settings.style.selectedBarBackgroundColor = UIColor(red: 234/255.0, green: 234/255.0, blue: 234/255.0, alpha: 1.0)
        settings.style.selectedBarHeight = 4.0
        settings.style.buttonBarMinimumLineSpacing = 0
        settings.style.buttonBarItemTitleColor = .black
        settings.style.buttonBarItemsShouldFillAvailableWidth = true
        settings.style.buttonBarLeftContentInset = 0
        settings.style.buttonBarRightContentInset = 0



        changeCurrentIndexProgressive =  [weak self] (oldCell: YoutubeIconCell?, newCell: YoutubeIconCell?, progressPercentage: CGFloat, changeCurrentIndex: Bool, animated: Bool) -> Void in
            guard changeCurrentIndex == true else  return 
            oldCell?.iconImage.tintColor = self?.unselectedIconColor
            newCell?.iconImage.tintColor = .white

        
        super.viewDidLoad()
        navigationController?.navigationBar.shadowImage = UIImage()
        navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
    

    // MARK: - PagerTabStripDataSource

    override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] 
        let child_1 = FeedsTableViewController(style: .plain, itemInfo: IndicatorInfo(title: " HOME", image: UIImage(named: "c1")))
        let child_2 = FeedsTableViewController(style: .plain, itemInfo: IndicatorInfo(title: " TRENDING", image: UIImage(named: "d1")))
        return [child_1, child_2]
    


    override func configure(cell: YoutubeIconCell, for indicatorInfo: IndicatorInfo) 
        cell.iconImage.image = indicatorInfo.image?.withRenderingMode(.alwaysTemplate)
    

    override func updateIndicator(for viewController: PagerTabStripViewController, fromIndex: Int, toIndex: Int, withProgressPercentage progressPercentage: CGFloat, indexWasChanged: Bool) 
        super.updateIndicator(for: viewController, fromIndex: fromIndex, toIndex: toIndex, withProgressPercentage: progressPercentage, indexWasChanged: indexWasChanged)
        if indexWasChanged && toIndex > -1 && toIndex < viewControllers.count 
            let child = viewControllers[toIndex] as! IndicatorInfoProvider
            UIView.performWithoutAnimation( [weak self] () -> Void in
                guard let me = self else  return 
                me.navigationItem.leftBarButtonItem?.title =  child.indicatorInfo(for: me).title
            )
        
    

    // MARK: - Actions

    @IBAction func closeAction(_ sender: UIButton) 
        dismiss(animated: true, completion: nil)
    

【问题讨论】:

【参考方案1】:

我不知道作者的解决方案,因为我还没有尝试过,但我的解决方案(刚刚尝试并成功)是:

注释掉继承自BaseButtonBarPagerTabStripViewController的类的所有代码

编写具有相同名称的新类,继承自 ButtonBarPagerTabStripViewController(不是基础类,这是魔法发生的地方)。

回到你的 storyboard/xib 文件,现在你可以用 containerView 和 buttonBarView 连接 outlet。

之后,撤消您对班级所做的操作。

【讨论】:

【参考方案2】:

最后我得到了答案,我现在发布它,如果是 XLPagerTabStrip,当我们从情节提要设置初始视图控制器时它不起作用。只有当我们以编程方式启动它时它才有效,这是我在使用这个库时犯的唯一错误。谢谢!

     let _ = YoutubeExampleViewController(nibName: nil, bundle: nil)
     let sb = UIStoryboard(name: "Main", bundle: nil)
     let tabBarViewController = sb.instantiateViewController(withIdentifier: "YoutubeExampleViewController")
     let nav = UINavigationController(rootViewController: tabBarViewController)
     window = UIWindow(frame: UIScreen.main.bounds)
     window?.rootViewController = nav
     window?.makeKeyAndVisible()

【讨论】:

以上是关于无法使用带有自定义单元的 BaseButtonBarPagerTabStripViewController 连接 XLPagertabStrip 插座的主要内容,如果未能解决你的问题,请参考以下文章

无法从自定义表格视图单元格按钮中删除行

WPF 数据网格。无法突出显示自定义样式的单元格

带有自定义单元格的 UITableView 在 iOS 6 上画得很好,但在 iOS 7 上完全是白色的

使用带有 TextField 的自定义表格视图单元格关闭数字键盘

带有自定义单元格的 UITableView

使用带有原型单元的自定义 UITableViewCell