简体   繁体   中英

Swift UIView autolayout and constraints

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

在此处输入图片说明 Here, iPhone XI 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?

I added view just like you

在此处输入图片说明

with constraints

在此处输入图片说明

Added this view as inputAccessoryView

myTextField.inputAccessoryView = viewAccessory

And its working fine

在此处输入图片说明


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.

在此处输入图片说明

在此处输入图片说明

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 .

在此处输入图片说明

Now your toolbar hierarchy should be like below

在此处输入图片说明

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:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM