Swift places multiple button examples on the left or right side of the navigation bar

Source: Internet
Author: User

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.

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.