//Lazy loading Tool bar PrivateLazy var Toolbar:uitoolbar =Uitoolbar ()//set the bottom bar Privatefunc Settoolbar () {//Adding child controlsView.addsubview (ToolBar)//Add constrainttoolbar.snp_makeconstraints {(make), Voidinchmake.bottom.left.right.equalTo (view) make.height.equalTo ( +) } //set toolbar by arrayvar items =[Uibarbuttonitem] ()//Add UibarbuttonitemLet itemsettings = [["ImageName":"compose_toolbar_picture"],["ImageName":"Compose_mentionbutton_background"], ["ImageName":"Compose_trendbutton_background"], ["ImageName":"Compose_emoticonbutton_background"], ["ImageName":"Compose_add_background"]] //Traverse Itemsettings forDictinchitemsettings {Let btn=UIButton (type:. Custom) Let ImageName= dict["ImageName"]!//set up a picturebtn.setimage (UIImage (Named:imagename), forstate:. Normal) Btn.setimage (UIImage (Named:imagename+"_highlighted"), Forstate:. Highlighted) let item=Uibarbuttonitem (CUSTOMVIEW:BTN) btn.sizetofit ()//Add item to arrayitems.append (item)//instanced SpringsLet space =Uibarbuttonitem (barbuttonsystemitem:. Flexiblespace, Target:nil, Action:nil) items.append (space)}//remove the last springitems.removelast ()//Add to ToolbarToolbar.items =Items}
Execution Result:
Add a toolbar to the controller (Swift language)