Please tell me how to specify the location of the UIToolBar item by code.

Asked 2 years ago, Updated 2 years ago, 45 views

I programmed it so that Picker and Toolbar appear when I press TextField, but I don't know how to position the Toolbar item.Please tell me how to write it.The image looks like the following:

Enter a description of the image here

ios swift

2022-09-30 20:53

1 Answers

The placement of Bar Button Item in UIToolBar uses Fixed Space Bar Button Item and Flexible Space Bar Button Item to adjust the space between items.
On the storyboard, you can place items on both sides by inserting the Flexible Space Bar Button Item as shown below.

Enter a description of the image here

The program does the same thing.

An instance of UIToolBar is toolBar.

//Left Bar Button Item
letitem1 = UIBarButtonItem(title: "Item1", style: UIBarButtonItemStyle.Plain, target:nil, action:nil)
// Flexible Space Bar Button Item
let flexibleItem = UIBarButtonItem (barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target:nil, action:nil)
// Right Bar Button Item
let item2 = UIBarButtonItem(title: "Item2", style: UIBarButtonItemStyle.Plain, target:nil, action:nil)
// Arrange the three.
let items = [item1, flexibleItem, item2]
// Replace array with UIToolBar property items
toolBar.items=items


2022-09-30 20:53

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.