spiegel-keyman/ios/engine/KMEI/KeymanEngine/Classes/LanguagePicker/KeyboardPickerBarButtonItem.swift
2017-11-10 14:25:50 +07:00

63 lines
1.9 KiB
Swift

//
// KeyboardPickerBarButtonItem.swift
// KeymanEngine
//
// Created by Gabriel Wong on 2017-09-12.
// Copyright © 2017 SIL International. All rights reserved.
//
import UIKit
public class KeyboardPickerBarButtonItem: UIBarButtonItem {
weak var presentingVC: UIViewController?
// Returns a UIBarButtonItem that will display the keyboard picker when tapped
// - since the keyboard picker is modal, a UIViewController must be supplied to display it
// - the button has default images for normal and landscape orientations
// - these can be overridden with other images or a title
public init(presentingVC: UIViewController) {
super.init()
style = .plain
action = #selector(self.showKeyboardPicker)
target = self
self.presentingVC = presentingVC
let bundlePath = Bundle(for: type(of: self)).path(forResource: "Keyman", ofType: "bundle")!
let keymanBundle = Bundle(path: bundlePath)!
let retinaSuffix = Manager.shared.retinaScreen ? "@2x" : ""
let imagePath = keymanBundle.path(forResource: "keyboard_icon\(retinaSuffix)", ofType: "png")!
image = UIImage(contentsOfFile: imagePath)
if UIDevice.current.userInterfaceIdiom == .phone {
let landscapeImagePath = keymanBundle.path(
forResource: "keyboard_icon_landscape\(retinaSuffix)", ofType: "png")!
landscapeImagePhone = UIImage(contentsOfFile: landscapeImagePath)
}
}
public required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
deinit {
presentingVC = nil
}
@objc func showKeyboardPicker() {
if let presentingVC = presentingVC {
Manager.shared.showKeyboardPicker(in: presentingVC, shouldAddKeyboard: false)
}
}
public override var title: String? {
get {
return super.title
}
set(title) {
image = nil
landscapeImagePhone = nil
super.title = title
}
}
}