UISearchController Swift 4: Spacing Problems - ios

I'm adding a UISearchController but I keep experiencing spacing problems. In particular, when I conduct a search in the search bar, it shifts up and leaves a black space between the tableview and the search bar.
Then, when I type the the black space disappears but there's a large white space between the search bar and the table.
I tried adding the following line but the spacing problem becomes worse:
searchController.hidesNavigationBarDuringPresentation = false
Any helpful hints would be greatly appreciated.
Here is my current code:
class SearchViewController: UITableViewController {
var searchController: UISearchController!
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.clear
view.isOpaque = false
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
//Set up Table View
self.navigationController?.navigationBar.shadowImage = UIImage()
let searchResultsController = UITableViewController(style: .plain)
searchResultsController.tableView.delegate = self
searchResultsController.tableView.dataSource = self
searchResultsController.tableView.rowHeight = 65
searchResultsController.tableView.register(SearchCell.self, forCellReuseIdentifier: "SearchCell")
// Setup Search Controller
searchController = UISearchController(searchResultsController: searchResultsController)
searchController.searchBar.tintColor = UIColor.white
searchController.searchBar.barTintColor = UIColor(red: 34/255, green: 167/255, blue: 240/255, alpha: 1.0)
searchController.searchBar.layer.borderColor = UIColor(red: 34/255, green: 167/255, blue: 240/255, alpha: 1.0).cgColor
searchController.searchBar.layer.borderWidth = 1.00
tableView.tableHeaderView?.addSubview(searchController.searchBar)
let searchBar = searchController.searchBar
searchBar.sizeToFit()
searchBar.placeholder = "Search"
searchController.searchBar.delegate = self
searchController.searchResultsUpdater = self
definesPresentationContext = true
searchController.dimsBackgroundDuringPresentation = false
tableView.tableHeaderView = searchController.searchBar
}

try to Add
self.automaticallyAdjustsScrollViewInsets = false
self.extendedLayoutIncludesOpaqueBars = true
in viewDidload.
or uncheck "Adjust Scroll View Insets"

Related

Unable to change the color to tabBar?

I have the following method in the parantTabBarController class:
There can be seen various attempts made to make the tabBar completely transparent. The only one that worked is the one found at the top.
override func viewDidLoad() {
super.viewDidLoad()
UITabBar.appearance().barTintColor = UIColor.clear
UITabBar.appearance().backgroundImage = UIImage()
// UITabBar.appearance().barTintColor = UIColor.blue
// changeTabBarOpacity()
// self.tabBar.unselectedItemTintColor = UIColor(red: 17.0/255.0, green: 70.0/255.0, blue: 95.0/255.0, alpha: 0.4)
// self.tabBar.backgroundColor = UIColor(red: 17.0/255.0, green: 70.0/255.0, blue: 95.0/255.0, alpha: 0.0)
// self.tabBar.backgroundColor = UIColor.clear
// self.tabBar.backgroundImage = UIImage()
// self.tabBar.shadowImage = UIImage() // removes the border
}
However with this approach I am not able to change the background color of this same tabBar in other view controllers. I have tried replacing the image with a white image, changing the background color: UITabBar.appearance().backgroundColor = UIColor.white But nothing works.
How can I have a translucent tabBar on one page and a white one on all others?
I use this code to configure tab bar in custom subclass of UITabBarController. It supports iOS 15 and XCode 13 updates.
let backgroundColor = UIColor.red
let selectedItemTextColor = UIColor.black
let unselectedItemTextColor = UIColor.white
if #available(iOS 15, *) {
let tabBarAppearance = UITabBarAppearance()
tabBarAppearance.backgroundColor = backgroundColor
tabBarAppearance.stackedLayoutAppearance.selected.titleTextAttributes = [.foregroundColor: selectedItemTextColor]
tabBarAppearance.stackedLayoutAppearance.normal.titleTextAttributes = [.foregroundColor: unselectedItemTextColor]
tabBar.standardAppearance = tabBarAppearance
tabBar.scrollEdgeAppearance = tabBarAppearance
} else {
UITabBarItem.appearance().setTitleTextAttributes([.foregroundColor: selectedItemTextColor], for: .selected)
UITabBarItem.appearance().setTitleTextAttributes([.foregroundColor: unselectedItemTextColor], for: .normal)
tabBar.barTintColor = backgroundColor
}
Swift 5 :
Okay i found the solution, the main key is using the isTranslucent property of UITabBar.
If you send isTranslucent : true to a tab bar with an opaque custom
background image the tab bar will apply a system opacity less than 1.0 to the image.
if you want to set clear color then u just have to set isTranslucent to true only. and if you want to apply other colors then set isTranslucent to false.
Use the below TabBarViewController class to your TabBarViewController
class TabBarViewController: UITabBarController, UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
self.tabBar.isTranslucent = true
UITabBar.appearance().backgroundImage = UIImage()
//This is for removing top line from the tabbar.
UITabBar.appearance().layer.borderWidth = 0.0
UITabBar.appearance().clipsToBounds = true
}
// This method will get called when you tap on any tab
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
if viewController == tabBarController.viewControllers?[0] { //<----- This is first viewController
//If you set isTranslucent to true then no need to set barTintColor. it will make your tabBar transparent
self.tabBar.isTranslucent = true
} else if viewController == tabBarController.viewControllers?[1] { //<----- This is second viewController
self.tabBar.isTranslucent = false
// the tab bar will provide an opaque background black for UIBarStyleBlack or white for UIBarStyleDefault if barTintColor is nil.
self.tabBar.barTintColor = .white
// OR
// self.tabBar.barTintColor = nil
} else {
self.tabBar.isTranslucent = false
self.tabBar.barTintColor = .red
}
return true
}
}
Output : -
Hope this helps
#isa123 try this code in appdelegate didFinishLaunchingWithOptions method
UITabBar.appearance().tintColor = .white
UITabBar.appearance().barTintColor = UIColor(named: "PrimaryDark")
UITabBar.appearance().isOpaque = false
UITabBar.appearance().backgroundImage = UIImage.init(color: UIColor(named: "PrimaryDark")!, size: CGSize(width: 1, height: 1))

Search bar disappears during scrolling

I've read some Stack OverFlow posts and nothing from there has worked. I have one simple problem that shouldn't be incredibly difficult to solve. I have to stop the search bar from disappearing when scrolling on the tableview that it is attached to.
I have tried:
set the tableview to .plain to make the header view sticky, but that didn't work.
I tried navigationItem.hidesSearchBarWhenScrolling = false, but this may not have worked because searchController.searchBar is set equal to tableView.tableViewHeader.
Here is my viewDidLoad, which should contain the relevant code when this works, if it is indeed possible:
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
tableView.separatorStyle = .none
tableView.backgroundColor = UIColor.init(red: 240/255, green: 246/255, blue: 243/255, alpha: 1)
tableView.register(SearchTableViewCell.self, forCellReuseIdentifier: "SearchTVC")
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.delegate = self
definesPresentationContext = true
navigationController?.navigationBar.isHidden = true
tableView.tableHeaderView = searchController.searchBar
searchController.searchBar.tintColor = .darkGray//UIColor.white
searchController.searchBar.barTintColor = UIColor.convertHexColor(hex: "eaeaea")
navigationItem.hidesSearchBarWhenScrolling = false
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.text = ""
addConstraints()
presentInitialView()
}

Swift - Changing Scope Bar Tint Color

I created a search controller with a scope bar programmatically and I couldn't change the scope bar's tint color. I created the search controller with the following code:
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search Buildings or Codes"
navigationItem.searchController = searchController
definesPresentationContext = true
searchController.searchBar.scopeButtonTitles = ["Building Names", "Building Codes"]
searchController.searchBar.delegate = self
There is also a cancel button for the search bar and I changed the tint color using this code:
UIBarButtonItem.appearance(whenContainedInInstancesOf:[UISearchBar.self]).tintColor = UIColor.red
Thank you so much for your help.
let darkRed = UIColor(red: 0.392, green: 0.012, blue: 0.020, alpha: 1.00)
searchController.searchBar.tintColor = darkRed

Bring navbar in front of the UISearchController, Swift

I used a navBar which is created programmatically with an UISearchController.
When I start editing the UISearchBar:
- the navBar stays behind the dim view
- the tableView hides half of the navbar
- when the tabeView appeared, the cancel buttons are not selectable.
My UISearchController :
let locationSearchTable = storyboard!.instantiateViewControllerWithIdentifier("SearchTableViewController") as! SearchTableViewController
resultSearchController = UISearchController(searchResultsController: locationSearchTable)
resultSearchController.searchResultsUpdater = locationSearchTable
resultSearchController.view.backgroundColor=UIColor.clearColor()
locationSearchTable.delegate = self
let searchBar = self.resultSearchController.searchBar
searchBar.sizeToFit()
searchBar.placeholder = "Search"
searchBar.searchBarStyle = .Minimal
searchBar.barStyle = .Default
searchBar.translucent = true
searchBar.barTintColor = UIColor.whiteColor()
searchBar.setImage(UIImage(named:"search"), forSearchBarIcon: .Search, state : .Normal)
searchBar.delegate=self
resultSearchController.hidesNavigationBarDuringPresentation = false
resultSearchController.dimsBackgroundDuringPresentation = true
definesPresentationContext = true
My navBar :
let newNavBar : UINavigationBar = UINavigationBar.init(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, 64.0))
func styleNavBar (){
UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.Default
self.navigationController?.setNavigationBarHidden(true, animated: false)
let newItem : UINavigationItem = UINavigationItem.init()
newItem.titleView = self.resultSearchController.searchBar
newNavBar.barTintColor = UIColor(red: 243/255, green: 242/255, blue: 238/255, alpha: 1.0)
newNavBar.translucent = false
if let font = UIFont(name: "Avenir-Black", size: 16.0) {
let navBarAttributesDictionary : [String : AnyObject]? = [
NSForegroundColorAttributeName: UIColor(red: 74/255, green: 74/255, blue: 74/255, alpha: 0.51),
NSFontAttributeName: font
]
newNavBar.titleTextAttributes = navBarAttributesDictionary
}
newNavBar.setItems([newItem], animated: false)
self.view.addSubview(newNavBar)
self.view.bringSubviewToFront(newNavBar)
}
How can I fix it, please?
Thanks!
Try using this :
self.navigationController?.navigationBar.layer.zPosition = 1
you can use stackview for it . select search and tableview and click on stackview and constraint stackview from top/left/right/bottom

UISearchController under UIRefreshControl

I'm trying tu put a UIRefreshControl and a UISearchController into a UITableView.
When i pull down to refresh it works well but at first loading the UIRefreshControl is covered by the UISearchController:
Here is my code:
override func viewDidLoad() {
super.viewDidLoad()
self.refreshControl = UIRefreshControl()
self.refreshControl?.backgroundColor=UIColor.clearColor()
self.refreshControl!.addTarget(self, action: #selector(SocialManTableViewController.refresh(_:)), forControlEvents: UIControlEvents.ValueChanged)
self.refreshControl!.tintColor = UIColor(colorLiteralRed: 59/255, green: 89/255, blue: 152/255, alpha: 1)
self.refreshControl!.beginRefreshing()
searchController = UISearchController(searchResultsController: nil)
searchController.searchBar.sizeToFit()
searchController.searchBar.barTintColor = UIColor.clearColor()
searchController.searchBar.backgroundImage = UIImage()
searchController.searchBar.tintColor=UIColor.blackColor()
tableView.setContentOffset(CGPoint(x: 0, y: searchController.searchBar.frame.size.height), animated: false)
tableView.tableHeaderView = searchController.searchBar
definesPresentationContext = true
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
}

Resources