Using Navigationitem.leftbarbuttonitem and Navigationitem.rightbarbuttonitem makes it easy to set the left and right buttons for the navigation bar separately, but only one button on the side.
If you want to use multiple buttons on one side, you can do so by Leftbarbuttonitems and Rightbarbuttonitems.
Here's a demonstration of adding two buttons to the right of the navigation bar (search and Settings):
The spacing between the two buttons is set and the Settings button is placed on the edge.
Import Uikit
Class Viewcontroller:uiviewcontroller {
Override Func Viewdidload () {
Super.viewdidload ()
Search button
Let button1 = UIButton (frame:cgrectmake (0, 0, 18, 18))
Button1.setimage (UIImage (named: "Search"), forstate:. Normal)
Button1.addtarget (Self,action:selector ("tapped1"), forControlEvents:. Touchupinside)
Let BarButton1 = Uibarbuttonitem (customview:button1)
Set button
Let button2 = UIButton (frame:cgrectmake (0, 0, 18, 18))
Button2.setimage (UIImage (named: "Settings"), Forstate:. Normal)
Button2.addtarget (Self,action:selector ("Tapped2"), forControlEvents:. Touchupinside)
Let BarButton2 = Uibarbuttonitem (customview:button2)
The gap between the buttons
Let gap = Uibarbuttonitem (barbuttonsystemitem:. Fixedspace, Target:nil,
Action:nil)
Gap.width = 15;
To eliminate the right side of the gap, or the button is not on the top edge
Let spacer = Uibarbuttonitem (barbuttonsystemitem:. Fixedspace, Target:nil,
Action:nil)
Spacer.width =-10;
Set buttons (note order)
Self.navigationItem.rightBarButtonItems = [Spacer,barbutton2,gap,barbutton1]
}
Func tapped1 () {
Print ("Search button clicks")
}
Func Tapped2 () {
Print ("Set button clicks")
}
Override Func didreceivememorywarning () {
Super.didreceivememorywarning ()
}
}
All right, here's the example of multiple buttons on the iOS open navigation bar.