Skip to content

Commit

Permalink
Merge pull request #540 from CodeStage/feature/accessibility
Browse files Browse the repository at this point in the history
Accessibility traits + labels
  • Loading branch information
Martin Barreto authored Mar 28, 2018
2 parents a974e6b + 04212ea commit 64a8ef8
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Sources/BaseButtonBarPagerTabStripViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ open class BaseButtonBarPagerTabStripViewController<ButtonBarCellType: UICollect
// selectedBar is resized and its contentOffset/scroll is set correctly (the selected
// tab/cell may end up either skewed or off screen after a rotation otherwise)
buttonBarView.moveTo(index: currentIndex, animated: false, swipeDirection: .none, pagerScroll: .scrollOnlyIfOutOfScreen)
buttonBarView.selectItem(at: IndexPath(item: currentIndex, section: 0), animated: false, scrollPosition: [])
}

// MARK: - View Rotation
Expand Down Expand Up @@ -340,6 +341,7 @@ open class ExampleBaseButtonBarPagerTabStripViewController: BaseButtonBarPagerTa

open override func configure(cell: ButtonBarViewCell, for indicatorInfo: IndicatorInfo) {
cell.label.text = indicatorInfo.title
cell.accessibilityLabel = indicatorInfo.accessibilityLabel
if let image = indicatorInfo.image {
cell.imageView.image = image
}
Expand Down
2 changes: 2 additions & 0 deletions Sources/ButtonBarPagerTabStripViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ open class ButtonBarPagerTabStripViewController: PagerTabStripViewController, Pa
// selectedBar is resized and its contentOffset/scroll is set correctly (the selected
// tab/cell may end up either skewed or off screen after a rotation otherwise)
buttonBarView.moveTo(index: currentIndex, animated: false, swipeDirection: .none, pagerScroll: .scrollOnlyIfOutOfScreen)
buttonBarView.selectItem(at: IndexPath(item: currentIndex, section: 0), animated: false, scrollPosition: [])
}

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

cell.label.text = indicatorInfo.title
cell.accessibilityLabel = indicatorInfo.accessibilityLabel
cell.label.font = settings.style.buttonBarItemFont
cell.label.textColor = settings.style.buttonBarItemTitleColor ?? cell.label.textColor
cell.contentView.backgroundColor = settings.style.buttonBarItemBackgroundColor ?? cell.contentView.backgroundColor
Expand Down
21 changes: 21 additions & 0 deletions Sources/ButtonBarViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,25 @@ open class ButtonBarViewCell: UICollectionViewCell {
@IBOutlet open var imageView: UIImageView!
@IBOutlet open var label: UILabel!

public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)

isAccessibilityElement = true
accessibilityTraits |= UIAccessibilityTraitButton
accessibilityTraits |= UIAccessibilityTraitHeader
}

open override var isSelected: Bool {
get {
return super.isSelected
}
set {
super.isSelected = newValue
if (newValue) {
accessibilityTraits |= UIAccessibilityTraitSelected
} else {
accessibilityTraits &= ~UIAccessibilityTraitSelected
}
}
}
}
14 changes: 14 additions & 0 deletions Sources/IndicatorInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ public struct IndicatorInfo {
public var title: String?
public var image: UIImage?
public var highlightedImage: UIImage?
public var accessibilityLabel: String?
public var userInfo: Any?

public init(title: String?) {
self.title = title
self.accessibilityLabel = title
}

public init(image: UIImage?, highlightedImage: UIImage? = nil, userInfo: Any? = nil) {
Expand All @@ -43,6 +45,15 @@ public struct IndicatorInfo {

public init(title: String?, image: UIImage?, highlightedImage: UIImage? = nil, userInfo: Any? = nil) {
self.title = title
self.accessibilityLabel = title
self.image = image
self.highlightedImage = highlightedImage
self.userInfo = userInfo
}

public init(title: String?, accessibilityLabel:String?, image: UIImage?, highlightedImage: UIImage? = nil, userInfo: Any? = nil) {
self.title = title
self.accessibilityLabel = accessibilityLabel
self.image = image
self.highlightedImage = highlightedImage
self.userInfo = userInfo
Expand All @@ -54,13 +65,16 @@ extension IndicatorInfo : ExpressibleByStringLiteral {

public init(stringLiteral value: String) {
title = value
accessibilityLabel = value
}

public init(extendedGraphemeClusterLiteral value: String) {
title = value
accessibilityLabel = value
}

public init(unicodeScalarLiteral value: String) {
title = value
accessibilityLabel = value
}
}

0 comments on commit 64a8ef8

Please sign in to comment.