iOS How to get keyboard height when the inputview is changed? - ios

I am trying to place a textview just above the keyboard. The keyboard has two views - one default view and one custom view. A button is there to toggle between these two keyboards.
I am trying to use the following code to position the textview just above the keyboard. But it is behaving very weirdly. The keyboardWillShow notification is not getting called all the times.
Note: I wanted it to support for both portrait and landscape.
class ViewController: UIViewController {
let textView = UITextView()
let button = UIButton()
var keyboardHeight: CGFloat = 0
var keyboardView = UIView()
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
textView.backgroundColor = .lightGray
button.backgroundColor = .red
self.view.addSubview(textView)
self.view.addSubview(button)
}
#objc func buttonAction() {
if self.textView.inputView == nil {
self.textView.inputView = keyboardView
self.textView.inputView?.autoresizingMask = .flexibleHeight
self.textView.reloadInputViews()
textView.frame = CGRect(x: 0, y: self.view.frame.size.height - self.textView.inputView!.frame.size.height - 50, width: self.view.frame.size.width - 50, height: 50)
button.frame = CGRect(x: self.view.frame.size.width - 50, y: self.view.frame.size.height - self.textView.inputView!.frame.size.height - 50, width: 50, height: 50)
} else {
self.textView.inputView = nil
self.textView.reloadInputViews()
textView.frame = CGRect(x: 0, y: self.view.frame.size.height - self.keyboardHeight - 50, width: self.view.frame.size.width - 50, height: 50)
button.frame = CGRect(x: self.view.frame.size.width - 50, y: self.view.frame.size.height - self.keyboardHeight - 50, width: 50, height: 50)
}
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
textView.frame = CGRect(x: 0, y: self.view.frame.size.height - self.keyboardHeight - 50, width: self.view.frame.size.width - 50, height: 50)
button.frame = CGRect(x: self.view.frame.size.width - 50, y: self.view.frame.size.height - self.keyboardHeight - 50, width: 50, height: 50)
}
#objc func keyboardWillShow(notification: Notification) {
if let userInfo = notification.userInfo {
if let keyboardFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
self.keyboardHeight = keyboardFrame.size.height
textView.frame = CGRect(x: 0, y: self.view.frame.size.height - self.keyboardHeight - 50, width: self.view.frame.size.width - 50, height: 50)
button.frame = CGRect(x: self.view.frame.size.width - 50, y: self.view.frame.size.height - self.keyboardHeight - 50, width: 50, height: 50)
}
}
}
}

If you wanna get the height of Keyboard when keyboard frame changes because of you toggling between custom keyboard and system keyboard, you can achieve it many ways.
I personally dont prefer notifications they always end up giving me a discrete values, though most of the realtime cases they are good enough when you need to modify the UI as keyboard frame changes in real time (Like try dismissing keyboard on scrolling tableView like whats app where keyboard would go down with your finger as u move and u need to align views in realtime when keyboards scrolls down)
Anyway I believe the approach explained below should also help u to get accurate keyboard height when toggled
Step 1:
Create a subclass of UIView which we will be adding as input accessory view to textView/textField later
protocol KeyBoardObserverProtocol : NSObjectProtocol {
func keyboardFrameChanged(frame : CGRect)
}
class KeyboardObserverAccessoryView: UIView {
weak var keyboardDelegate:KeyBoardObserverProtocol? = nil
private var kvoContext: UInt8 = 1
override func willMove(toSuperview newSuperview: UIView?) {
if newSuperview == nil {
self.superview?.removeObserver(self, forKeyPath: "center")
}
else{
newSuperview?.addObserver(self, forKeyPath: "center", options: [NSKeyValueObservingOptions.new, NSKeyValueObservingOptions.initial], context: &kvoContext)
}
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if let theChange = change as [NSKeyValueChangeKey : AnyObject]?{
if theChange[NSKeyValueChangeKey.newKey] != nil{
if self.keyboardDelegate != nil && self.superview?.frame != nil {
self.keyboardDelegate?.keyboardFrameChanged(frame: (self.superview?.frame)!)
}
}
}
}
}
Though code looks big and messy whats inside is pretty easy to understand. All it does is it start obseving the parent view frame using KVO once it receives the call willMove(toSuperview newSuperview: UIView?) because thats when you know ur view is moved to parent and u know that now u need to monitor the parent's frame.
And every time your KVO triggers the new value u inform it to the interred party using the delegate
How to use it?
keyBoardObserverAccessoryView = KeyboardObserverAccessoryView(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
keyBoardObserverAccessoryView.keyboardDelegate = self
self.textView.inputAccessoryView = keyBoardObserverAccessoryView
Thats it :) Now if you wanna provide your custom input accessory view in future just make sure u extend from KeyboardObserverAccessoryView n enjoy the benefits
Hope it helps :)

Related

How do you pass a gesture between subviews in UIKit?

I've got a ViewController with three subviews. I'm trying to get them to detect touches in their bounds from a starting point outside their bounds without the user lifting their finger (ie the user dragging into the view). I thought hitTest would do this but it only works for separate taps. I assume this is probably passing a gesture through instead but I've not found out how to implement this.
class SuperViewController: UIViewController {
var view01 = UIView(frame: CGRect(x: 0, y: 0, width: 1000,
height: 800))
var view02 = UIView(frame: CGRect(x: 0, y: 0, width: 600,
height: 400))
let view03 = UIView(frame: CGRect(x: 0, y: 0, width: 300,
height: 200))
override func viewDidLoad() {
super.viewDidLoad()
self.view = TestView()
view01.backgroundColor = .orange
view02.backgroundColor = .blue
view03.backgroundColor = .green
self.view.addSubview(view01)
self.view.addSubview(view02)
self.view.addSubview(view03)
}
}
Which produces this
And then I've subclassed UIView for the SuperViewController's view.
class TestView: UIView {
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
guard self.isUserInteractionEnabled, !isHidden, alpha > 0.01 else {return nil}
if self.point(inside: point, with: event) {
for subview in subviews.reversed() {
let hitView = subview.hitTest(point, with: event)
if hitView != nil {
hitView?.backgroundColor = .red
return hitView
}
}
return self
}
return nil
}
}
So each one turns red when the user taps. But ideally I want them to each respond with one drag from the top left corner of the screen to the other.
You can accomplish this with a UIPanGestureRecognizer.
Here's an example below:
class ViewController: UIViewController {
var view01 = UIView(frame: CGRect(x: 0, y: 0, width: 1000,
height: 800))
var view02 = UIView(frame: CGRect(x: 0, y: 0, width: 600,
height: 400))
let view03 = UIView(frame: CGRect(x: 0, y: 0, width: 300,
height: 200))
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
view01.backgroundColor = .orange
view02.backgroundColor = .blue
view03.backgroundColor = .green
self.view.addSubview(view01)
self.view.addSubview(view02)
self.view.addSubview(view03)
let gestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePan))
self.view.addGestureRecognizer(gestureRecognizer)
}
#objc
private func handlePan(_ gestureRecognizer: UIPanGestureRecognizer) {
guard let view = gestureRecognizer.view else {
return
}
let translation = gestureRecognizer.translation(in: view)
for subview in view.subviews.reversed() {
if let hitView = subview.hitTest(translation, with: nil) {
hitView.backgroundColor = .red
return
}
}
}
}

Single extension for TextView and TextField to add a toolBar

I want to create a single extension to add a toolBar on a keyboard for TextView and TextField.
For now I’m doing that on both TextView and TextField:
extension UITextView {
func setKeyboardToolBar(items: [UIBarButtonItem]) {
let screenWidth = UIScreen.main.bounds.width
let toolBar = UIToolbar(frame: CGRect(x: 0.0, y: 0.0, width: screenWidth, height: 44.0))
toolBar.setItems(items, animated: false)
self.inputAccessoryView = toolBar
}
}
But it’s annoying to have the exact same function in 2 different extensions.
I tried to extend UIView like in this question Single extension for UITextView and UITextField in Swift but I got an error with inputAccessoryView because it's a get only property.
How could I factorise these two same functions?
Maybe this will help you:
extension UITextField: KeyboardToolbarCompatible {}
extension UITextView: KeyboardToolbarCompatible {}
protocol KeyboardToolbarCompatible: AnyObject {
func setKeyboardToolBar(items: [UIBarButtonItem])
var inputAccessoryView: UIView? { get set }
}
extension KeyboardToolbarCompatible {
func setKeyboardToolBar(items: [UIBarButtonItem]) {
let screenWidth = UIScreen.main.bounds.width
let toolBar = UIToolbar(frame: CGRect(x: 0.0, y: 0.0, width: screenWidth, height: 44.0))
toolBar.setItems(items, animated: false)
self.inputAccessoryView = toolBar
}
}
If this is needed only for TextView and TextField then KeyboardToolbarCompatible can confirm to UITextInput.
protocol KeyboardToolbarCompatible: UITextInput { ... }

How do I programmatically change the height of a navigationBar in Swift?

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let height = CGFloat(84)
self.navigationController?.navigationBar.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: height)
}
This code simply inserts space above the titleView. A custom titleView at point (0,0) has ~20 points of space above it. A height >40 starts to run off the navBar.
You can subclass UINavigationBar :
class CustomNavigationBar: UINavigationBar {
override func sizeThatFits(_ size: CGSize) -> CGSize {
let newSize :CGSize = CGSize(width: self.frame.size.width,height: 84)
return newSize
}
}
Then create the navigation controller and use the initialiser to use your custom navigation bar class.
let nav = UINavigationController(navigationBarClass:CustomNavigationBar.self,toolbarClass: nil)
All existing behavior for UINavigationBar is preserved and your custom height is adopted.
OR
Like you already tried :
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let height: CGFloat = 84 //whatever height you want
let bounds = self.navigationController!.navigationBar.bounds
self.navigationController?.navigationBar.frame = CGRect(x: 0, y: 0, width: bounds.width, height: bounds.height + height)
}
OR :
You can try this solution Changing the height of the Navigation bar iOS Swift
you can use a custom view to replace the navigation bar.This is more easy and flexible. hide the navi bar and implement a custom view.
class ViewController : UIViewController {
var navBar: UINavigationBar = UINavigationBar()
override func viewDidLoad() {
super.viewDidLoad()
self.setCustomNavBarView()
}
func setCustomNavBarView() {
self.navBar.frame = CGRect(x: 0, y: 0, width: 350, height: 50) // Set you custom width and Height
self.navBar.backgroundColor = UIColor.gray
self.view.addSubview(navBar)
}
}
A simple tutorial on how to do that:
Hope this helps!!
class ViewController: UIViewController {
var navBar: UINavigationBar = UINavigationBar()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.setCustomNavBarView()
}
`func setCustomNavBarView() {
self.navBar.frame = CGRect(x: 0, y: 0, width: 350, height: 100) // Set you custom width and Height
self.navBar.backgroundColor = UIColor.gray
self.view.addSubview(navBar)
}
`
swift 3 updated code here

Strange behaviour - instantiating a UITextField in willMoveToSuperview as opposed to didMoveToSuperview

I'm working with XCode 6.3/iOS/Swift 1.2. I've just stumbled upon some strange behaviour that I don't understand (coming from a strong OO background).
Put simply, if I subclass a UIView and use it to instantiate some UITextFields; in the constructor, overriding didMoveToSuperview, on dynamically (ie, with a click event)... the UITextFields work as expected. However, when creating the UITextField object during willMoveToSuperview, it seems fine... but it doesn't respond to touch events.
This is demonstrated in the code below (a UIViewController and the subclassed UIView). I've added a gesture recognizer to the entire view containing the UITextFields. Clicking any of them other than the one creating during willMoveToSuperview, will move the focus to that textfield, and will ignore the touch event (as expected). However, clicking on the one added during willMoveToSuperview fires a touch event. Event without the touch event... this textfield remains unresponsive. Can anyone explain this behaviour?
import UIKit
class RootViewController:UIViewController
{
override func viewDidLoad()
{
super.viewDidLoad()
self.view.frame = CGRectMake(0,0,500,500)
}
override func viewDidAppear(animated: Bool)
{
var uiview = ExtendedUIView()
view.addSubview(uiview)
}
}
class ExtendedUIView:UIView
{
internal var hasMovedToSuperView:Bool = false
override init (frame : CGRect)
{
super.init(frame : frame)
}
convenience init()
{
self.init(frame:CGRectMake(0,0,500,500))
var tf: UITextField = UITextField(frame: CGRect(x: 0, y: 50, width: 300, height: 40))
tf.backgroundColor = UIColor.greenColor()
tf.text = "init // ok"
addSubview(tf)
}
required init(coder aDecoder: NSCoder)
{
fatalError("This class does not support NSCoding")
}
override func willMoveToSuperview(newSuperview: UIView?)
{
if (!hasMovedToSuperView) {
var tf: UITextField = UITextField(frame: CGRect(x: 0, y: 100, width: 300, height: 40))
tf.backgroundColor = UIColor.redColor()
tf.text = "willMoveToSuperview // not ok"
addSubview(tf)
}
}
override func didMoveToSuperview()
{
if (!hasMovedToSuperView) {
var tf: UITextField = UITextField(frame: CGRect(x: 0, y: 150, width: 300, height: 40))
tf.backgroundColor = UIColor.greenColor()
tf.text = "didMoveToSuperview // ok"
addSubview(tf)
hasMovedToSuperView = true
addGestureRecognizer(UITapGestureRecognizer(target:self, action:Selector("createTFDynamically:")))
}
}
func createTFDynamically(value:AnyObject)
{
var tf: UITextField = UITextField(frame: CGRect(x: 0, y: 200, width: 300, height: 40))
tf.backgroundColor = UIColor.greenColor()
tf.text = "createTFDynamically // ok"
addSubview(tf)
}
}

UITabBar top of the screen

I need to put my tab bar at the top of the screen.
I've tried and I did, this is the code:
override func viewDidLoad() {
self.tabBar.frame = CGRectMake(0, 90, self.view.bounds.size.width, self.view.bounds.size.height);
self.tabBar.autoresizingMask = UIViewAutoresizing.FlexibleWidth
}
the problem is I when I turn the screen of my device.
I should regenerate the layout and change in this way:
self.tabBar.frame = CGRectMake(0, 80, self.view.bounds.size.width, self.view.bounds.size.height);
How can I change this every time you turn the screen?
EDIT:
New Code:
override func viewDidLayoutSubviews()
{
//check tabBar not null
if (self.tabBar != nil)
{
//make changes in frame here according to orientation if any
self.tabBar.frame = CGRect(x: 00, y: 20, width:self.view.bounds.size.width, height: 49)
}
}
override func viewDidLoad()
{
//Make changes in frame according to your requirement
self.tabBar.frame = CGRectMake(0, 20, self.view.bounds.size.width,49);
//resizing here
self.tabBar.autoresizingMask = UIViewAutoresizing.FlexibleTopMargin;UIViewAutoresizing.FlexibleLeftMargin;UIViewAutoresizing.FlexibleWidth;UIViewAutoresizing.FlexibleRightMargin;
}
Define UITabBar in viewDidLoad
override func viewDidLoad()
{
//Make changes in frame according to your requirement
self.tabBar.frame = CGRectMake(0, 20, self.view.bounds.size.width,49);
//resizing here
self.tabBar.autoresizingMask = UIViewAutoresizing.FlexibleTopMargin;UIViewAutoresizing.FlexibleLeftMargin;UIViewAutoresizing.FlexibleWidth;UIViewAutoresizing.FlexibleRightMargin;
}
Add below method viewDidLayoutSubviews
override func viewDidLayoutSubviews()
{
//check tabBar not null
if (self.tabBar != nil)
{
//make changes in frame here according to orientation if any
self.tabBar.frame = CGRect(x: 00, y: 20, width:self.view.bounds.size.width, height: 49)
}
}

Resources