3

问题

我正在开发一个 IOS 应用程序。我使用 XLPagerTabStrip 库来创建像在 Android 中一样的选项卡,例如。YouTube。一切正常,但我的选项卡有问题。

我有 3 个选项卡,但它们不会填满整个宽度,而且它们会相互重叠。

3 个有问题的标签


父类代码

import UIKit
import XLPagerTabStrip

class InformationsParentView: ButtonBarPagerTabStripViewController {

    let purpleInspireColor = UIColor(red:0.13, green:0.03, blue:0.25, alpha:1.0)

    override func viewDidLoad() {

        super.viewDidLoad()

        settings.style.buttonBarBackgroundColor = .white
        settings.style.buttonBarItemBackgroundColor = .white
        settings.style.selectedBarBackgroundColor = purpleInspireColor
        settings.style.buttonBarItemLeftRightMargin = 0
        settings.style.selectedBarHeight = 2
        settings.style.buttonBarMinimumLineSpacing = 20
        settings.style.buttonBarItemTitleColor = .black
        settings.style.buttonBarItemLeftRightMargin = 0
        settings.style.buttonBarItemsShouldFillAvailiableWidth = true
    }

    override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] {
        let tabWorkers = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "sbWorkers")
        let tabPLs = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "sbPLs")
        let tabSuperiors = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "sbSuperiors")

        return [tabWorkers, tabPLs, tabSuperiors]
    }

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

}

故事板

场景

场景

场景

场景

属性检查器

属性检查器

尺寸检查器

尺寸检查器


信息

我找到了一个解决方案,但它对我不起作用(Github Issue 256):

settings.style.buttonBarItemsShouldFillAvailableWidth = true除非我也指定,否则不会为我填充可用宽度settings.style.buttonBarItemLeftRightMargin = 0

有没有人解决这个问题?

我正在使用 XCode 版本 9、Swift 3 和 IOS 11。

4

5 回答 5

8

在 pod 中打开ButtonBarPagerTabStripViewController.swift文件。

ButtonBarPagerTabStripViewController类定义中,去掉UICollectionViewDelegate协议,实现UICollectionViewDelegateFlowLayout协议。

所以更改源代码

open class ButtonBarPagerTabStripViewController: 
    PagerTabStripViewController, PagerTabStripDataSource, 
    PagerTabStripIsProgressiveDelegate, UICollectionViewDelegate, 
    UICollectionViewDataSource {
        ...
}

open class ButtonBarPagerTabStripViewController: 
    PagerTabStripViewController, PagerTabStripDataSource, 
    PagerTabStripIsProgressiveDelegate, UICollectionViewDataSource, 
    UICollectionViewDelegateFlowLayout {
        ...
}

当我将我的应用程序从 XCode 8.3.3 迁移到 XCode 9 时,我遇到了这个问题。这是 pod 的问题。如果您使用 XCode 8.3.3 进行尝试,您将看不到任何错误。如果要使用 XCode 9,请执行上述方法。

于 2017-09-27T12:52:06.603 回答
1

除了 jcarlos276 的回答之外,上述解决方案都不适合我。 https://stackoverflow.com/a/61784184/3549781

虽然它解决了这个问题,但有一些副作用,比如在我的情况下,其中一个子视图控制器中有 UITableView 并且它发出警告,

[TableView] 仅警告一次:UITableView 被告知布局其可见单元格和其他内容而不位于视图层次结构中(表视图或其超级视图之一尚未添加到窗口中)。这可能会通过强制表视图中的视图在没有准确信息的情况下加载和执行布局(例如表视图边界、特征集合、布局边距、安全区域插入等)而导致错误,并且还会由于额外的布局传递而导致不必要的性能开销. 在 UITableViewAlertForLayoutOutsideViewHierarchy 处创建一个符号断点,以便在调试器中捕获它并查看导致这种情况发生的原因,因此如果可能的话,您可以完全避免此操作,或者将其推迟到表格视图添加到窗口中。表格视图:<UITableView: 0x7f8366988000; 帧 = (0 0; 414 822); clipsToBounds = YES; 自动调整大小 = RM+BM;手势识别器 = <NSArray: 0x600002b2f780>; 层 = <CALayer: 0x60000245df20>; 内容偏移:{0, 0}; 内容大小:{414, 438};调整内容插入:{0, 0, 0, 0}; 数据源:(空)>

这是由以下代码引起的,该代码要求视图立即在func viewDidLoad()

view.setNeedsLayout()
view.layoutIfNeeded()

我想出的实际修复有点相似。我没有要求整体view进行布局,而是连接了一个插座ButtonBarView并要求它进行布局,

buttonBarView.setNeedsLayout()
buttonBarView.layoutIfNeeded()
于 2021-07-04T15:03:40.860 回答
1

试试这个

    buttonBarView.selectedBar.backgroundColor = #colorLiteral(red: 0.1353600621, green: 0.1353642344, blue: 0.1353619695, alpha: 1)


    settings.style.buttonBarBackgroundColor =  .white
    settings.style.buttonBarItemBackgroundColor =  .white

    settings.style.selectedBarHeight = 2.0
    settings.style.selectedBarBackgroundColor = purpleInspireColor


    settings.style.buttonBarItemFont = .boldSystemFont(ofSize: 14)

    settings.style.buttonBarItemTitleColor = .black

    settings.style.buttonBarItemsShouldFillAvailiableWidth = false


    super.viewDidLoad()

buttonBarItemsShouldFillAvailiableWidth = false并确保super.viewDidload() 是代码的结尾,因为我在更改指标信息时遇到了一些问题

于 2017-09-25T08:35:19.317 回答
1

你需要在调用super.viewDidLoad()之前先配置好你的设置然后在配置完设置后尝试添加view.layoutIfNeeded(),如下所示:

override func viewDidLoad() {
    //configure settings first
    configurePagerTabStrip()

    super.viewDidLoad()

    //then
    self.view.layoutIfNeeded()
}

func configurePagerTabStrip() {
    settings.style.buttonBarBackgroundColor = .white
    settings.style.buttonBarItemBackgroundColor = .white
    settings.style.selectedBarBackgroundColor = purpleInspireColor
    settings.style.buttonBarItemLeftRightMargin = 0
    settings.style.selectedBarHeight = 2
    settings.style.buttonBarMinimumLineSpacing = 20
    settings.style.buttonBarItemTitleColor = .black
    settings.style.buttonBarItemLeftRightMargin = 0
    settings.style.buttonBarItemsShouldFillAvailiableWidth = true
}
于 2020-05-13T20:25:33.327 回答
0

试试这个,它在生产应用程序中使用,它适用于我

   import UIKit
    import XLPagerTabStrip

    class EventsPagerViewController: ButtonBarPagerTabStripViewController {

        /// MARK: - Data

        var routingModel: RoutingModel?

        // MARK: - Lifecycle

        override func viewDidLoad() {
            // change selected bar color
            settings.style.buttonBarBackgroundColor = .white
            settings.style.buttonBarItemBackgroundColor = .white
            settings.style.selectedBarBackgroundColor = ColorManager.mainOrange
            settings.style.buttonBarItemFont = .boldSystemFont(ofSize: 14)
            settings.style.selectedBarHeight = 2.0
            settings.style.buttonBarMinimumLineSpacing = 0
            settings.style.buttonBarItemTitleColor = .black
            settings.style.buttonBarItemsShouldFillAvailableWidth = true
            settings.style.buttonBarLeftContentInset = 0
            settings.style.buttonBarRightContentInset = 0

            changeCurrentIndexProgressive = { (oldCell: ButtonBarViewCell?, newCell: ButtonBarViewCell?, progressPercentage: CGFloat, changeCurrentIndex: Bool, animated: Bool) -> Void in
                guard changeCurrentIndex == true else { return }
                oldCell?.label.textColor = .black
                newCell?.label.textColor = ColorManager.mainOrange
            }

            containerView?.isScrollEnabled = true
            super.viewDidLoad()
            definesPresentationContext = true
            setupNavSettings()        
        }

        override public func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] {
            let upcomingVC = EventsUpcomingItchesViewController()
            upcomingVC.routingModel = routingModel
            let pastVC = EventsPastItchesViewController()

            // deinit data
            routingModel = nil 
            return [upcomingVC, pastVC]
        }


        private func setupNavSettings() {
            navigationItem.title = "My Events"
        }

    }
于 2017-09-25T09:59:58.767 回答