gpt4 book ai didi

swift - 当固定在 UINavigationBar 下方时,UISearchBar 没有居中

转载 作者:行者123 更新时间:2023-12-05 07:18:20 27 4
gpt4 key购买 nike

下面是配置 View 约束的代码:

func configNavigationBarConstraints() {
navigationBar.autoPinEdge(toSuperviewEdge: .leading)
navigationBar.autoPinEdge(toSuperviewEdge: .trailing)
if #available(iOS 11.0, *) {
navigationBar.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
return
}
navigationBar.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor).isActive = true
}

func configSearchBarConstraints() {
searchBar.autoPinEdge(.top, to: .bottom, of: navigationBar)
// taskHistorySearchBar.autoPinEdge(.top, to: .bottom, of: navigationBar, withOffset: 6) //This is a solution but no ideal, the height diff varies by device
searchBar.autoPinEdge(toSuperviewEdge: .left)
searchBar.autoPinEdge(toSuperviewEdge: .right)
}

这会导致此 View :

enter image description here

请注意,UISearchBar 未对齐,知道我能做什么吗?即使我通过约束将其设置为具有较高的高度值,中心也会偏移。

最佳答案

您可能设置了 UINavigationBar 的委托(delegate)和 UISearchBar 的委托(delegate),它们都扩展了 UIBarPositioningDelegate,所以您需要这样做:

extension ViewController: UINavigationBarDelegate {
func position(for bar: UIBarPositioning) -> UIBarPosition {
if bar is UINavigationBar {
return .topAttached
}
return .any
}
}

这只会应用 UINavigationBar.topAttached 设置。

关于swift - 当固定在 UINavigationBar 下方时,UISearchBar 没有居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58375346/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com