remove searchbar black border - ios

I have a search bar that looks like this:
Is there anyway to remove that black border on the top?
I tried the minimal bar approach but got:
So the border was removed but now I would need to somehow color the inner field white.
I tried:
if let searchTextField = searchBar.valueForKey("searchField") as? UITextField {
searchTextField.backgroundColor = UIColor.whiteColor()
}
But it didn't seem to change anything.
I'm looking for a solution to either of these problems. I just want the pink border with the white text field.
Edit:
There is a similar question for iOS7 but the accepted answer did not work for me.

Creating an image filled with the pink color:
And setting it as the backgroundImage for the search bar did the trick.

Related

Set UISearchBar Background Color to White

I am facing issue to set bg color of UISearchBar as White. I have tried to set bg as white but still default gray bg color is there.
I tried this code.
searchController?.searchBar.searchTextField.backgroundColor = UIColor.white
I dont understand what's wrong. Bcz when i set any color like green or red, they are set easily.
Please provide answer in Swift 4+ .
Use the below in Appdelegate
if #available(iOS 13, *) {
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).backgroundColor = . white
}
We can't set background color of SearchBar as white.
As we can see all Apple default Apps doesn't have white BG color,
Solution: We need to make customised SearchBar for that.

UIImageView tintColor colors whole image

I've tried changing the tintColor of a UIImageView inside a UICollectionViewCell after a tap by setting render mode of the icons to Template Image and then setting the color by calling
iconView.tintColor = .blue
Problem: This fills the whole image with the chosen color, Instead of just changing the non-transparent parts.
This method has been working for me in the past, and I'm unable to figure out the problem. I assumed that the icons might have a white background instead of a transparent one, but that's not the case. Maybe it has something to do with the UIImageView being inside a UICollectionViewCell?
Help would be highly appreciated!
try this
imageViewHome.image = UIImage.init(named: "yourImageName")?.withRenderingMode(.alwaysTemplate)
imageView.tintColor = .blue
The following code will color only the non transparent area.
yourImage.image = yourImage.image!.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
yourImage.tintColor = UIColor.redColor()
Happy coding.

Change UISearchBar magnify icon color and position

I have a UISearchBar and I would like to change the position of the initial magnify icon (the one that appears in the middle of the UISearchBar) as well as the color or icon.
So far I changed the tint and the icon image.
However, the new icon shows up only if I test the app on a simulator but on an actual device(both run iOS 9.3) it still shows the default icon.
UISearchBar.appearance().setImage(UIImage(named: "SearchbarIcon"), forSearchBarIcon: UISearchBarIcon.Search, state: UIControlState.Normal)
As for the magnify icon position, I want it in the left side, where it shows up if I activate the UISearchBar.
I found quite a lot of answers around here many of the provided solutions don't work on iOS 8.0+ or are in Objective-C and I am having some problems understanding them.
I tried to add a custom background containing the icon but it shows up at the bottom of the search bar while the icon is still there:
The background shows up OK if I change the background offset for Y to -44 (the height of the UISearchBar) but I can no longer type since it seems the entire text field is pushed up. I also tried to change the vertical offset for the SearchText to 44 to compensate but no luck. To be honest, I am not sure what Search Text and the Background offsets are supposed to do but I decided to give them a try.
Is there any way to accomplish this? Or maybe a different approach?
You can adjust the position of the search bar icon using
func positionAdjustmentForSearchBarIcon(_ icon: UISearchBarIcon) -> UIOffset
See https://developer.apple.com/library/ios/documentation/UIKit/Reference/UISearchBar_Class/#//apple_ref/occ/instm/UISearchBar/positionAdjustmentForSearchBarIcon:
You can use:
uiSearchBar.setPositionAdjustment(UIOffset, for: UISearchBar.Icon)
replace UIOffset with your desired offset value UIOffset(horizontal: CGFloatvertical: CGFloat) and UISearchBar.Icon with .search
#Michael - Thanks for all the help.
I managed to grab the UISearchBar's UITextField:
func customizeSearchBar()
{
for subview in srcRegimenSearchBar.subviews
{
for view in subview.subviews
{
if let searchField = view as? UITextField
{
let imageView = UIImageView()
let image = UIImage(named: "SearchBarIcon.png")
imageView.image = image;
imageView.frame = CGRectMake(0, 0, 0, 0)
/*imageView.frame = CGRectMake(100, 0, 20, 19)*/
searchField.leftView = imageView
searchField.leftViewMode = UITextFieldViewMode.Always
}
}
}
}
I wanted to change the position since I have access to the frame but it seems only the the top and height can be modified, at least the way I tried so I set the height and width to 0 (I couldn't find a way to make it nil or remove it completely) and I added an UIImageView with the new icon over in the left side of the UISearchbar and added a custom horizontal offset for the tint.
Not the best solution out there, I'm sure of it, but for now it works.

Tint Color fading when popover displayed

I have a simple block of code wherein I am setting the tint color of a UIView. Under normal condition, this works well, but when I present a popover from somewhere, the rgb tint color fades to grayscale colors.
Once the popover is dismissed, the colors return to their normal values.
The code runs something like this:
UIView *view = self.imageViews[index];
view.tintColor = tintColor;
Is there a way to stop these colors from fading under presence of popover ?
Thanks in advance.
UIView has a property called tintAdjustmentMode. Did you try that out ??
A single line code like
view.tintAdjustmentMode = UIViewTintAdjustmentModeNormal;
could help you out in this case.
Edit: Swift 4.2 update:
view.tintAdjustmentMode = .normal

Monotouch UIView Appearance changing UITextField background colors, UIButton background colors, etc

I am trying to implement a custom appearance for UIView when contained in UIViewController.
var viewStyle = UIView.AppearanceWhenContainedIn(typeof(UIViewController));
viewStyle.BackgroundColor = UIColor.Blue;
The result I get from doing this is something ugly, like this:
I tried adding code to change the UITextField/UIButton background by doing:
var txtBoxStyle = UITextField.AppearanceWhenContainedIn(typeof(UIViewController));
txtBoxStyle.BackgroundColor = UIColor.White;
var btnStyle = UIButton.AppearanceWhenContainedIn(typeof(UIViewController));
btnStyle.SetTitleShadowColor(UIColor.White, UIControlState.Normal);
btnStyle.SetTitleColor(UIColor.Black, UIControlState.Normal);
but that doesn't help my result at all (the above image isn't updated from when I changed the button title color from white to black, you'll have to imagine the button's text as black instead of white). Anyone know how to get rid of that ugly blue in the (foreground or background?) of the UITextFields and UIButtons? Bonus if you can figure out why the Title's background is blue as well and how to fix that!

Resources