I am new to swift and am trying to achieve a rather simple task. I am creating an iPad application wherein I want to open the keyboard programmatically.
- Keyboard should have a textField bar on top to record what's typed using the keyboard.
- There should be a button right next to the textField.( as shown in the image )
I tried to achieve the same but doing this :
lazy var textFieldPanel: UIView = {
let view = UIView(frame: CGRect(x: 0.0, y: self.view.bounds.height, width: self.view.bounds.width, height: 50.0))
self.view.addSubview(view)
view.backgroundColor = UIColor.white
view.borderColor = UIColor.blue
view.borderWidth = 2.0
let fieldBottomConstraint = NSLayoutConstraint(item: view, attribute: .bottom, relatedBy: .equal, toItem: self.view, attribute: .bottom, multiplier: 1.0, constant: 0.0)
customTextFieldBottomConstraint = fieldBottomConstraint
view.translatesAutoresizingMaskIntoConstraints = false
self.view.addConstraint(.init(item: view, attribute: .left, relatedBy: .equal, toItem: self.view, attribute: .left, multiplier: 1.0, constant: 0.0))
self.view.addConstraint(.init(item: view, attribute: .right, relatedBy: .equal, toItem: self.view, attribute: .right, multiplier: 1.0, constant: 0.0))
self.view.addConstraint(fieldBottomConstraint)
self.view.addConstraint(.init(item: view, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 50.0))
return view
}()
lazy var customTextField: UITextField = {
let field = UITextField(frame: CGRect(x: 0.0, y: 0.0, width: self.view.bounds.width - 300 , height: 50.0))
textFieldPanel.addSubview(field)
field.translatesAutoresizingMaskIntoConstraints = false
self.view.addConstraint(.init(item: field, attribute: .left, relatedBy: .equal, toItem: textFieldPanel, attribute: .left, multiplier: 1.0, constant: 8.0))
self.view.addConstraint(.init(item: field, attribute: .right, relatedBy: .equal, toItem: textFieldPanel, attribute: .right, multiplier: 1.0, constant: 8.0))
self.view.addConstraint(.init(item: field, attribute: .top, relatedBy: .equal, toItem: textFieldPanel, attribute: .top, multiplier: 1.0, constant: 2.0))
self.view.addConstraint(.init(item: field, attribute: .bottom, relatedBy: .equal, toItem: textFieldPanel, attribute: .bottom, multiplier: 1.0, constant: 2.0))
let button = UIButton(frame: CGRect(x: self.view.bounds.width - 350, y: 0.0, width: 200 , height: 50.0))
button.backgroundColor = UIColor.clear
button.setTitle("Send", for: .normal)
button.borderWidth = 2.0
button.borderColor = UIColor.blue
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
textFieldPanel.addSubview(button)
button.translatesAutoresizingMaskIntoConstraints = false
return field
}()
and then calling it in the IBAction like :
customTextField.becomeFirstResponder()
customTextFieldBottomConstraint?.constant = -360.0
The issue that I am facing is the following :
- The view is comopletely distorted. ( as shown )

- Send button View is distorted as it can be seen in the image.
- While hiding the keyboard, the textField view still stays which I want it to be removed.
- The view is not shifting up when the keyboard launches.
Can anyone help me as to what am I doing wrong here. Any help would be appreciated.
