XLPagerTabStrip 不填满整个宽度

XLPagerTabStrip doesn't fill full width

问题

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

我有 3 个标签,但它们不会填满整个宽度,而且它们自己重叠。


Paren-Class代码

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 wouldn't fill the available width for me unless I specified settings.style.buttonBarItemLeftRightMargin = 0 as well.

有人解决这个问题吗?

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

试试这个

    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() 是代码的结尾,因为我在更改指标信息时遇到了一些问题

试试这个,它在生产应用程序中使用,对我有用

   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"
        }

    }

打开 pod 中的 ButtonBarPagerTabStripViewController.swift 文件。

ButtonBarPagerTabStripViewController class 定义中,删除 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,您将看不到任何错误。如果要使用XCode9,按上面的方法。

您需要先配置您的设置,然后再调用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
}

None 以上解决方案对我有用,除了, jcarlos276 的回答。

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

[TableView] Warning once only: UITableView was told to layout its visible cells and other contents without being in the view hierarchy (the table view or one of its superviews has not been added to a window). This may cause bugs by forcing views inside the table view to load and perform layout without accurate information (e.g. table view bounds, trait collection, layout margins, safe area insets, etc), and will also cause unnecessary performance overhead due to extra layout passes. Make a symbolic breakpoint at UITableViewAlertForLayoutOutsideViewHierarchy to catch this in the debugger and see what caused this to occur, so you can avoid this action altogether if possible, or defer it until the table view has been added to a window. Table view: <UITableView: 0x7f8366988000; frame = (0 0; 414 822); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x600002b2f780>; layer = <CALayer: 0x60000245df20>; contentOffset: {0, 0}; contentSize: {414, 438}; adjustedContentInset: {0, 0, 0, 0}; dataSource: (null)>

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

中进行布局
view.setNeedsLayout()
view.layoutIfNeeded()

我提出的实际修复方法有些相似。我没有要求整个 view 自行布局,而是为 ButtonBarView 连接了一个插座并要求它布局,

buttonBarView.setNeedsLayout()
buttonBarView.layoutIfNeeded()