Skip to content

Commit 64a8ef8

Browse files
author
Martin Barreto
authored
Merge pull request #540 from CodeStage/feature/accessibility
Accessibility traits + labels
2 parents a974e6b + 04212ea commit 64a8ef8

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@ 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) {
@@ -43,6 +45,15 @@ public struct IndicatorInfo {
4345

4446
public init(title: String?, image: UIImage?, highlightedImage: UIImage? = nil, userInfo: Any? = nil) {
4547
self.title = title
48+
self.accessibilityLabel = title
49+
self.image = image
50+
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
4657
self.image = image
4758
self.highlightedImage = highlightedImage
4859
self.userInfo = userInfo
@@ -54,13 +65,16 @@ extension IndicatorInfo : ExpressibleByStringLiteral {
5465

5566
public init(stringLiteral value: String) {
5667
title = value
68+
accessibilityLabel = value
5769
}
5870

5971
public init(extendedGraphemeClusterLiteral value: String) {
6072
title = value
73+
accessibilityLabel = value
6174
}
6275

6376
public init(unicodeScalarLiteral value: String) {
6477
title = value
78+
accessibilityLabel = value
6579
}
6680
}

0 commit comments

Comments
 (0)