fix: searchbar wasn't correct display in ipad

This commit is contained in:
sunxiaojian 2021-04-12 12:49:01 +08:00
parent a007b7a980
commit e784e123c9
4 changed files with 56 additions and 8 deletions

View File

@ -20,7 +20,7 @@ extension SearchViewController {
searchingTableView.register(SearchBottomLoader.self, forCellReuseIdentifier: String(describing: SearchBottomLoader.self))
view.addSubview(searchingTableView)
searchingTableView.constrain([
searchingTableView.frameLayoutGuide.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
searchingTableView.frameLayoutGuide.topAnchor.constraint(equalTo: searchBar.bottomAnchor),
searchingTableView.frameLayoutGuide.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
searchingTableView.frameLayoutGuide.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
searchingTableView.frameLayoutGuide.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),

View File

@ -17,6 +17,12 @@ final class SearchViewController: UIViewController, NeedsDependency {
var disposeBag = Set<AnyCancellable>()
private(set) lazy var viewModel = SearchViewModel(context: context, coordinator: coordinator)
let statusBar: UIView = {
let view = UIView()
view.backgroundColor = Asset.Colors.Background.navigationBar.color
return view
}()
let searchBar: UISearchBar = {
let searchBar = UISearchBar()
searchBar.placeholder = L10n.Scene.Search.Searchbar.placeholder
@ -25,7 +31,6 @@ final class SearchViewController: UIViewController, NeedsDependency {
let micImage = UIImage(systemName: "mic.fill")
searchBar.setImage(micImage, for: .bookmark, state: .normal)
searchBar.showsBookmarkButton = true
searchBar.showsScopeBar = false
searchBar.scopeButtonTitles = [L10n.Scene.Search.Searching.Segment.all, L10n.Scene.Search.Searching.Segment.people, L10n.Scene.Search.Searching.Segment.hashtags]
searchBar.barTintColor = Asset.Colors.Background.navigationBar.color
return searchBar
@ -110,16 +115,15 @@ final class SearchViewController: UIViewController, NeedsDependency {
extension SearchViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = Asset.Colors.Background.systemGroupedBackground.color
let barAppearance = UINavigationBarAppearance()
barAppearance.configureWithTransparentBackground()
barAppearance.backgroundColor = Asset.Colors.Background.navigationBar.color
navigationItem.standardAppearance = barAppearance
navigationItem.compactAppearance = barAppearance
navigationItem.scrollEdgeAppearance = barAppearance
searchBar.delegate = self
navigationItem.titleView = searchBar
view.backgroundColor = Asset.Colors.Background.systemGroupedBackground.color
navigationItem.hidesBackButton = true
setupSearchBar()
setupScrollView()
setupHashTagCollectionView()
setupAccountsCollectionView()
@ -128,10 +132,28 @@ extension SearchViewController {
setupSearchHeader()
}
func setupSearchBar() {
searchBar.delegate = self
view.addSubview(searchBar)
searchBar.constrain([
searchBar.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
searchBar.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
searchBar.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
])
view.addSubview(statusBar)
statusBar.constrain([
statusBar.topAnchor.constraint(equalTo: view.topAnchor),
statusBar.leadingAnchor.constraint(equalTo: view.leadingAnchor),
statusBar.trailingAnchor.constraint(equalTo: view.trailingAnchor),
statusBar.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 3),
])
}
func setupScrollView() {
view.addSubview(scrollView)
scrollView.constrain([
scrollView.frameLayoutGuide.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
scrollView.frameLayoutGuide.topAnchor.constraint(equalTo: searchBar.bottomAnchor),
scrollView.frameLayoutGuide.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
scrollView.frameLayoutGuide.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
scrollView.frameLayoutGuide.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),

View File

@ -15,6 +15,8 @@ final class SearchingTableViewCell: UITableViewCell {
let _imageView: UIImageView = {
let imageView = UIImageView()
imageView.tintColor = Asset.Colors.Label.primary.color
imageView.layer.cornerRadius = 4
imageView.clipsToBounds = true
return imageView
}()

View File

@ -10,7 +10,31 @@ import UIKit
// Make status bar style adptive for child view controller
// SeeAlso: `modalPresentationCapturesStatusBarAppearance`
final class AdaptiveStatusBarStyleNavigationController: UINavigationController {
var viewControllersHiddenNavigationBar: [UIViewController.Type]
override var childForStatusBarStyle: UIViewController? {
return visibleViewController
visibleViewController
}
override init(rootViewController: UIViewController) {
self.viewControllersHiddenNavigationBar = [SearchViewController.self]
super.init(rootViewController: rootViewController)
self.delegate = self
}
@available(*, unavailable)
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
extension AdaptiveStatusBarStyleNavigationController: UINavigationControllerDelegate {
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
let isContain = self.viewControllersHiddenNavigationBar.contains { type(of: viewController) == $0 }
if isContain {
self.setNavigationBarHidden(true, animated: animated)
} else {
self.setNavigationBarHidden(false, animated: animated)
}
}
}