2017-11-21 2 views
0

NavigationBarUISearchBar이 있습니다. searchBar를 누르면 새보기가 나타나지만 내 검색 창을 계속 활성화 (동일한 navBar)합니다. 왜냐하면 그냥 pushViewController을 사용하면 새보기가 새 NavigationBar으로 표시되고 내 UISearchBar이 사라집니다.새로운보기이지만 여전히 동일한 탐색 모음이 있습니다

UISearchBar

:

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 


    } 

} 

답변

0

실제로 탐색 모음은 변경되지 않습니다, 그것은 여전히 ​​같은 객체의 여기 내 코드입니다. 그러나 탑 뷰 컨트롤러가 변경되면 탐색 컨트롤러가 새 뷰 컨트롤러의 탐색 항목 정보로 탐색 막대 내용을 업데이트합니다.

이렇게 두 가지 작업을 수행 할 수 있습니다. 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; 
}]; 
+0

답변 해 주셔서 감사합니다. 하지만 두 번째 옵션을 선택하면 코드를 어떻게 처리해야합니까? 나는 조금 주위를 수색했지만 여전히 코드를 작성해야하는 방법을 알지 못한다. 나는 Swift와 함께 꽤 새로운 사람이다. – Stc

+0

답안을 편집하고 샘플 코드를 추가했습니다. – Srdr

+0

내 질문을 편집하고 코드를 추가했습니다. 여전히 작동하지 않습니다. 이제 코드를 작성해야합니다. – Stc

관련 문제