2017-09-20 5 views

답변

0

AppDelegate.swift에서 UINavigationController로 rootViewController를 설정하십시오.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
     // Override point for customization after application launch. 
     window = UIWindow(frame: UIScreen.main.bounds) 
     window?.makeKeyAndVisible() 
     window?.rootViewController = UINavigationController(rootViewController: ViewController()) 

     return true 
    } 

그런 다음 ViewController.swift에 제대로 UITableViewDataSource와 UITableViewDelegate 방법을 구현하여있는 TableView를 추가합니다. 그런 다음 UISearchController를 선언하고 하위 뷰로 추가하는 대신 tableHeaderView에 추가하십시오. 그런 다음 기본적으로 searchController가 숨겨 지도록 tableView의 내용 오프셋을 설정하십시오. tableView가 아래로 스크롤되면 searchBar 만 표시됩니다. 있는 tableView 아래로 스크롤 한 후 이 enter image description here

, 다음 검색 창이 나타납니다 :

import UIKit 

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

    private var myData: NSArray = ["One", "Two", "Three"] 

    private var tableView: UITableView! 

    //lazy var searchBar:UISearchBar = UISearchBar() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
     navigationItem.title = "Home" 

     tableView = UITableView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)) 

     tableView.register(UITableViewCell.self, forCellReuseIdentifier: "MyCell") 
     tableView.dataSource = self 
     tableView.delegate = self 

     self.tableView.backgroundColor = .brown 

     let searchBarController: UISearchController = UISearchController(searchResultsController: nil) 

     tableView.tableHeaderView = searchBarController.searchBar 

     tableView.setContentOffset(CGPoint.init(x: 0, y: 44), animated: false) 

     self.view.addSubview(tableView) 

    } 

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
     print("\(indexPath.row)") 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return myData.count 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) 
     cell.textLabel?.text = myData[indexPath.row] as! String 
     cell.backgroundColor = .brown 
     return cell 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 


} 

응용 프로그램을 처음 시작되는 초기에 검색 창 만있는 tableView가 표시됩니다 숨겨져

enter image description here

+0

감사합니다. @bthapa !! 나는 동일한 것을 구현할 수있다. 탐색 막대 위에 검색 막대를 놓을 때 취소 막대를 사용하여 탐색 막대 위로 어떻게 탐색 막대를 밀어 넣을 수 있는지 설명 할 수있다. –

+0

이렇게하려면 searchBarSearchButtonClicked 인 두 개의 UISearchBarDelegate 메서드를 추가해야한다. _ searchBar : UISearchBar) 및 searchBarCancelButtonClicked (_ searchBar : UISearchBar)입니다. searchBar를 두드렸을 때 이미 UINavigationBarController에 정의 된 setNavigationBarHidden()을 사용하여 navigationBar를 숨 깁니다. 여기 간단한 소스 코드의 완전한 소스 코드 링크가 있습니다. https://github.com/bthapa4791/TableViewWithSearchBarController – bthapa

+0

고맙습니다. @bthapa. 이 접근 방식을 따랐지만 searchBarSearchButtonClicked를 클릭하면 탐색이 숨겨져 있지만 searchBar가 navigationBar에 표시되지 않습니다. searchBarSearchButtonClicked를 클릭하면 navigationBarHidden = true를 수행하고 첨부 된 스크린 샷을 참조하십시오. 감사 !! –

0

내가 할 수있는 이 코드로 검색 바 숨기기

let searchController = UISearchController(searchResultsController: nil) 

override func viewDidLoad() { 
    super.viewDidLoad() 

    self.searchbar() 
    self.automaticallyAdjustsScrollViewInsets = false 
    tableView.contentInset = UIEdgeInsets.zero 
    tableView.tableHeaderView = searchController.searchBar 
    //Hide SearchBar 
    tableView.setContentOffset(CGPoint.init(x: 0, y: 44), animated: false) 
    } 

func searchbar() { 
    searchController.searchResultsUpdater = self as UISearchResultsUpdating 
    searchController.dimsBackgroundDuringPresentation = false 
    definesPresentationContext = true 

    }