XLPagerTabStrip ButtonBar 粘在导航栏后面

Posted

技术标签:

【中文标题】XLPagerTabStrip ButtonBar 粘在导航栏后面【英文标题】:XLPagerTabStrip ButtonBar sticks behind Navigation Bar 【发布时间】:2016-10-13 12:11:27 【问题描述】:

我在 swift 3 应用程序中使用 XLPagerTabStrip,我使用默认的 ButtonBar 并且一切正常,但是当我启用这样的导航栏时,该栏被隐藏:

如果我隐藏导航栏,它会显示如下:

我如何将它放在导航栏下方而不是它后面,因为我需要导航栏?

import UIKit
import XLPagerTabStrip

class DesignersController: ButtonBarPagerTabStripViewController 

override func viewDidLoad() 

    buttonBarView.backgroundColor = .white
    settings.style.buttonBarBackgroundColor = .white
    settings.style.selectedBarBackgroundColor = UIColor(netHex: 0x4a4a4a)
    settings.style.selectedBarHeight = 1
    settings.style.buttonBarItemBackgroundColor = .white
    settings.style.buttonBarItemTitleColor = UIColor(netHex: 0x8c8c8c)

    super.viewDidLoad()


override func viewWillDisappear(_ animated: Bool) 
    super.viewWillDisappear(animated)
    self.navigationController?.setNavigationBarHidden(true, animated: animated)


override func viewWillAppear(_ animated: Bool) 
    super.viewWillAppear(animated)
    //self.navigationController?.setNavigationBarHidden(false, animated: animated)


var isReload = false

override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] 
    let child_1 = ChildExampleViewController(itemInfo: "Top")
    let child_2 = ChildExampleViewController(itemInfo: "Men")
    let child_3 = ChildExampleViewController(itemInfo: "Women")

    guard isReload else 
        return [child_1, child_2, child_3]
    

    var childViewControllers = [child_1, child_2, child_3]

    for (index, _) in childViewControllers.enumerated()
        let nElements = childViewControllers.count - index
        let n = (Int(arc4random()) % nElements) + index
        if n != index
            swap(&childViewControllers[index], &childViewControllers[n])
        
    
    let nItems = 1 + (arc4random() % 8)
    return Array(childViewControllers.prefix(Int(nItems)))


override func reloadPagerTabStripView() 
    isReload = true
    if arc4random() % 2 == 0 
        pagerBehaviour = .progressive(skipIntermediateViewControllers: arc4random() % 2 == 0, elasticIndicatorLimit: arc4random() % 2 == 0 )
    
    else 
        pagerBehaviour = .common(skipIntermediateViewControllers: arc4random() % 2 == 0)
    
    super.reloadPagerTabStripView()




import Foundation
import XLPagerTabStrip

class ChildExampleViewController: UIViewController, IndicatorInfoProvider 

var itemInfo: IndicatorInfo = "View"

init(itemInfo: IndicatorInfo) 
    self.itemInfo = itemInfo
    super.init(nibName: nil, bundle: nil)


required init?(coder aDecoder: NSCoder) 
    fatalError("init(coder:) has not been implemented")


override func viewDidLoad() 
    super.viewDidLoad()

    let label = UILabel()
    label.translatesAutoresizingMaskIntoConstraints = false
    label.text = "XLPagerTabStrip"

    view.addSubview(label)
    view.backgroundColor = .red

    view.addConstraint(NSLayoutConstraint(item: label, attribute: .centerX, relatedBy: .equal, toItem: view, attribute: .centerX, multiplier: 1, constant: 0))
    view.addConstraint(NSLayoutConstraint(item: label, attribute: .centerY, relatedBy: .equal, toItem: view, attribute: .centerY, multiplier: 1, constant: -50))


// MARK: - IndicatorInfoProvider

func indicatorInfo(for pagerTabStripController: PagerTabStripViewController) -> IndicatorInfo 
    return itemInfo


提前致谢。

【问题讨论】:

是的.. 创建一个演示应用程序并检查,给它一些时间谢谢 :) 感谢您的宝贵时间,等待 :) @Sharpkits 有什么更新吗? 有事忙,会回复你.. @Sharpkits 什么? 【参考方案1】:

我刚刚遇到了同样的问题。只需在 Interface Builder 中将 CollectionView 添加到 VC,并将其类设置为“ButtonBarView”。Here 然后将其插座与左侧面板中的视图连接。Like this 就是这样,现在 buttonBar 应该在 NavigationBar 下。

【讨论】:

在我的例子中,我只是忽略了SafeArea 并且将约束与SuperView 对齐(参见UIStoryBoard - Prevent overlapping status bar)【参考方案2】:

这对我有帮助并且适用于 iPhone X:

self.navigationController!.navigationBar.isTranslucent = false 

【讨论】:

【参考方案3】:
self.edgesForExtendedLayout = []

我使用 nib 创建 PagerViewController 并在 viewDidLoad() 中编写了上面的代码

【讨论】:

这确实显示了选项卡,但它也使我的导航栏背景颜色变白,并消除了透明度。我需要我的导航栏透明。【参考方案4】:

这对我有帮助

self.edgesForExtendedLayout = UIRectEdge()

【讨论】:

以上是关于XLPagerTabStrip ButtonBar 粘在导航栏后面的主要内容,如果未能解决你的问题,请参考以下文章

更改 Flex Spark ButtonBar 控件的标签和图标位置

XLPagerTabStrip 内的 Collectionview

XLPagerTabStrip 黑色空间

XLPagerTabStrip 高度条

XLPagerTabStrip:不尊重状态栏

如何使用 XLPagerTabStrip