How to animate add UISearchBar on top of UINavigationBar

后端 未结 4 1118
傲寒
傲寒 2020-12-07 08:44

If I set the displaysSearchBarInNavigationBar = YES in viewDidLoad, the search bar will be in navigation bar when the view show up. But I want to s

4条回答
  •  一生所求
    2020-12-07 09:20

    Following Nick's answer, I made a similar one on Xcode 7.1 -swift 2.0.
    
    Note:
    To the Navigation Bar, I added
    
    (a) UIBarButtons( Drag& Drop) - menuButton & searchButton
    (b) UIBarButtons (programatically) - leftSearchBarButtonItem & rightSearchBarButtonItem.
    
    The common methods are : 
    (a) showSearchBar(), hideSearchBar()
    (b) revealToggle: - It is connected to SWRevealController for Slider Menu.
    

    //  DashBoardViewController.swift
    
    import UIKit
    
    class DashBoardViewController: UIViewController,UISearchBarDelegate,SWRevealViewControllerDelegate {
    
        //MARK:- STORYBOARD REFERENCE
        @IBOutlet weak var menuButton: UIBarButtonItem!
        @IBOutlet weak var searchButtton: UIBarButtonItem!
    
        //Making secondary Searchbar
        var searchBar = UISearchBar()
        var leftSearchBarButtonItem: UIBarButtonItem?
        var rightSearchBarButtonItem: UIBarButtonItem?
        var logoImageView   : UIImageView!
    
        override func viewDidLoad() {
    
            super.viewDidLoad()
            self.activateInitialUISetUp()
            //    self.revealViewController().delegate = self
            makeTopNavigationSearchbar()
    
    
        }
        override func viewWillAppear(animated: Bool) {
            makeTopNavigationSearchbar()
            activateInitialUISetUp()        
    
        }
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
        }
    
        //MARK:- SEARCHBAR METHODS
        func searchBarSearchButtonClicked(searchBar: UISearchBar) {
            hideSearchBar()
            searchBar.resignFirstResponder()
        }
        func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
    
        }
        func searchBarCancelButtonClicked(searchBar: UISearchBar) {
            hideSearchBar()
        }
    
        //Search Bar Appear & Disappear
        func showSearchBar() {
            searchBar.hidden =  false
            searchBar.alpha = 0
            navigationItem.titleView = searchBar
            navigationItem.setLeftBarButtonItem(nil, animated: true)
            navigationItem.setRightBarButtonItem(nil, animated: true)
    
            UIView.animateWithDuration(0.5, animations: {
                self.searchBar.alpha = 1
                }, completion: { finished in
                    self.searchBar.becomeFirstResponder()
            })
        }
    
        func hideSearchBar() {
            hideSearchBarAndMakeUIChanges()
            logoImageView.alpha = 0
            UIView.animateWithDuration(0.3, animations: {
    
                self.logoImageView.alpha = 1
                }, completion: { finished in
    
            })
        }
        //Making secondary Searchbar
        func makeTopNavigationSearchbar()
        {
            let logoImage = UIImage(named: "password")!
            logoImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: logoImage.size.width, height: logoImage.size.height))
            logoImageView.image = logoImage
            searchButtton.customView?.addSubview(logoImageView)
    
            searchBar.delegate = self
            searchBar.searchBarStyle = UISearchBarStyle.Minimal
    
            leftSearchBarButtonItem = navigationItem.leftBarButtonItem
            rightSearchBarButtonItem =  navigationItem.rightBarButtonItem
    
            leftSearchBarButtonItem?.tintColor =  UIColor.whiteColor()
            rightSearchBarButtonItem?.tintColor =  UIColor.whiteColor()
    
        }
    
        //Adding secondary uibar butttons to navigation bar
        func hideSearchBarAndMakeUIChanges ()
        {
            searchBar.hidden =  true
    
            //Adding secondary uibarbuttons to the nav bar and revoke its methods
            navigationItem.setLeftBarButtonItem(leftSearchBarButtonItem, animated: true)
            navigationItem.setRightBarButtonItem(rightSearchBarButtonItem, animated: true)
    
            leftSearchBarButtonItem?.title = "Menu"
            leftSearchBarButtonItem?.target = self.revealViewController()
            leftSearchBarButtonItem?.action = "revealToggle:"
    
            rightSearchBarButtonItem?.title = "Search"
            rightSearchBarButtonItem?.target = self
            rightSearchBarButtonItem?.action = "showSearchBar"
    
    
            //Adding Title Label
            var navigationTitlelabel = UILabel(frame: CGRectMake(0, 0, 200, 21))
            navigationTitlelabel.center = CGPointMake(160, 284)
            navigationTitlelabel.textAlignment = NSTextAlignment.Center
            navigationTitlelabel.textColor  = UIColor.whiteColor()
            navigationTitlelabel.text = "WORK ORDER"
            self.navigationController!.navigationBar.topItem!.titleView = navigationTitlelabel
    
        }    
    
        //UI-Related Methods
        func activateInitialUISetUp()
        {
            self.navigationController?.navigationBarHidden =  false
            self.navigationController?.navigationBar.barStyle = UIBarStyle.BlackOpaque
            self.navigationController?.navigationBar.translucent =  true
            self.navigationController?.navigationBar.backgroundColor =  UIColor.redColor()
    
            //Nav Bar Searchbar
            searchBar.delegate = self
            searchBar.placeholder = "Start Your Search Here"
            searchButtton.action = "showSearchBar"
            searchButtton.target = self
    
            //searchbar Text Color
            var textFieldInsideSearchBar = searchBar.valueForKey("searchField") as? UITextField
            textFieldInsideSearchBar?.textColor = UIColor.whiteColor()
    
    
            //Nav Bar Title
            self.title = "WORK ORDER"
    
            if self.revealViewController() != nil {
                menuButton.target = self.revealViewController()
                menuButton.action = "revealToggle:"
    
                self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
                self.revealViewController().rearViewRevealWidth = self.view.frame.width / 2
                self.revealViewController().rearViewRevealOverdraw = 0.0
                self.view.addGestureRecognizer(self.revealViewController().tapGestureRecognizer())
            }
        }    
    
        func revealController(revealController: SWRevealViewController!, didMoveToPosition position: FrontViewPosition) {
            if(position.rawValue == 3)
            {
    
    
            }
            else
            {
    
            }
            print("position\(position)")
    
        }    
    }
    

提交回复
热议问题