Swift settings navigation left and right side buttons

Source: Internet
Author: User
Tags vars

Let's take the right button for example, the left method is similar

Method One, direct custom text

    1. Let Item=uibarbuttonitem (title: "Share", Style:uibarbuttonitemstyle. Plain, target: Self , action: nil)
    2. Self. Navigationitem. Rightbarbuttonitem=item


Method Two, use the System icon

    1. 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.
    2. Self. Navigationitem. Rightbarbuttonitem=item1


Method Three, using a custom control

Here we customize the button as an example to implement

    1. let btn1=uibutton (frame:  CGRectMake (0, 0, 5< Span class= "Xcodenumber" >0, 30))   
    2. btn1 . Settitle ( forstate: uicontrolstate
    3. let item 2=uibarbuttonitem (Customview: btn1)   
    4. self.navigationitem.rightbarbuttonitem=item2&NBSP;&NBSP;


Method Four, we can also directly put a picture up

    1. var img=uiimage (named: "test_img")
    2. Let item3=uibarbuttonitem (image:img, style:uibarbuttonitemstyle. Plain, target: Self , action: nil)
    3. 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

    1. let items1=uibarbuttonitem ( Barbuttonsystemitem: uibarbuttonsystemitem target: self, action: nil)   
    2. Let items2=uibarbuttonitem (Barbuttonsystemitem: uibarbuttonsystemitem  target: self, action: nil)   
    3. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.