 
     open fileprivate(set) var selectedIndex = 0 {
         didSet {
+            if selectedIndex != oldValue {
+                updateTitleSizes()
+            }
             setNeedsDisplay()
         }
     }


    open fileprivate(set) var titles = [NSAttributedString]() {
        didSet {
            titleSizes = titles.map { sizeForAttributedString($0) }
        }
    }
    open fileprivate(set) var selectedTitles: [NSAttributedString]?

    open fileprivate(set) var selectedTitleAttachedIcons: [UIImage]?

    fileprivate var titleSizes = [CGSize]()

    private func updateTitleSizes() {
        titleSizes = titles.enumerated().map({ (offset, title) in
            if offset == selectedIndex,
                let selected = selectedTitles,
                0..<selected.count ~= offset {
                return sizeForAttributedString(selected[offset])
            }
            return sizeForAttributedString(title)
        })
    }

    open var isLongPressEnabled = false {
        didSet {
