Use of Swift-navigation bar (Uinavigationbar)

Source: Internet
Author: User

Unlike the navigation controller (Uinavigationcontroller), both the navigation bar and the page switching function are implemented. The navigation bar (Uinavgationbar) can be used alone to add to any uiview. Uinavigationbar More important attributes are the left button, the middle caption, and the right button.
Here is a sample to use, click on the left plus will add a new navigation item, click on the right side cancel will remove the current topmost navigation item.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 import UIKitclass ViewController: UIViewController {        var count = 0    //声明导航条    var navigationBar:UINavigationBar?    override func viewDidLoad() {        super.viewDidLoad()        // Do any additional setup after loading the view, typically from a nib.        //实例化导航条        navigationBar = UINavigationBar(frame: CGRectMake(0, 20, 320, 44))        self.view.addSubview(navigationBar!)        onAdd()    }    override func didReceiveMemoryWarning() {        super.didReceiveMemoryWarning()        // Dispose of any resources that can be recreated.    }        //增加导航项函数    func onAdd(){        count++        //给导航条增加导航项        navigationBar?.pushNavigationItem(onMakeNavitem(), animated: true)    }        //删除导航项函数    func onRemove(){        if count > 1{            //减少导航项数量            count--            //从导航条中移除最后一个导航项            navigationBar?.popNavigationItemAnimated(true)        }    }        //创建一个导航项    func onMakeNavitem()->UINavigationItem{        var navigationItem = UINavigationItem()        //创建左边按钮        var leftBtn = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add,            target: self, action: "onAdd")        //创建右边按钮        var rightBtn = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Cancel,            target: self, action: "onRemove")        //设置导航栏标题        navigationItem.title = "第\(count)个导航项"        //设置导航项左边的按钮        navigationItem.setLeftBarButtonItem(leftBtn, animated: true)        //设置导航项右边的按钮        navigationItem.setRightBarButtonItem(rightBtn, animated: true)        return navigationItem    }}

Use of Swift-navigation bar (Uinavigationbar)

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.