代码之家  ›  专栏  ›  技术社区  ›  Stc

新视图,但仍具有相同的导航栏

  •  0
  • Stc  · 技术社区  · 7 年前

    我有一个 NavigationBar 具有 UISearchBar . 当我按下搜索栏时,我希望出现一个新视图,但仍保持我的搜索栏处于活动状态(相同的导航栏)。因为如果我只是使用 pushViewController 是否以新视图显示新视图 还有我的 搜索栏 消失。这是我的代码:

    这个 搜索栏 :

    extension HomeController {
    
    func setupNavigationBar() {
    
        searchBar.delegate = self
        searchBar.searchBarStyle = UISearchBarStyle.minimal
        searchBar.placeholder = "Search"
        searchBar.barTintColor = UIColor.gray
        navigationItem.titleView = searchBar
    
    }
    
    public func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
        searchBar.showsCancelButton = true
        let layout = UICollectionViewFlowLayout()
        let firstVC = HomeController(collectionViewLayout: layout)
        let secondVC = UserSearchController(collectionViewLayout: layout)
    
    
        self.addChildViewController(secondVC)
        secondVC.view.frame = firstVC.view.bounds;
        self.view.addSubview(secondVC.view)
        secondVC.didMove(toParentViewController: self)
    
        secondVC.view.frame = CGRect(x: firstVC.view.frame.size.width, y: secondVC.view.frame.origin.y, width: secondVC.view.frame.width, height: secondVC.view.frame.size.height)
        UIView.animate(withDuration: 0.33) {
            secondVC.view.frame = firstVC.view.bounds;
        }
    
    }
    
    
    
    }
    
    public func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
        searchBar.text = nil
        searchBar.endEditing(true)
        searchBar.showsCancelButton = false
    
    
    }
    
    
    func hideSearchBar() {
        navigationItem.setRightBarButton(searchBarButtonItem, animated: true)
        label.alpha = 0
        UIView.animate(withDuration: 0.3, animations: {
            self.navigationItem.titleView = self.label
            self.label.alpha = 1
            }, completion: { finished in
    
            })
        }
    
    }
    

    新视图:

    import UIKit
    
    
    class UserSearchController: UICollectionViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            collectionView?.backgroundColor = UIColor.blue
    
    
        }
    
    }
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Srdr    7 年前

    所以,你可以做两件事; 1-您可以直接将搜索栏添加为导航栏的子视图,而不是通过导航项。因此,导航控制器不会更改有关该栏的任何内容。但我个人不建议这样做,因为你们需要自己处理搜索栏的可见性。

    2-您可以将第二个视图控制器作为子视图控制器添加到第一个视图控制器,并在屏幕上适当设置其动画,而不是按下第二个视图控制器。由于导航控制器的俯视图控制器不变,导航栏内容保持不变。

    第二个选项的代码示例;

    // Add the second view controller as a child of the first view controller
    [firstVC addChildViewController:secondVC];
    secondVC.view.frame = firstVC.view.bounds;
    [firstVC.view addSubview:secondVC.view];
    [secondVC didMoveToParentViewController:firstVC];
    
    // Now you can animate it as if it's pushed to the navigation controller, or anyway else you want
    secondVC.view.frame = CGRectMake(firstVC.view.frame.size.width, secondVC.view.frame.origin.y, secondVC.view.frame.size.width, secondVC.view.frame.size.height);
    [UIView animateWithDuration:0.33 animations:^{
        secondVC.view.frame = firstVC.view.bounds;
    }];