I'm experiencing an odd behavior with the UISearchBar in iOS. My search bar was working fine:
But when I set UINavigationBar.appearance().translucent = false in my ApplicationDelegate's didFinishLaunchingWithOptions to get the colors right in the Navigation Bar in my iOS app, the search bar gets a white section above it like this:
When scrolling the results table view, the underlying content is visible in that gap:
The closest I've gotten to fixing it, was setting mySearchResultsViewController.edgesForExtendedLayout = UIRectEdge.None, which stopped the translucent gap, but still doesn't have the right color:
Is there something I can do to prevent it from showing that discolored gap and still have globally opaque navigation bars?
After much googling, I found the following answer buried in the search results: UISearchController doesn't work properly with a non-translucent UINavigationBar
Specifically, this snippet worked when I put it in the presenting view controller:
func viewDidLoad() {
extendedLayoutIncludesOpaqueBars = true
}
So simple, yet difficult to guess.
Related
I am attempting to have a search bar tied to my UISearchController that appears below the navigation bar, similar to this:
Currently the search bar replaces the title as is common in most/all IOS apps I've seen. I've attempted to control the search bar presentation through a few methods:
1) Creating a new view that sits underneath the nav bar, and assigning the SearchController's search bar to it.
//instead of:
//navigationItem.titleView = searchBar
//do this:
searchplaceholder.addSubview(searchBar)
However the search bar shows up 1/2 way down the screen.
2)Second attempt was to move the frame of the search bar to origin:
searchBar.frame = CGRectMake(0, 0, self.view.frame.width, searchBar.frame.height)
view.addSubview(searchBar)
resultSearchController?.active = true
Looks good initially:
But as soon as I start typing, the bar jumps low again:
How can i Hijack the SearchController to present it's bar either in the nav bar without hiding the page title or constrain the search bar to appear right under the nav bar?
The long and short of this is that you can not do what I was attempting to do. You create a searchbar that when clicked transitions to a second screen with a searchbar in the header. This provides the cleanest experience compared to what I have listed above.
I've been looking for an answer for quite some time but couldn't find any that would satisfy me. It's my first question here on StackOverflow and my first real app so I hope that I didn't make any silly mistake that I'd have to be ashamed of.
In my app I have a table with a Search Bar under a Navigation Controller. In the Interface Builder I've set the search bar to match the color of navigation controller. They are exactly the same.
Interface Builder
However, when the app is running the search bar is slightly lighter and it has some lines dividing it from the navigation controller.
When I press the search bar the color stays brighter than it should.
Also, during the animation you can see white background of the view which doesn't look too good.
I've tried different options, from changing the background to the specified color to setting the backgroundImage to UIImage(). I wasn't able to achieve smooth-looking result. There has to be a way to solve this, perhaps through adding custom layers or direct subclassing but I just wanted to know if there is a simple solution to my problem.
I'm actually using the search bar and search display controller. But it doesn't make any difference. It's also interesting that I didn't really customize any behavior of it. Even when I change the color of a fresh new search bar added to the Storyboard it still displays different color. Is there a behavior that I'm not aware of?
Have you tried using a search bar and search display controller. When the user taps on the search bar, it animates up and pushes the nav bar up. You can change the color of it to match the nav bar. You can even programmatically (not in storyboard) add a searchBar within the nav bar to get rid of the color problems.
Here is some code I found from another post that may help!
Try this
var leftNavBarButton = UIBarButtonItem(customView:Yoursearchbar)
self.navigationItem.leftBarButtonItem = leftNavBarButton
Update
You keep a lazy UISearchBar property
lazy var searchBar:UISearchBar = UISearchBar(frame: CGRectMake(0, 0, 200, 20))
In viewDidLoad
searchBar.placeholder = "Your placeholder"
var leftNavBarButton = UIBarButtonItem(customView:searchBar)
self.navigationItem.leftBarButtonItem = leftNavBarButton
If you want to use storyboard just drag your searchbar as a outlet,then replace the lazy property with your outlet searcher
Here is where I got the code from (first answer): Get search bar in navigation bar in Swift
I haven't used UISearchBar yet, so this is just a guess, but maybe it has to do with the alpha property of the UISearchBar since it inherits from UIView. Try setting alpha to 1.0 and see what happens :)
So I continued to look for a solution and I partially found one. It turns out that the "Search bar and search display controller" that we can use in Interface Builder was deprecated in iOS 8.0. And I even made a blank app with just one navigation controller and aforementioned search bar and the color of the latter is always out of place.
I didn't find any solution to that exact problem but managed to change my project a bit. Now I'm using UISearchController added programmatically.
let searchController = UISearchController(searchResultsController: nil)
tableView.tableHeaderView = searchController.searchBar
It looks much much better. BUT to be sure I've checked the colors using OS X stock Colorimeter and they still vary a bit. It has probably something to do with translucence and the background underneath and I will eventually solve this problem but for now it has to stay like that
I have the following storyboard setup:
For some reason, the navigation bar style (i.e. the color) does not carry over to the RSVP view controller (it shows as a darker blue than what was selected in the nav controller). I've tried changing it programatically as suggested by other posts, however, it didn't seem to fix it.
I've tried adding the following into both ViewWillAppear and ViewDidLoad, but it was not effective (note: I'm just using the greenColor() as a test to see if the colour changes):
UINavigationBar.appearance().backgroundColor = UIColor.greenColor()
I've tried to use all the properties UISearchBar has with different combinations, I've searched for the answer for a while but still can't solve the problem.
Problem:
I have a UIViewController with UITableView in it and some other view in storyboard. I've added Search Bar and Screen Display Controller into my project (search bar is added as a subview/header of the UITableView)
I need to make it red color with no transparency/translucency to match my navigation bar.
I've tried "solution" from How to change background color of UISearchBar in iOS7 but it doesn't work. Result of this solution looks like that:
searchDisplayController?.searchBar.barTintColor = UIColor.myAppRedColor()
searchDisplayController?.searchBar.translucent = false
As you can see - that isn't exactly matching Navigation Bar color. It also has a separator between navbar and search bar.
Next solution almost worked:
searchDisplayController?.searchBar.backgroundImage = UIImage()
searchDisplayController?.searchBar.backgroundColor = UIColor.myAppRedColor()
But when I tap on the search bar it changes color:
It also has very weird animation:
https://youtu.be/Dq1GKBGbmTc
Do you have an idea what else I can do instead of implementing my own SearchBar?
Do not use Search Display Controller. Instead use only searchBar and TableView.
Simply delete Search Display Controller from your storyboard.
You will receive delegate methods of searchBar.
Here you can apply search on tableView.
I am facing a weird scenario, I have used a search bar in my application and tied it up with a table view as is seen in the below image!
But when ever I try to search anything the search bar slides up and gets hidden by navigation bar, I have used search bar a number of times and never seen me thing like this below is its screen shot !
I can't remove the navigation bar since I need it, but I just can't figure out why or how it can be happening,
One point to note is that the search bar and it's controller are working perfectly when tried to search, only thing is it's getting hidden behind the navigation bar,
Any help on this would be appreciated, as I just can't figure out its reason.
Try this:
Set the navigation bar's translucent property to NO:
self.navigationController.navigationBar.translucent = NO;
This line will fix the view from being framed underneath the navigation bar and status bar.
If you have to show and hide the navigation bar, then use this code in viewDidLoad
if ([self respondsToSelector:#selector(edgesForExtendedLayout)])
self.edgesForExtendedLayout = UIRectEdgeNone; // iOS 7 specific