0

My scenario, I have created toolbarview top of the keyboard. I added UIView drag and drop on ViewController topside like below Image

enter image description here Here, iPhone X I can see perfect size width, but if I run other devices width changing. so toolbarview showing half size of width, not showing perfect width. How to set auto layout and constraints for this. How to fix this?

Ben Dev
  • 67
  • 8
  • 2
    if you could drag and drop to add it, you can also pin it in storyboard – Lu_ Aug 01 '19 at 05:56
  • how to do that? Sorry I can't get you @Lu_ – Ben Dev Aug 01 '19 at 05:57
  • Show us the constraints you applied to `Tool Bar` in Interface Builder – zaitsman Aug 01 '19 at 05:59
  • did you use storyboard ever before? you can search stack overflow for storyboard constraints – Lu_ Aug 01 '19 at 06:00
  • I didn't applied constraints for Tool Bar view. Thats my question how to do that, because its out of the iPhone frame ViewController right, so how to set constraints. please check above my all images. @zaitsman – Ben Dev Aug 01 '19 at 06:01
  • @Lu_ Please understand my question. the tool bar view out of the iPhone viewcontroller frame, So how to do constraints. If I click tool bar view and check the add new constraints popup, its all not editable. – Ben Dev Aug 01 '19 at 06:04
  • 1
    you didn't show any code, there is no screenshots from visual debugger, it is hard to understand question like that – Lu_ Aug 01 '19 at 06:05
  • Please read this https://developer.apple.com/library/archive/documentation/UserExperience/Conceptual/AutolayoutPG/WorkingwithConstraintsinInterfaceBuidler.html – Eddwin Paz Aug 01 '19 at 06:08
  • @Lu_ I already added Images. Here, nothing to show code. Just I want to know how to set constraints for above mention toolbar view. Thats it. – Ben Dev Aug 01 '19 at 06:09
  • @BenDev how do you make this view appear above the keyboard without adding it to your viewcontroller? never heard of that before like that. – zaitsman Aug 01 '19 at 06:13
  • @zaitsman please check this https://stackoverflow.com/questions/23904848/how-can-i-add-a-toolbar-above-the-keyboard/23904935#23904935 – Ben Dev Aug 01 '19 at 06:16
  • @BenDev so you have code, you can set the constraints in code then – Lu_ Aug 01 '19 at 06:21

1 Answers1

2

I added view just like you

enter image description here

with constraints

enter image description here

Added this view as inputAccessoryView

myTextField.inputAccessoryView = viewAccessory

And its working fine

enter image description here


Edit - ToolBar as InputAccesory

You can achieve this with toolbar (w/o any constraint headache). Check below steps.

Select a toolbar like below and add this tool bar is place of your view.

enter image description here

enter image description here

Now add buttons like shown in above image by dragging and dropping UIBarButtonItem and assign images each BarButton. To make it flexible width and equally distributed just add Flexible Space Bar Button Item.

enter image description here

Now your toolbar hierarchy should be like below

enter image description here

And in your code

@IBOutlet var toolBarAccessory: UIToolbar!
@IBOutlet weak var myTextField: UITextField!

override func viewDidLoad() {
    super.viewDidLoad()
    myTextField.delegate = self
    myTextField.inputAccessoryView = toolBarAccessory

}

Output:

enter image description here

dahiya_boy
  • 7,885
  • 1
  • 25
  • 39
  • wow. this is I am expected. I have same like this but not working anyway I am dong double check, I will add my constraint screen. thanks man – Ben Dev Aug 01 '19 at 06:24
  • please check above updated my question. still its not working. Please let me know if I did anything mistakes. – Ben Dev Aug 01 '19 at 06:37
  • @BenDev Check updated answer. You dont need any single constraint. If you wanted to try new update then go with it. – dahiya_boy Aug 01 '19 at 06:59