탐색 항목에 검색 막대를 추가하는 새로운 방법에 문제가 있습니다.NavigationItem에 포함 된 깨진 UISearchBar 애니메이션
아래 그림에서 알 수 있듯이 두 개의 UIViewController가 차례로 있고 검색 막대가 있습니다. 문제는 검색 막대가 첫 번째보기 컨트롤러에서는 보이지만 두 번째보기 컨트롤러에서는 보이지 않을 때 생기는 애니메이션입니다. 검색 막대가 차지하는 영역이 화면에 머물러 갑자기 사라집니다.
코드는 아주 기본적인 (프로젝트에 다른 변경되지 있었다)된다
이(나는 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
}
}
아이디어가 있습니까?
장치를 체크인하십시오. 시뮬레이터 문제 일 수 있습니다. 'viewDidLoad()'(loadView() 대신)로 시도하십시오. – Krunal
아니요, 기기에서는 동일합니다. – BartoszCichecki
및 viewDidLoad 같은 결과가 있습니다. – BartoszCichecki