0

Problem relates to this. I'm trying to use a custom view that I made that has two TextField inputs and a button, I made it using IB its xib, I've placed it in my story board and have it set to be at the bottom of my view.

The problem comes in when I want to make ContactInfoView the keyboards accessory view.

class ViewController: UIViewController, UITextViewDelegate {

@IBOutlet var cameraPreview: UIView!
@IBOutlet weak var ContactInfoView: KeyboardAccessoryView!



override func viewDidLoad() {
    super.viewDidLoad()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)





        }
    }

}


override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    //cameraPreviewLayer!.frame = cameraPreview.bounds

    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillAppear"), name: UIKeyboardWillShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide"), name: UIKeyboardWillHideNotification, object: nil)
    ContactInfoView.NameInput.inputAccessoryView = ContactInfoView



}


override func viewDidDisappear(animated: Bool) {


    super.viewDidDisappear(animated)


    NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)


}

// keyboard stuff

var accessoryView = KeyboardAccessoryView(frame: CGRectZero);

override var inputAccessoryView: KeyboardAccessoryView {


    return accessoryView

}

override func becomeFirstResponder() -> Bool {


    return true
}

override func canBecomeFirstResponder() -> Bool {
    return true
}


func keyboardWillAppear() {
    print("Keyboard appeared")
}

func keyboardWillHide() {
    print("Keyboard hidden")
}

  }

ContactInfoView.NameInput.inputAccessoryView = ContactInfoView isn't placing the view on top of the keyboard.

I fooled out around with the code and it started to crash relating to the link provided, But trying that solution didn't work either.

Community
  • 1
  • 1
user1086377
  • 328
  • 5
  • 16

3 Answers3

4

According to the comment in the UITextView.h file:

@property (nullable, readwrite, strong) UIView *inputView;             
@property (nullable, readwrite, strong) UIView *inputAccessoryView;

Presented when object becomes first responder. If set to nil, reverts to following responder chain. If set while first responder, will not take effect until reloadInputViews is called.

You should call reloadInputViews method after setting input.. property.

BurtK
  • 1,016
  • 13
  • 12
1

Yes, This Looks Little Tricky since everything seems fine and same as everyone and the Xcode Auto Suggestion and Completion Tools Helps you to complete the predicted "override" Functions..

Here Is A simple Mistakes that I made While Showing "inputAccessoryView"

I Entered Xcode Suggested Text For Completing "inputAccessoryView" And "inputAccessoryView" for Override Function.

But For Adding "override var inputAccessoryView: UIView?" Func its okay To Select from Xcode Suggestion / Auto Completion

    override var inputAccessoryView: UIView? {
        get {
           return inputContainerView
        }
    }

For "override var canBecomeFirstResponder : Bool" you should make sure that you type ": Bool" and not the "() -> Bool", Like I made mistakes couple of time.

So Please If you want You can type yourself or just Copy Below Code Paste in your Class Anywhere and it will work Smoothly.

 override var inputAccessoryView: UIView? {
    get {
        let inputContainerView = UIView()
        inputContainerView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 50)
        inputContainerView.backgroundColor = UIColor.gray

        let textfield = UITextField()
        textfield.text = "asasfdfs df sdf sdf ds f"
        textfield.frame = CGRect(x: 0, y: 0, width: inputContainerView.frame.size.width, height: 50)
        inputContainerView.addSubview(textfield)

        // You can add Any Other Objects Like UIButton, Image, Label    //you want And Constraints too.


        return inputContainerView
    }
}

override var canBecomeFirstResponder : Bool {
    return true
}
0

In the code above, you are assigning the property InputAccessoryView not the property inputAccessoryView. Case is important.

Assuming everything else is setup correctly, this should get it to work, but there is something I don't understand. Why is your text field/text view a property of your input accessory view? If NameInput is a subview of ContactInfoView, then setting ContactInfoView.NameInput.inputAccessoryView will not work.

Jeffery Thomas
  • 42,202
  • 8
  • 92
  • 117
  • ContactInfoView has a UITextFiels in it NameInput I want ContactInfoView to be at the bottom of the phone and When the InputName is tapped it because the accossoryView like how iMessanger does it. That make more sense? How else am I supposed to do it? – user1086377 Sep 19 '15 at 19:36
  • I've also tried adding a dummy UItextField to the storyboard and setting its inputAccessoryView to ContactInfoView and I still get the error mentioned it the link. I've tried removing it from the super view as suggested and it still doesn't work. – user1086377 Sep 19 '15 at 19:40
  • So I found [this](http://derpturkey.com/uitextfield-docked-like-ios-messenger/) post. I think it's what you are basing your code on. I would recommend trying the second method. I just don't understand how the first method works. Simply put, a view cannot have two superviews and I don't see how a view have both the keyboard and the view controller as it's superview. – Jeffery Thomas Sep 19 '15 at 22:22