我在tableView 的标题视图中有一个搜索栏,我没有更改它的高度(默认为 56pt)。当搜索栏处于活动状态并且其位置位于屏幕顶部时,它的高度变为 50pt。它都将发生在 iPhone 或模拟器(iOS 11 版本)上。
首次出现:
<UISearchBar: 0x7f94b6646900; frame = (0 0; 375 56); text = ''; gestureRecognizers = <NSArray: 0x60400025fc20>; layer = <CALayer: 0x604000225fe0>>
编辑:
<UISearchBar: 0x7f94b6646900; frame = (0 14; 375 50); text = ''; autoresize = W+TM; gestureRecognizers = <NSArray: 0x60400025fc20>; layer = <CALayer: 0x604000225fe0>>
感谢您的任何想法。
编辑1: 这是我的代码:
fileprivate func configSearchBar() -> Void {
let bar = self.searchBar
var size = bar.frame.size
let statusBarHeight = UIApplication.shared.statusBarFrame.size.height
bar.setBackgroundImage(UIImage.imageWith(color: .white, size: size),
for: .any,
barMetrics: .default)
let colorWhenEditing = UIColor.colorFromHexString("#FAFAFA")
if UIDevice.isIPHONEX() {
bar.barTintColor = colorWhenEditing
} else {
bar.setBackgroundImage(UIImage.imageWith(color: colorWhenEditing, size: CGSize(width: size.width, height: size.height + statusBarHeight)),
for: .topAttached,
barMetrics: .default)
}
size = CGSize(width: bar.frame.size.width - 30, height: 30)
let image = UIImage.imageWith(color: UIColor(hexString: "#F0F0F0"), size: size)?.zoom(toSize: size, cornerRadius: 4)
bar.setSearchFieldBackgroundImage(image, for: .normal)
bar.searchTextPositionAdjustment = UIOffset(horizontal: 5, vertical: 0)
}
self.tableView.tableHeaderView = self.searchBar
Edit2: - 搜索控制器和搜索栏初始化:
// Main view controller
lazy var searchResultController: UISearchController = {
let vc = MailSearchResultViewController(nibName: nil, bundle: nil)
let sc = UISearchController(searchResultsController: vc)
sc.searchResultsUpdater = self
sc.modalPresentationCapturesStatusBarAppearance = true
sc.delegate = self
return sc
}()
var searchBar: UISearchBar {
return searchResultController.searchBar
}
// MailSearchResultViewController - viewDidLoad:
self.edgesForExtendedLayout = []
self.automaticallyAdjustsScrollViewInsets = false