I have a view which can be rotated while whole other application is not. The rotation being done using view transform rather than rotation whole viewController. The issue is that in a landscape mode keyboard's inputaccessory view has wrong orientation - keyboard is being shown correctly in landscape mode, while inputAccessory view is being shown as in portrait:
In portrait orientation all looks correctly:
I'm using extension to build input accessory view:
extension UITextField
{
static var _doneAccessoryViewButtonCallback = [String:()->Void]()
var doneAccessoryViewButtonCallback:(()->Void)? {
get {
let tmpAddress = String(format: "%p", unsafeBitCast(self, to: Int.self))
return UITextField._doneAccessoryViewButtonCallback[tmpAddress]
}
set(newValue) {
let tmpAddress = String(format: "%p", unsafeBitCast(self, to: Int.self))
UITextField._doneAccessoryViewButtonCallback[tmpAddress] = newValue
}
}
@IBInspectable var doneAccessory: Bool{
get{
return self.doneAccessory
}
set (hasDone) {
if hasDone{
addDoneButtonOnKeyboard()
}
}
}
func addDoneButtonOnKeyboard()
{
let view:UIView = UIView()
view.frame = CGRect(x: CGFloat(0), y: CGFloat(0), width: UIScreen.main.bounds.width, height: CGFloat(44))
view.backgroundColor = UIColor.RGB(hexString: "#D1D5DB")
let button: UIButton = self.getButton()
view.addSubview(button)
button.translatesAutoresizingMaskIntoConstraints = false
button.constrainToRightOfSuperView(CGFloat(16))
button.centerVerticallyInSuperView()
self.inputAccessoryView = view
let version = OperatingSystemVersion(majorVersion: 13, minorVersion: 0, patchVersion: 0)
if ProcessInfo.processInfo.isOperatingSystemAtLeast(version) {
view.translatesAutoresizingMaskIntoConstraints = false
view.stretchToBoundsOfSuperView()
} else {
view.autoresizingMask = [.flexibleHeight, .flexibleWidth]
}
}
func isEmpty() -> Bool
{
return self.text == nil || self.text!.count == 0
}
// MARK: -
// MARK: - PRIVATES
private func getButton() -> UIButton
{
let button = UIButton(type: .custom)
let attString:NSMutableAttributedString = NSMutableAttributedString(string: "Done")
attString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.RGB(0xed, g: 0x3c, b: 00), range: NSRange(location: 0, length: attString.string.length))
attString.addAttribute(NSAttributedString.Key.font, value: UIFont(name: "Avenir-Book", size: 17)!, range: NSRange(location: 0, length: attString.string.length))
button.setAttributedTitle(attString, for: .normal)
button.addTarget(self, action: #selector(doneButtonAction), for: .touchUpInside)
return button
}
@objc func doneButtonAction()
{
self.resignFirstResponder()
self.doneAccessoryViewButtonCallback?()
}
}

