Bring navbar in front of the UISearchController, Swift - ios

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

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))

How to modify the width and height of UISearchBar in Swift?

I want to adjust the height and width of the searchBar in my app. I tried some solution from SO but there is something wrong in the adjustment of the width and height of the search bar. When I build and run the app, the image below is the look of my searchBar which is what I wanted to look like. Height is 70, width is 714.
But when I tapped the searchBar and start to type text, the size adjusted to smaller height. Please see 2nd image below.
Then when I tapped the cancel button it goes back to the default look of UISearchBar. Please see 3rd image below.
How can I modify width and height of searchBar permanently without experiencing auto adjustment while typing text or tapping the cancel button. Please take a look at my codes for your reference. Hope you can help me. Thank you.
private var searchBar: UISearchBar!
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
configureSearchBar()
searchController.searchBar.frame = CGRect(x: 15, y: 100, width: 714, height: 100)
}
override func viewDidAppear(_ animated: Bool) {
let searchTextField: UITextField = searchController.searchBar.subviews[0].subviews.last as! UITextField
searchTextField.layer.cornerRadius = 10
searchTextField.textAlignment = NSTextAlignment.left
searchTextField.placeholder = "Search by Name, Department or Employee Number"
searchTextField.rightViewMode = UITextFieldViewMode.always
}
//MARK: Function
func configureSearchBar() {
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
searchController.searchBar.textColor = UIColor.black
searchController.searchBar.placeholder = "Search by name, department or employee number"
searchController.searchBar.searchBarStyle = .prominent
searchController.searchBar.barTintColor = UIColor(red: 26/255.0, green: 99/255, blue: 42/255, alpha: 1.0)
searchController.searchBar.tintColor = UIColor.black
searchController.searchBar.backgroundColor = UIColor(red: 255/255.0, green: 255/255, blue: 255/255, alpha: 1.0)
searchController.searchBar.setImage(#imageLiteral(resourceName: "search"), for: .search, state: .normal)
let margins = searchController.searchBar.layoutMarginsGuide
searchController.searchBar.leadingAnchor.constraint(equalTo: margins.leadingAnchor, constant: 20).isActive = true
searchController.searchBar.isTranslucent = true
if #available(iOS 11.0, *) {
searchController.searchBar.heightAnchor.constraint(equalToConstant: 100).isActive = true
}
self.ParticipantTableView.tableHeaderView = searchController.searchBar
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
searchController.searchBar.layoutIfNeeded()
searchController.searchBar.layoutSubviews()
_ = searchController.searchBar.frame
let newheight: CGFloat = 70
let newWidth: CGFloat = 714
for subView in searchController.searchBar.subviews
{
for subsubView in subView.subviews
{
if let textField = subsubView as? UITextField
{
var currentTextFieldBounds = textField.bounds
currentTextFieldBounds.size.height = newheight
currentTextFieldBounds.size.width = newWidth
textField.bounds = currentTextFieldBounds
textField.borderStyle = UITextBorderStyle.roundedRect
}
}
}
}
extension ParticipantsViewController: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
filterContentForSearchText(searchText: searchController.searchBar.text!)
}
}
extension UISearchBar {
var textColor: UIColor? {
get {
if let textField = self.value(forKey: "searchField") as? UITextField {
return textField.textColor
}else {
return nil
}
}
set (newValue) {
if let textField = self.value(forKey: "searchField") as? UITextField {
textField.textColor = newValue
textField.font = UIFont(name: "HelveticaNeue", size: 25.0)
}
}
}

UISearchController Swift 4: Spacing Problems

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"

UIPageViewController indicators don't change color

I've read many other threads here about this problem, it appears that the best solution now is to set it as follows:
let pageControl = UIPageControl.init(frame: CGRectMake(0, UIScreen.mainScreen().bounds.size.height*14/15, UIScreen.mainScreen().bounds.size.width, UIScreen.mainScreen().bounds.size.height))
pageControl.backgroundColor = UIColor(red: 0, green: 147/255, blue: 229/255, alpha: 1)
pageControl.pageIndicatorTintColor = UIColor.lightGrayColor()
pageControl.currentPageIndicatorTintColor = UIColor.blackColor()
view.addSubview(pageControl)
But for some reason, this does not work for me. Page control background changes its color, but the indicators remain white.
My entire viewDidLoad() method
override func viewDidLoad() {
super.viewDidLoad()
self.pageViewController = self.storyboard!.instantiateViewControllerWithIdentifier("OnboardingPageViewController") as! UIPageViewController
self.pageViewController.dataSource = self
let startingViewController:OnboardingPageContentViewController = self.viewControllerAtIndex(0)!
let viewControllers:Array<OnboardingPageContentViewController> = [startingViewController]
self.pageViewController.setViewControllers(viewControllers, direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)
let pageControl = UIPageControl.init(frame: CGRectMake(0, UIScreen.mainScreen().bounds.size.height*14/15, UIScreen.mainScreen().bounds.size.width, UIScreen.mainScreen().bounds.size.height))
pageControl.backgroundColor = UIColor(red: 0, green: 147/255, blue: 229/255, alpha: 1)
pageControl.pageIndicatorTintColor = UIColor.lightGrayColor()
pageControl.currentPageIndicatorTintColor = UIColor.blackColor()
view.addSubview(pageControl)
self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)
self.addChildViewController(pageViewController)
self.view.addSubview(pageViewController.view)
self.pageViewController.didMoveToParentViewController(self)
}
Thanks in advance
it appears that the best solution now is to set it as follows:
No, it isn't. You are adding a new UIPageControl, but the UIPageViewController already has a UIPageControl. What you want is to set the attributes of that UIPageControl.
The way to do that is to use the appearance proxy. Example:
let proxy = UIPageControl.appearance()
proxy.pageIndicatorTintColor = UIColor.redColor().colorWithAlphaComponent(0.6)
proxy.currentPageIndicatorTintColor = UIColor.redColor()
proxy.backgroundColor = UIColor.yellowColor()

Swift : Navigation bar should be stick on to the TOP

I wan to stick my navigation bar on top for every pages that i have in app.
I have my Code below
navigationController?.hidesBarsOnSwipe = true
navigationController?.hidesBarsOnTap = true
navigationController?.navigationBar.barStyle = UIBarStyle.BlackTranslucent
navigationController?.navigationBar.opaque = true
navigationController?.navigationBar.translucent=true
navigationController?.navigationBar.alpha = 0.4
navigationController?.navigationBar.backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.4)
navigationController?.navigationBar.translucent = true
navigationController?.navigationBar.tintColor = UIColor.whiteColor()
self.navigationController?.setNavigationBarHidden(false, animated: false)
What can i do to just stick navigation bar?
Thanks,
Dhaval.
Ok, here some guide, assume that you're a do-everything-by-code guy.
In app delegate:
let tempVC = UIViewController()
tempVC.backgroundColor = UIColor.redColor()
let navVC = UINavigationController(rootViewController:tempVC)
window?.rootViewController = navVC
it should work.
Then you do your navigationBar customization code in here or in your viewcontroller.
Issue resolved
Code below
navigationController?.navigationBar.tintColor = UIColor.whiteColor()
navigationController?.navigationBar.barStyle = UIBarStyle.BlackTranslucent
navigationController?. navigationBar. hidden=false

Resources