UISearchBar with solid color in iOS 7+ - ios

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.

Related

Color of the UISearchBar is not the same as the UINavigationBar

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

SWIFT: Navigation bar color is not displaying properly (Xcode 7)

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

Gap above UISearchBar using UISearchController

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.

Create a search bar like in the iOS 7 Contacts app

I have an application where I have a UITableView with sections which are scrollable on the side through sectionIndexTitlesForTableView:.
In the tableHeaderView of the UITableView I want to add a UISearchBar, which I want to behave like the one Apple is using in their new Contacts app in iOS7. But I can't seem to get it to work.
The search bar is not 100% the width of the screen, but instead has its right side against the section index.
My questions are:
How do I get the search bar's width to 100% when it's the tableHeaderView of a UITableView that has a visible section index?
How can I create a transition like in the Contacts app of iOS 7 where the navigation bar hides and the search bar's grey background extends to the status bar?
I have tried several things already, including adding the search bar in the navigation bar, and using UISearchBarController, but I can't find good documentation on the Apple website on how to create this. Also, the transition guide from iOS6 to iOS7 has been no good help for me.
Here are two pictures that illustrate my problem:
Two main differences to the way Apple do their app, which will solve your problems.
First, their UISearchBar isn't the headerView for the tableView. It's a separate view above the table. You'll need to change your UITableViewController to a UIViewController that has a UITableView positioned just below your UISearchBar.
Second, their UISearchBar has an associated Search Display Controller. Assuming you're using a storyboard file, this comes bundled together for you - just add the UISearchBar with the Search Display Controller. This will handle all of the transitioning to underneath the status bar for you.
Two things:
1) Set tableView's sectionIndexBackgroundColor to [UIColor clearColor].
2) Implement 'viewDidLayoutSubviews' method to fix frame for UISearchBar
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
CGRect barFrame = self.searchBar.frame;
barFrame.size.width = self.view.bounds.size.width;
self.searchBar.frame = barFrame;
}

iOS: table view's title bar covered by navigation bar unexpectedly

I'm developing a simple UI for an iOS app.
what I've done is I developed it into a table view which has a navigation controller associated to it and had it laid out in Storyboard.
Everything seems working fine, except the navigation bar accidentally cover the view's title bar "Click to select item" as shown in the pictures below.
the first picture is when it first shows on the screen.
the second one illustrates when I drag the title bar off the covering navigation bar.
what do i do to get completely rid of this?
In Interface Builder, select your view controller and on the attributes inspector turn off Layout: Wants Full Screen. Then, your layout will automatically adjust its size to make room for the navigation bar.
Add this code in your viewDidLoad method
self.navigationController?.navigationBar.translucent = false
It works for me, since you've the same problem with me, I think it's work for you as well.
where u want hide navigation bar use this line
self.navigationController.hidden = YES;

Resources