Let's take the right button for example, the left method is similar
Method One, direct custom text
- Let Item=uibarbuttonitem (title: "Share", Style:uibarbuttonitemstyle. Plain, target: Self , action: nil)
- Self. Navigationitem. Rightbarbuttonitem=item
Method Two, use the System icon
- Let item1=uibarbuttonitem (Barbuttonsystemitem:uibarbuttonsystemitem. Done, target: Self , action: nil)//The icon used here Uibarbuttonsystemitem is an enumeration. You can try out what the other values are.
- Self. Navigationitem. Rightbarbuttonitem=item1
Method Three, using a custom control
Here we customize the button as an example to implement
- let btn1=uibutton (frame: CGRectMake (0, 0, 5< Span class= "Xcodenumber" >0, 30))
- btn1 . Settitle ( forstate: uicontrolstate
- let item 2=uibarbuttonitem (Customview: btn1)
- self.navigationitem.rightbarbuttonitem=item2&NBSP;&NBSP;
Method Four, we can also directly put a picture up
- var img=uiimage (named: "test_img")
- Let item3=uibarbuttonitem (image:img, style:uibarbuttonitemstyle. Plain, target: Self , action: nil)
- Self. Navigationitem. Rightbarbuttonitem=item3
If we just follow the above method, we will find out is a solid color picture, is because the iOS flat design style application to make such a, if you need a real picture, we can set a img=img? Imagewithrenderingmode (uiimagerenderingmode.alwaysoriginal)
The above method is to add a button
What do I do if I want to add more than one time?
We found that the way we assign values to the right button is that Rightbarbuttonitem actually has properties Rightbarbuttonitems
Look at the code
- let items1=uibarbuttonitem ( Barbuttonsystemitem: uibarbuttonsystemitem target: self, action: nil)
- Let items2=uibarbuttonitem (Barbuttonsystemitem: uibarbuttonsystemitem target: self, action: nil)
- self.navigationitem1,items2]&NBSP;&NBSP;
If you want to set the left button directly call Self.navgigationItem.leftBarButtonItem can
Swift settings navigation left and right side buttons