You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:UISearchController点击时内容提前跳转致动画异常问题

UISearchController Animation Glitch: Content Jumps to Safe Area Top Prematurely

Hey folks, I'm dealing with a frustrating animation bug when using UISearchController. When I tap to activate the search bar, the content underneath it snaps up to the top of the view.safeAreaLayoutGuide before the search controller's animation completes—making the whole transition feel janky and unsmooth.

I've replicated this issue in a stripped-down test project, and you can see exactly what's happening in a demo animation.

Here's the relevant code from my project:

ViewController.swift

private lazy var text: UILabel = { 
    let label = UILabel() 
    label.text = "dfgh" 
    label.translatesAutoresizingMaskIntoConstraints = false 
    return label 
}()

override func viewDidLoad() { 
    super.viewDidLoad() 
    view.backgroundColor = .white 
    setupSearchController() 
    setupLabel() 
}

func setupSearchController() { 
    let searchController = UISearchController(searchResultsController: nil) 
    searchController.searchBar.placeholder = "Search for names and emails" 
    navigationItem.searchController = searchController 
}

func setupLabel() { 
    view.addSubview(text) 
    text.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true 
    text.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true 
}

SceneDelegate.swift

guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
let vc = ViewController()
let nc = UINavigationController(rootViewController: vc)
window?.rootViewController = nc
window?.makeKeyAndVisible()

I've also shared the full test project via a Git repo. Does anyone know how to fix this animation glitch so the content transitions smoothly alongside the search controller? Thanks a ton for any help!

内容的提问来源于stack exchange,提问作者Carlos Damião

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 08:22:33