2017-09-19 4 views
8

탐색 항목에 검색 막대를 추가하는 새로운 방법에 문제가 있습니다.NavigationItem에 포함 된 깨진 UISearchBar 애니메이션

아래 그림에서 알 수 있듯이 두 개의 UIViewController가 차례로 있고 검색 막대가 있습니다. 문제는 검색 막대가 첫 번째보기 컨트롤러에서는 보이지만 두 번째보기 컨트롤러에서는 보이지 않을 때 생기는 애니메이션입니다. 검색 막대가 차지하는 영역이 화면에 머물러 갑자기 사라집니다.

Demo

코드는 아주 기본적인 (프로젝트에 다른 변경되지 있었다)된다

(나는 C#에서 주로 쓰기 때문에이 코드에 오류가있을 수 있습니다.)

ViewController.swift :

import UIKit 

class ViewController: UITableViewController, UISearchResultsUpdating { 

override func loadView() { 
    super.loadView() 

    definesPresentationContext = true; 

    navigationController?.navigationBar.prefersLargeTitles = true; 
    navigationItem.largeTitleDisplayMode = .automatic; 
    navigationItem.title = "VC" 

    tableView.insetsContentViewsToSafeArea = true; 
    tableView.dataSource = self; 

    refreshControl = UIRefreshControl(); 
    refreshControl?.addTarget(self, action: #selector(ViewController.handleRefresh(_:)), for: UIControlEvents.valueChanged) 
    tableView.refreshControl = refreshControl; 

    let stvc = UITableViewController(); 
    stvc.tableView.dataSource = self; 

    let sc = UISearchController(searchResultsController: stvc); 
    sc.searchResultsUpdater = self; 
    navigationItem.searchController = sc; 
} 

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    var cell = tableView.dequeueReusableCell(withIdentifier: "cell1"); 
    if (cell == nil) { 
     cell = UITableViewCell(style: .default, reuseIdentifier: "cell1"); 
    } 
    cell?.textLabel?.text = "cell " + String(indexPath.row); 
    return cell!; 
} 

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 20; 
} 

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    let vc = ViewController(); 
    navigationController?.pushViewController(vc, animated: true); 
} 

@objc func handleRefresh(_ refreshControl: UIRefreshControl) { 
    DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(2), execute: { 
     refreshControl.endRefreshing(); 
    }) 
} 

func updateSearchResults(for searchController: UISearchController) { 
} 
} 

AppDelegate.swift :

import UIKit 

@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate { 

var window: UIWindow? 

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 

    window = UIWindow(frame: UIScreen.main.bounds); 
    window?.rootViewController = UINavigationController(rootViewController: ViewController()); 
    window?.makeKeyAndVisible(); 

    UINavigationBar.appearance().barTintColor = UIColor.red; 

    return true 
} 
} 

아이디어가 있습니까?

+0

장치를 체크인하십시오. 시뮬레이터 문제 일 수 있습니다. 'viewDidLoad()'(loadView() 대신)로 시도하십시오. – Krunal

+0

아니요, 기기에서는 동일합니다. – BartoszCichecki

+0

및 viewDidLoad 같은 결과가 있습니다. – BartoszCichecki

답변

4

애플은 여전히 ​​새로운 대형 타이틀 스타일에서 UISearchBar의 사용을 철저히해야 할 것처럼 보입니다. UIViewController에 푸시 버튼의 navigationItem.searchController 세트가 없으면 애니메이션이 정상적으로 작동합니다. UIViewController의 두 인스턴스 사이를 탐색 할 때 searchController가 설정되면 내비게이션 막대의 높이가 이동하는 위치를 설명하는 문제가 발생합니다.

당신은 때마다 viewDidAppear가 호출되는 UISearchController을 만드는 (대신 loadView에 그것을 만들기) 및 viewDidDisappear에 전무로 navigationItem.searchController을 설정하여 (약 작업) 문제를 해결할 수 있습니다.

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Only one palette with a top boundary edge can be active outside of a transition. Current active palette is <_UINavigationControllerManagedSearchPalette: 0x7fad67117e80; frame = (0 116; 414 0); layer = <CALayer: 0x60400002c8e0>>' 

나는이 주변 단지 작품이다 알아요,하지만 희망이 뜻을 다음 viewDidAppear 방법에 navigationItem.searchController 인라인을 설정할 때

override func viewDidAppear(_ animated: Bool) { 
    super.viewDidAppear(animated) 

    DispatchQueue.main.async { 
     let stvc = UITableViewController() 
     stvc.tableView.dataSource = self 

     let sc = UISearchController(searchResultsController: stvc) 
     sc.searchResultsUpdater = self 
     self.navigationItem.searchController = sc 
    } 
} 

override func viewDidDisappear(_ animated: Bool) { 
    super.viewDidDisappear(animated) 

    self.navigationItem.searchController = nil 
} 

비동기 파견하는 이유는, 예외가 발생된다 Apple이 두 UISearchController을 가지고있는 두 개의보기 컨트롤러 사이를 탐색하여이 문제를 해결할 때까지 지금 도움을 받으십시오. navigationItem.

+1

iOS 11 ... 좋은 놓치기 사과 .. –