xmartlabs / XLPagerTabStrip

Android PagerTabStrip for iOS.
MIT License
6.97k stars 1.33k forks source link

Width issue for ButtonBarView with Label and Image #737

Closed sz-ashik440 closed 3 years ago

sz-ashik440 commented 5 years ago

I am trying to implement XLPagerTabStrip just like YoutubeWithLabelExampleViewController from Example project. I can configure it right but issue is width become fixed as I have to set width of buttonBarItemSpec.

buttonBarItemSpec = ButtonBarItemSpec.nibFile(nibName: "YoutubeIconWithLabelCell", bundle: Bundle(for: YoutubeIconWithLabelCell.self), width: { _ in
        return 40.0
})

I also tried to set settings.style.buttonBarItemsShouldFillAvailableWidth = true and settings.style.buttonBarItemLeftRightMargin = 0, but still it won't resize with the label size and keep the cell size fixed(40.0).

tsomaev commented 4 years ago

Anyone does fixed this bug ?

NikitaHappilyUnmarried commented 3 years ago

I m facing the same issue, have you fixed this?

sz-ashik440 commented 3 years ago

I found a work-around. Override sizeForItemAtIndexPath of UICollectionViewDelegateFlowLayout and set your custom size for you Button Bar cell size.

override func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize {
    return CGSize(width: <custom_width>, Height:<custom_height>)
}

PS: to get label size, you can use this extension for String

extension String {
    /// Get size for String
    /// - Parameters:
    ///   - font: used font for string
    ///   - spacing: kerning value for spacing
    /// - Returns: size require for text to shown
    func size(with font: UIFont?, spacing: Double = 0) -> CGSize {
        var attributes: [NSAttributedString.Key : Any] = font != nil ? [NSAttributedString.Key.font: font!] : [:]
        attributes[.kern] = spacing
        let textSize = self.size(withAttributes: attributes)
        return textSize
    }
}