Skip to content

Commit d0b0d76

Browse files
committed
Added accessibility traits
1 parent ce3a003 commit d0b0d76

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

Sources/BaseButtonBarPagerTabStripViewController.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ open class BaseButtonBarPagerTabStripViewController<ButtonBarCellType: UICollect
131131
// selectedBar is resized and its contentOffset/scroll is set correctly (the selected
132132
// tab/cell may end up either skewed or off screen after a rotation otherwise)
133133
buttonBarView.moveTo(index: currentIndex, animated: false, swipeDirection: .none, pagerScroll: .scrollOnlyIfOutOfScreen)
134+
buttonBarView.selectItem(at: IndexPath(item: currentIndex, section: 0), animated: false, scrollPosition: [])
134135
}
135136

136137
// MARK: - View Rotation
@@ -340,6 +341,7 @@ open class ExampleBaseButtonBarPagerTabStripViewController: BaseButtonBarPagerTa
340341

341342
open override func configure(cell: ButtonBarViewCell, for indicatorInfo: IndicatorInfo) {
342343
cell.label.text = indicatorInfo.title
344+
cell.accessibilityLabel = indicatorInfo.accessibilityLabel
343345
if let image = indicatorInfo.image {
344346
cell.imageView.image = image
345347
}

Sources/ButtonBarPagerTabStripViewController.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ open class ButtonBarPagerTabStripViewController: PagerTabStripViewController, Pa
194194
// selectedBar is resized and its contentOffset/scroll is set correctly (the selected
195195
// tab/cell may end up either skewed or off screen after a rotation otherwise)
196196
buttonBarView.moveTo(index: currentIndex, animated: false, swipeDirection: .none, pagerScroll: .scrollOnlyIfOutOfScreen)
197+
buttonBarView.selectItem(at: IndexPath(item: currentIndex, section: 0), animated: false, scrollPosition: [])
197198
}
198199

199200
// MARK: - Public Methods
@@ -323,6 +324,7 @@ open class ButtonBarPagerTabStripViewController: PagerTabStripViewController, Pa
323324
let indicatorInfo = childController.indicatorInfo(for: self)
324325

325326
cell.label.text = indicatorInfo.title
327+
cell.accessibilityLabel = indicatorInfo.accessibilityLabel
326328
cell.label.font = settings.style.buttonBarItemFont
327329
cell.label.textColor = settings.style.buttonBarItemTitleColor ?? cell.label.textColor
328330
cell.contentView.backgroundColor = settings.style.buttonBarItemBackgroundColor ?? cell.contentView.backgroundColor

Sources/ButtonBarViewCell.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,25 @@ open class ButtonBarViewCell: UICollectionViewCell {
2929
@IBOutlet open var imageView: UIImageView!
3030
@IBOutlet open var label: UILabel!
3131

32+
public required init?(coder aDecoder: NSCoder) {
33+
super.init(coder: aDecoder)
34+
35+
isAccessibilityElement = true
36+
accessibilityTraits |= UIAccessibilityTraitButton
37+
accessibilityTraits |= UIAccessibilityTraitHeader
38+
}
39+
40+
open override var isSelected: Bool {
41+
get {
42+
return super.isSelected
43+
}
44+
set {
45+
super.isSelected = newValue
46+
if (newValue) {
47+
accessibilityTraits |= UIAccessibilityTraitSelected
48+
} else {
49+
accessibilityTraits &= ~UIAccessibilityTraitSelected
50+
}
51+
}
52+
}
3253
}

Sources/IndicatorInfo.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,34 @@ public struct IndicatorInfo {
2929
public var title: String?
3030
public var image: UIImage?
3131
public var highlightedImage: UIImage?
32+
public var accessibilityLabel: String?
3233
public var userInfo: Any?
3334

3435
public init(title: String?) {
3536
self.title = title
37+
self.accessibilityLabel = title
3638
}
3739

3840
public init(image: UIImage?, highlightedImage: UIImage? = nil, userInfo: Any? = nil) {
3941
self.image = image
4042
self.highlightedImage = highlightedImage
43+
self.userInfo = userInfo
4144
}
4245

4346
public init(title: String?, image: UIImage?, highlightedImage: UIImage? = nil, userInfo: Any? = nil) {
4447
self.title = title
48+
self.accessibilityLabel = title
4549
self.image = image
4650
self.highlightedImage = highlightedImage
51+
self.userInfo = userInfo
52+
}
53+
54+
public init(title: String?, accessibilityLabel:String?, image: UIImage?, highlightedImage: UIImage? = nil, userInfo: Any? = nil) {
55+
self.title = title
56+
self.accessibilityLabel = accessibilityLabel
57+
self.image = image
58+
self.highlightedImage = highlightedImage
59+
self.userInfo = userInfo
4760
}
4861

4962
}
@@ -52,13 +65,16 @@ extension IndicatorInfo : ExpressibleByStringLiteral {
5265

5366
public init(stringLiteral value: String) {
5467
title = value
68+
accessibilityLabel = value
5569
}
5670

5771
public init(extendedGraphemeClusterLiteral value: String) {
5872
title = value
73+
accessibilityLabel = value
5974
}
6075

6176
public init(unicodeScalarLiteral value: String) {
6277
title = value
78+
accessibilityLabel = value
6379
}
6480
}

0 commit comments

Comments
 (0)