How I get the keyboard frame before the keyboard appears - ios

I have a button where you get a textfield with a keyboard. At the moment, when I get this textfield, the textfield is in the left corner and the textfield appears. How can I handle this, that the textfield is above my keyboard?
The problem is, that I get the keyboard height after the code in editTextButtonTapped is done. How can I get the height of the keyboard before editTextButtonTapped is called?
// This are my global Variables to create the textfield
var myTextField: UITextField = UITextField(frame: CGRect(x: 0,y: 0, width: 0, height:0))
var firstkeyboardHeight = CGFloat()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
#objc func keyboardWillShow(_ notification: NSNotification) -> Void {
if let keyboardRect = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as AnyObject).cgRectValue {
let keyboardHeight = keyboardRect.height
firstkeyboardHeight = keyboardHeight
}
}
#IBAction func editTextButtonTapped(_ sender: UIButton) {
myTextField.becomeFirstResponder()
self.myTextField = UITextField(frame: CGRect(x: 0,y: self.firstkeyboardHeight, width: self.view.frame.width, height: 50.0))
self.myTextField.backgroundColor = .green
self.Gestures(addGesture: self.myTextField)
self.view.addSubview(self.myTextField)
}

There is only this set of notifications:
[ UIResponder.keyboardWillShowNotification, UIResponder.keyboardWillHideNotification, UIResponder.keyboardDidShowNotification, UIResponder.keyboardDidHideNotification]
Only inside can obtain the frame values:
NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillHideNotification,
object: nil,
queue: .main) { [weak self] notification in
guard let userInfo = notification.userInfo else {
return
}
let initialValue = userInfo[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue,
let finalValue = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue,
let durationValue = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? NSNumber,
let curveValue = userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? NSNumber
}
So, I don't think that there are any possible allowed ways.

You have several ways to do that. In either case I'd avoid recreating the textfield every time.
let myTextField: UITextField = {
let textfield = UITextField()
textfield.translatesAutoresizingMaskIntoConstraints = false
textfield.backgroundColor = .green
textfield.isHidden = true
return textfield
}()
var bottomConstraint: NSLayoutConstraint?
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(myTextField)
let bottomConstraint = myTextField.bottomAnchor.constraint(equalTo: view.bottomAnchor)
NSLayoutConstraint.activate([
myTextField.leadingAnchor.constraint(equalTo: view.leadingAnchor),
myTextField.trailingAnchor.constraint(equalTo: view.trailingAnchor),
myTextField.heightAnchor.constraint(equalToConstant: 50),
bottomConstraint
])
self.bottomConstraint = bottomConstraint
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
}
#objc func keyboardWillShow(_ notification: NSNotification) -> Void {
guard let keyboardFrame = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else { return }
bottomConstraint?.constant = -keyboardFrame.height
}
#IBAction func editTextButtonTapped(_ sender: UIButton) {
myTextField.becomeFirstResponder()
myTextField.isHidden = false
}

Related

Programmatically scroll ScrollView with StackView inside of it

I have a ScrollView with a StackView inside of it. When the keyboard appears I am changing the bottomConstraint .
1. view withouth keyboard
2. how it looks if keyboard shows
3. How it should look like
The problem is that I would like to scroll the ScrollView a bit up but I can not make it work.
scrollView.setContentOffset(CGPoint(x: x, y: y), animated: true) is NOT working. Ive tried it as you can see in the code but it has no effect:
Keyboard Observer methods
var keyboardHeight: CGFloat?
//MARK: keyboardObserver
#objc func keyboardWillShow(_ notification: Notification) {
if let keyboardFrame: NSValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
let keyboardRectangle = keyboardFrame.cgRectValue
self.keyboardHeight = keyboardRectangle.height
if self.passwordWiederholenTextField.isEditing {
scrollBottomViewConstraint.constant = -(self.keyboardHeight!)
self.theScrollView.setContentOffset(CGPoint(x: 0, y: 20), animated: true)
self.view.layoutIfNeeded()
}
}
}
#objc func keyboardWillHide(_ notification: Notification) {
if let keyboardFrame: NSValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
let keyboardRectangle = keyboardFrame.cgRectValue
self.keyboardHeight = keyboardRectangle.height
if self.passwordWiederholenTextField.isEditing {
scrollBottomViewConstraint.constant = 0
self.view.layoutIfNeeded()
}
}
}
Constraints:
theScrollView.topAnchor.constraint(equalTo: theLabel.bottomAnchor, constant: 20).isActive = true
theScrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 30).isActive = true
theScrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -30).isActive = true
scrollBottomViewConstraint = theScrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
scrollBottomViewConstraint.isActive = true
theStackView.topAnchor.constraint(equalTo: theScrollView.topAnchor).isActive = true
theStackView.leadingAnchor.constraint(equalTo: theScrollView.leadingAnchor).isActive = true
theStackView.trailingAnchor.constraint(equalTo: theScrollView.trailingAnchor).isActive = true
theStackView.widthAnchor.constraint(equalTo: theScrollView.widthAnchor).isActive = true
stackViewBottomConstraint = theStackView.bottomAnchor.constraint(equalTo: theScrollView.bottomAnchor)
stackViewBottomConstraint.isActive = true
I couldn't find anything on this so if anyone has any idea why it is not working I am very grateful!
With #Arun's tip I managed to get it done. This is my final code and it works perfectly:
var keyboardHeight: CGFloat?
//MARK: keyboardObserver
#objc func keyboardWillShow(_ notification: Notification) {
if let keyboardFrame: NSValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
let keyboardRectangle = keyboardFrame.cgRectValue
self.keyboardHeight = keyboardRectangle.height
let activeField: UITextField? = [passwordTextField, passwordWiederholenTextField].first { $0.isFirstResponder }
switch activeField {
case passwordTextField:
let insets = UIEdgeInsets(top: 0, left: 0, bottom: keyboardHeight! + 130, right: 0)
theScrollView.contentInset = insets
theScrollView.scrollIndicatorInsets = insets
self.view.layoutIfNeeded()
case passwordWiederholenTextField:
let insets = UIEdgeInsets(top: 0, left: 0, bottom: keyboardHeight! + 30, right: 0)
theScrollView.contentInset = insets
theScrollView.scrollIndicatorInsets = insets
self.view.layoutIfNeeded()
default:
break
}
}
}
#objc func keyboardWillHide(_ notification: Notification) {
if let keyboardFrame: NSValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
let keyboardRectangle = keyboardFrame.cgRectValue
self.keyboardHeight = keyboardRectangle.height
theScrollView.contentInset = UIEdgeInsets.zero
theScrollView.scrollIndicatorInsets = UIEdgeInsets.zero
}
}

How to edit accessory view of keyboard shown from WKWebView?

I am using a WKWebView in my Swift app to present some textfields.
I set some appearance properties to match a specific design, in this case its background has to be blue.
But when the keyboard is triggered by the WKWebView, it does something with the appearance properties and shows the keyboard toolbar in a pale light appearance of my color, do you know why?
The only appearance manipulation on UIToolBar that somewhat worked is this one:
UIToolbar.appearance().backgroundColor = .blue
This is my problem:
This is my goal:
Found a way, ended up to swizzle UIToolbars. Hopefully everything is there, but you would get an idea. Swift 4:
class YourController: UIViewController {
#IBOutlet weak var webView: PWebView!
var toolbar : UIToolbar?
func viewDidLoad() {
webView.addInputAccessoryView(toolbar: self.getToolbar(height: 44))
}
func getToolbar(height: Int) -> UIToolbar? {
let toolBar = UIToolbar()
toolBar.frame = CGRect(x: 0, y: 50, width: 320, height: height)
toolBar.barStyle = .black
toolBar.tintColor = .white
toolBar.barTintColor = UIColor.blue
let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(onToolbarDoneClick(sender:)) )
let flexibleSpaceItem = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil )
toolBar.setItems([flexibleSpaceItem, doneButton], animated: false)
toolBar.isUserInteractionEnabled = true
toolBar.sizeToFit()
return toolBar
}
#objc func onToolbarDoneClick(sender: UIBarButtonItem) {
webView?.resignFirstResponder()
}
}
var ToolbarHandle: UInt8 = 0
extension WKWebView {
func addInputAccessoryView(toolbar: UIView?) {
guard let toolbar = toolbar else {return}
objc_setAssociatedObject(self, &ToolbarHandle, toolbar, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
var candidateView: UIView? = nil
for view in self.scrollView.subviews {
let description : String = String(describing: type(of: view))
if description.hasPrefix("WKContent") {
candidateView = view
break
}
}
guard let targetView = candidateView else {return}
let newClass: AnyClass? = classWithCustomAccessoryView(targetView: targetView)
guard let targetNewClass = newClass else {return}
object_setClass(targetView, targetNewClass)
}
func classWithCustomAccessoryView(targetView: UIView) -> AnyClass? {
guard let _ = targetView.superclass else {return nil}
let customInputAccesoryViewClassName = "_CustomInputAccessoryView"
var newClass: AnyClass? = NSClassFromString(customInputAccesoryViewClassName)
if newClass == nil {
newClass = objc_allocateClassPair(object_getClass(targetView), customInputAccesoryViewClassName, 0)
} else {
return newClass
}
let newMethod = class_getInstanceMethod(WKWebView.self, #selector(WKWebView.getCustomInputAccessoryView))
class_addMethod(newClass.self, #selector(getter: WKWebView.inputAccessoryView), method_getImplementation(newMethod!), method_getTypeEncoding(newMethod!))
objc_registerClassPair(newClass!)
return newClass
}
#objc func getCustomInputAccessoryView() -> UIView? {
var superWebView: UIView? = self
while (superWebView != nil) && !(superWebView is WKWebView) {
superWebView = superWebView?.superview
}
guard let webView = superWebView else {return nil}
let customInputAccessory = objc_getAssociatedObject(webView, &ToolbarHandle)
return customInputAccessory as? UIView
}
}
private var keyBordView: UIView?
override func viewDidLoad() {
super.viewDidLoad()
let webView = WKWebView.init(frame: view.bounds)
webView.loadHTMLString("<html><body><div contenteditable='true'></div></body></html>", baseURL: nil)
view.addSubview(webView)
for subview in webView.scrollView.subviews {
if subview.classForCoder.description() == "WKContentView" {
keyBordView = subview
}
}
NotificationCenter.default.addObserver(self, selector: #selector(keyboardShow), name: .UIKeyboardDidShow, object: nil)
// Do any additional setup after loading the view.
}
#objc private func keyboardShow() {
let keyboardToolbar = UIToolbar()
keyboardToolbar.backgroundColor = UIColor.blue
keyboardToolbar.sizeToFit()
let flexBarButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let doneBarButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(self.dismissKeyBord))
keyboardToolbar.items = [flexBarButton, doneBarButton]
keyBordView = keyboardToolbar
}

DatePicker opening on second click on TextField

I have a TextField on click of which a custom date picker should open. But on first click keyboard opens and on second click the date picker opens. Please help me how to show date picker on first click. eodDateTime my TextField here where i want date picker to show on click of textfield and show the selected date time in the same textfield
Here is my code:
import UIKit
class ATMDiscrepancyViewController: UIViewController, UIScrollViewDelegate,UITextFieldDelegate
{
var datePicker : UIDatePicker!
#IBOutlet weak var remarks: UITextField!
#IBOutlet weak var lastTransaction: UITextField!
#IBOutlet weak var eodDateTime: UITextField!
var atmData : SelectedATM!
#IBOutlet weak var scrollView: UIScrollView!
var activeField: UITextField?
var selectedDate : String = ""
var alertControllerScanCode:CustViewController!
let button = UIButton(type: UIButtonType.custom)
}
#IBOutlet weak var saveAction: UIButton!
func datePickerValuechanged (sender : UIDatePicker)
{
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = DateFormatter.Style.medium
dateFormatter.timeStyle = DateFormatter.Style.none
eodDateTime.text = dateFormatter.string(from: sender.date)
selectedDate = dateFormatter.string(from: sender.date)
self.view.endEditing(true)
}
#IBAction func eodAction(_ sender: UITextField) {
deregisterFromKeyboardNotifications()
self.button.isHidden=true
self.pickUpDate(eodDateTime)
}
func pickUpDate(_ textField : UITextField){
deregisterFromKeyboardNotifications()
self.button.isHidden=true
self.button.isEnabled = false
NotificationCenter.default.addObserver(self, selector: #selector(ATMDiscrepancyViewController.keyboardRemoveReturn(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
// DatePicker
self.datePicker = UIDatePicker(frame:CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 216))
self.datePicker.backgroundColor = UIColor.white
self.datePicker.datePickerMode = UIDatePickerMode.dateAndTime
textField.inputView = self.datePicker
// ToolBar
let toolBar = UIToolbar()
toolBar.barStyle = .default
toolBar.isTranslucent = true
toolBar.tintColor = UIColor(red: 92/255, green: 216/255, blue: 255/255, alpha: 1)
toolBar.sizeToFit()
// Adding Button ToolBar
let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(ATMDiscrepancyViewController.doneClick))
let spaceButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let cancelButton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(ATMDiscrepancyViewController.cancelClick))
toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
toolBar.isUserInteractionEnabled = true
textField.inputAccessoryView = toolBar
}
func doneClick() {
let dateFormatter1 = DateFormatter()
// let timeFormatter = DateFormatter()
dateFormatter1.dateStyle = .medium
dateFormatter1.timeStyle = .none
dateFormatter1.dateFormat = "yyyy-MM-dd HH:mm:SS"
eodDateTime.text = dateFormatter1.string(from: datePicker.date)
eodDateTime.resignFirstResponder()
}
func cancelClick() {
eodDateTime.resignFirstResponder()
}
func handleDatePicker(sender: UIDatePicker) {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
eodDateTime.text = dateFormatter.string(from: sender.date)
selectedDate = dateFormatter.string(from: sender.date)
}
func doneButton(sender:UIButton)
{
eodDateTime.resignFirstResponder() // To resign the inputView on clicking done.
eodDateTime.text = selectedDate
}
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title = "Discrepancy"
registerForKeyboardNotifications()
button.setTitle("Return", for: UIControlState())
button.setTitleColor(UIColor.black, for: UIControlState())
button.frame = CGRect(x: 0, y: 163, width: 106, height: 53)
button.adjustsImageWhenHighlighted = false
button.addTarget(self, action: #selector(ATMDiscrepancyViewController.Done(_:)), for: UIControlEvents.touchUpInside)
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(ATMDiscrepancyViewController.dismissKeyboard))
view.addGestureRecognizer(tap)
self.remarks.delegate = self
self.lastTransaction.delegate = self
self.eodDateTime.delegate = self
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if textField == self.lastTransaction || textField == self.remarks {
let textString = (textField.text as! NSString).replacingCharacters(in: range, with: string)
return true
}
let allowedCharacters = CharacterSet.decimalDigits
let characterSet = CharacterSet(charactersIn: string)
return allowedCharacters.isSuperset(of: characterSet)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
self.lastTransaction.resignFirstResponder()
self.remarks.resignFirstResponder()
self.button.isHidden=true
self.lastTransaction.becomeFirstResponder()
dismissKeyboard()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
if textField == self.lastTransaction
{
if isEmptyValidate(edit: self.lastTransaction) == true
{
return false
}
self.button.isHidden = true
self.remarks.becomeFirstResponder()
}
if textField == self.remarks
{
if isEmptyValidate(edit: self.remarks)
{
return false
}
}
return true
}
func valid() -> Bool
{
if isEmptyValidate(edit: eodDateTime) == true
{
return false
}
return true
}
func isEmptyValidate(edit : UITextField) ->Bool
{
if edit.text?.isEmpty ?? true
{
edit.attributedPlaceholder = NSAttributedString(string: "Enter value", attributes: [NSForegroundColorAttributeName : UIColor.red])
return true
}
return false
}
func registerForKeyboardNotifications(){
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
func deregisterFromKeyboardNotifications(){
//Removing notifies on keyboard appearing
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
func keyboardWasShown(notification: NSNotification){
//Need to calculate keyboard exact size due to Apple suggestions
self.scrollView.isScrollEnabled = true
var info = notification.userInfo!
let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size
let contentInsets : UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize!.height, 0.0)
self.scrollView.contentInset = contentInsets
self.scrollView.scrollIndicatorInsets = contentInsets
var aRect : CGRect = self.view.frame
aRect.size.height -= keyboardSize!.height
if let activeField = self.activeField{
if (!aRect.contains(activeField.frame.origin)){
self.scrollView.scrollRectToVisible(activeField.frame, animated: true)
}
}
}
func keyboardWillBeHidden(notification: NSNotification){
//Once keyboard disappears, restore original positions
var info = notification.userInfo!
self.view.endEditing(true)
self.scrollView.isScrollEnabled = true
}
func textFieldDidBeginEditing(_ textField: UITextField){
if textField == self.lastTransaction {
//self.button.isHidden = true;
NotificationCenter.default.addObserver(self, selector: #selector(ATMDiscrepancyViewController.keyboardRemoveReturn(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
}
activeField = textField
NotificationCenter.default.addObserver(self, selector: #selector(ATMDiscrepancyViewController.keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
}
func textFieldDidEndEditing(_ textField: UITextField){
activeField = nil
}
func dismissKeyboard() {
view.endEditing(true)
}
func keyboardWillShow(_ note : Notification) -> Void{
DispatchQueue.main.async { () -> Void in
self.button.isHidden = false
self.scrollView.isScrollEnabled = true
var info = note.userInfo!
let keyBoardWindow = UIApplication.shared.windows.last
self.button.frame = CGRect(x: 0, y: (keyBoardWindow?.frame.size.height)!-53, width: 106, height: 53)
keyBoardWindow?.addSubview(self.button)
keyBoardWindow?.bringSubview(toFront: self.button)
let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size
let contentInsets : UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize!.height, 0.0)
self.scrollView.contentInset = contentInsets
self.scrollView.scrollIndicatorInsets = contentInsets
var aRect : CGRect = self.view.frame
aRect.size.height -= keyboardSize!.height
if let activeField = self.activeField {
if (!aRect.contains(activeField.frame.origin)){
self.scrollView.scrollRectToVisible(activeField.frame, animated: true)
}
}
UIView.animate(withDuration: (((note.userInfo! as NSDictionary).object(forKey: UIKeyboardAnimationCurveUserInfoKey) as AnyObject).doubleValue)!, delay: 0, options: UIViewAnimationOptions.curveEaseIn, animations: { () -> Void in
self.view.frame = self.view.frame.offsetBy(dx: 0, dy: 0)
}, completion: { (complete) -> Void in
})
}
}
func Done(_ sender : UIButton){
DispatchQueue.main.async { () -> Void in
self.textFieldShouldReturn(self.activeField!)
}
}
func keyboardRemoveReturn(_ note : Notification) -> Void{
DispatchQueue.main.async { () -> Void in
self.button.isHidden = true
UIView.animate(withDuration: (((note.userInfo! as NSDictionary).object(forKey: UIKeyboardAnimationCurveUserInfoKey) as AnyObject).doubleValue)!, delay: 0, options: UIViewAnimationOptions.curveEaseIn, animations: { () -> Void in
self.view.frame = self.view.frame.offsetBy(dx: 0, dy: 0)
}, completion: { (complete) -> Void in
})
}
}
}
Thanks
Make first responder on button click
#IBAction func eodAction(_ sender: UITextField) {
deregisterFromKeyboardNotifications()
self.button.isHidden=true
eodDateTime.becomeFirstResponder()
// self.pickUpDate(eodDateTime)
}
And implement textFieldShouldBeginEditing method to show date picket in input view
extension ATMDiscrepancyViewController: UITextFieldDelegate {
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
if textField == eodDateTime {
self.pickUpDate(eodDateTime)
}
}
}
I could not understand the whole code but the best way to fix this would be to assign the picker as inputView in viewDidLoad. So place the following code, you are using, in viewDidLoad and it should work fine :
self.datePicker = UIDatePicker(frame:CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 216))
self.datePicker.backgroundColor = UIColor.white
self.datePicker.datePickerMode = UIDatePickerMode.dateAndTime
textField.inputView = self.datePicker

Label is attached to the keyboard

How to add a block with the send button so that it moves with the keyboard. And also that would be textview behaved the same.
preview
Add this code in your controller
func createInputAccessoryView () -> UIToolbar {
let toolbarAccessoryView = UIToolbar(frame: CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height: 44))
toolbarAccessoryView.barStyle = .default
toolbarAccessoryView.tintColor = UIColor.blue
let flexSpace = UIBarButtonItem(barButtonSystemItem:.flexibleSpace, target:nil, action:nil)
let doneButton = UIBarButtonItem(barButtonSystemItem:.done, target:self, action:Selector(("doneTouched")))
toolbarAccessoryView.setItems([flexSpace, doneButton], animated: false)
return toolbarAccessoryView
}
#objc func doneTouched() {
/* Your action goes here */
}
Now add this in your viewDidLoad or anywhere
yourTextView.inputAccessoryView = createInputAccessoryView ()
Design the block view and then attach it's bottom constraint as IBOutlet and do this
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
#objc func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
self.viewBotCon.constant = -1 * keyboardSize.height
self.view.layoutIfNeeded()
}
}
#objc func keyboardWillHide(notification: NSNotification) {
self.viewBotCon.constant = 0
self.view.layoutIfNeeded()
}

Container View Wont Raise

I have some code that is supposed to raise a container view when the keyboard comes in to enter some text. I feel like I have definitely
implemented this right but the text view is not raising.
var bottomConstraint: NSLayoutConstraint?
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "Comments"
collectionView?.backgroundColor = UIColor.white
self.navigationItem.hidesBackButton = true
let backButton = UIBarButtonItem(image: UIImage(named: "icons8-Back-64"), style: .plain, target: self, action: #selector(GoBack))
self.navigationItem.leftBarButtonItem = backButton
self.collectionView?.register(CommentCell.self, forCellWithReuseIdentifier: cellID)
collectionView?.scrollIndicatorInsets = UIEdgeInsets(top: 0, left: 0, bottom:-50, right: 0)
collectionView?.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: -50, right: 0)
collectionView?.alwaysBounceVertical = true
collectionView?.keyboardDismissMode = .interactive
setupKeyboardObserver()
view.addSubview(containerView)
view.addConstraintsWithFormat("H:|[v0]|", views: containerView)
view.addConstraintsWithFormat("V:[v0(48)]", views: containerView)
Here im setting the constant to 0
let bottomConstraint = NSLayoutConstraint(item: containerView, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1, constant: 0)
view.addConstraint(bottomConstraint)
NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardNotification), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardNotification), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
// Register cell classes
// self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cellID)
fetchComments()
}
Here im taking control of constant and making it react to the keyboard height
func handleKeyboardNotification(notification: NSNotification){
if let userinfo = notification.userInfo{
let keyboardFrame = (userinfo[UIKeyboardFrameEndUserInfoKey] as AnyObject).cgRectValue
bottomConstraint?.constant = -(keyboardFrame?.height)!
let isKeyboardShowing = notification.name == NSNotification.Name.UIKeyboardWillShow
bottomConstraint?.constant = isKeyboardShowing ? CGFloat(-(keyboardFrame?.height)!) : CGFloat(0)
}
}
Despite all this, the keyboard still covers the container view. WHen I change the constant manually it moves but these functions seem to have no effect at dynamically moving the view. I'm confused and any help will be rewarded with a shot at the WWE championship belt. No but seriously I would appreciate the help
What I am able to observe from your code is you are managing the scroll top to down and vice versa with single method you just need to do that with individual method one is while keyboard is appear and another one is while the keyboard is dismiss
Here is the sample snippet which may help you
add the different selector for each notification
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
Handle the keyboard appear and disappear event with following method
func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y == 0{
UIView.animate(withDuration: 0.1, animations: { () -> Void in
self.view.frame.origin.y -= keyboardSize.height
})
}
}
}
func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y != 0{
UIView.animate(withDuration: 0.1, animations: { () -> Void in
self.view.frame.origin.y += keyboardSize.height
})
}
}
}
Note:
Do not forgot to remove Observer while your view is going to disappear like
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil)
Your handleKeyboardNotification function is setting the constant back to 0 in the last line of the function.
Also, your constant shouldn't be negative keyboard value, it should be the height of the keyboard plus a margin if you need it.
Fixed it by changing bottomConstraint from let to var. Forgot let makes it immutable. Haha guess I'm the WWE champion

Resources