How to implement UISearchBar with no border - iphone

I am trying to add a UISearchBar without the border around it, but it's simply not working.
This is what I have:
On the XIB file, I added UISearchBar. Style = Black Transparent. Tint = Default. None of the options (bookmarks, cancel button, scope bar, etc) are selected. Background = Default. Drawing = opaque (basically the default setting of the UISearchBar).
I have a property for UISearchBar - searchBar.
In the implementation file, when the view loads, I have:
[[self.searchBar.subviews objectAtIndex:0] removeFromSuperview];
But for whatever reason, the background (the dark border around the box) is simply not going.
I tried [[self.searchBar.subviews objectAtIndex:0] setAlpha:0], but that's not working at all either.
Any help please?

This worked for me
for (UIView * view in [[[self.searchBar subviews] objectAtIndex:0] subviews])
{
if (![view isKindOfClass:[UITextField class]])
{
view .alpha = 0;
}
}

Here is the code .....
for (id img in searchBar.subviews)
{
if ([img isKindOfClass:NSClassFromString(#"UISearchBarBackground")])
{
[img removeFromSuperview];
}
}
happy coding :)

How about:
[self.searchBar setOpaque:NO];

[[_searchBar.subviews objectAtIndex:0] removeFromSuperview];
_searchBar.backgroundColor = [UIColor clearColor];
Hope this work for you, its working for me.

this work only in static UISearchBar, not if you want to show search CancelButton, this problem occur in devices having ios above 5.0.
For that you have to change/set BackgroundView of UiSearchBar.

Try This
for (UIView * view in [mySearchBar subviews]) {
if (![view isKindOfClass:[UITextField class]]) {
view .alpha = 0;
}
}

For those of you using 8.0 and above, the best solution is to simply use the UIAppearance protocol, by doing the following:
[[UISearchBar appearance] setBackgroundImage:[UIImage new] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
You can also do it on one specific instance with:
[mySearchBar setBackgroundImage:[UIImage new] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];

if someone has same problem as I (see picture) code above could fix
[self.tableView setContentInset:UIEdgeInsetsMake(-1, 0, 0, 0)];

Related

Cell subviews transparency while selected/highlighted

I'd like to know what makes the backgrounds of subviews (labels for example) of a UITableViewCell become transparent while selected/highlighted. I need to avoid that behaviour for some subviews of my content view. I tried overriding the setSelected / setHighlighted methods with some success, but that transparency I wasn't able to reproduce. Any thoughts?
I think I got a somewhat close behaviour using the following piece of code
- (void) setView:(UIView*) view asHighlighted:(BOOL) highlighted {
if([view isKindOfClass:[UILabel class]]){
UILabel* label = (UILabel*) view;
[label setHighlighted:highlighted];
}
else if([view isKindOfClass:[UIImageView class]]){
UIImageView* imageView = (UIImageView*) view;
[imageView setHighlighted:highlighted];
}
if(highlighted){
[view.undoManager registerUndoWithTarget:view
selector:#selector(setBackgroundColor:)
object:view.backgroundColor];
view.backgroundColor = [UIColor clearColor];
}
else {
[view.undoManager undo];
}
for(UIView* subview in view.subviews){
[self setView:subview asHighlighted:highlighted];
}
}
Not sure this is the right way to use UndoManager but it works. You can use this in both setSelected and setHighlighted to mimic selection behaviour of UITableViewCells

Deleting controls programmatically

I want to remove a UITextField/UIButton/UILabel added to a view on the click of button. How can this be achieved in iOS?
To remove you can simply use:
[myTextField removeFromSuperview];
try this one...
Blockquote
UILabel *t1;
NSArray *arr1=[YourView subviews];
for (UIView *v in arr1)
{
if([v isKindOfClass:[UILabel class]])
{
t1 = (UILabel *)v;
if (t1.tag==your_tag)
[t1 removeFromSuperview];
}
}
Blockquote
You can use [myTextField removeFromSuperview]; to get rid of a view.
[myTextField removeFromSuperview]
See UIView documentation.
All controls inherited from UIView.
So you can remove all the controls which added on UIView using like this. By clicking the button action write like this
for(UIView *control in [self.view subviews])
{
[control removeFromSuperview];
}

How to change color of Search Bar?

I've implemented a Search Bar and I want to change color of Search Bar. How can I do that?
I've tried with:
self.mySearchBar.backgroundColor = [UIColor redColor];
but with no success.
UPDATE (Solved):
I had to remove the default background color before I defined background color with the following code.
for (UIView *subview in mySearchBar.subviews) {
if ([subview isKindOfClass:NSClassFromString(#"UISearchBarBackground")]) {
[subview removeFromSuperview];
break;
}
}
... so code would be next:
for (UIView *subview in mySearchBar.subviews) {
if ([subview isKindOfClass:NSClassFromString(#"UISearchBarBackground")]) {
[subview removeFromSuperview];
break;
}
}
self.mySearchBar.backgroundColor = [UIColor redColor];
Try:
self.mySearchBar.barTintColor = [UIColor redColor];
self.mySearchBar.backgroundColor = [UIColor redColor];
won't do anything,
but
self.mySearchBar.tintColor = [UIColor redColor];
will!
self.mySearchBar.backgroundVolor = [UIColor redColor];
"Volor" or "Color
self.mySearchBar.backgroundColor = [UIColor redColor];
Also if you want to tint it:
According to the documentation : https://developer.apple.com/iphone/library/documentation/UIKit/Reference/UISearchBar_Class/Reference/Reference.html, there is a tintColor property on the UISearchBar class.
In the TableSearch example: https://developer.apple.com/library/ios/#samplecode/TableSearch/ the search bar is defined and loaded in the MainView.xib. If you want to change its tintColor or style, just do it in the xib and it will be loaded into the application.
BUT THE ONLY REAL WAY to change background color is to override it, look at the answer from the following question
UISearchBar clear background color or set background image [iphone sdk]
I'm not sure if it has changed since iOS7, but it seems that the search bar in the search display controller requires an extra loop to properly remove the UISearchBarBackground view.
I created a recursive method to properly clean the search bar.
- (void) removeUISearchBarBackgroundInViewHierarchy:(UIView *)view
{
for (UIView *subview in [view subviews]) {
if ([subview isKindOfClass:NSClassFromString(#"UISearchBarBackground")]) {
[subview removeFromSuperview];
break; //To avoid an extra loop as there is only one UISearchBarBackground
} else {
[self removeUISearchBarBackgroundInViewHierarchy:subview];
}
}
}
You can simply send your search bar to the method and change the color afterward, a bit like the suggested answers above.
[self removeUISearchBarBackgroundInViewHierarchy:self.searchDisplayController.searchBar];
self.searchDisplayController.searchBar.backgroundColor = yourUIColor;
I had to set UISearchBar's searchBarStyle to .default, otherwise nothing works.
searchBar.barTintColor = UIColor.redColor()
Try this code it works fine for me.
for( UIView* v in [parent subviews] ) {
if( [v isKindOfClass:[UISearchBar class]] ) {
[(UISearchBar*)v setTintColor:[UIColor blackColor]];
}
If you have issues with top and bottom lines try to remove the UISearchBarBackground:
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 50)];
for (UIView *subview in self.searchBar.subviews) {
if ([subview isKindOfClass:NSClassFromString(#"UISearchBarBackground")]) {
[subview removeFromSuperview];
break;
}
for (UIView *subsubview in subview.subviews) {
if ([subsubview isKindOfClass:NSClassFromString(#"UISearchBarBackground")]) {
[subsubview removeFromSuperview];
break;
}
}
}
self.searchBar.barTintColor = [UIColor redColor];
self.searchBar.backgroundColor = [UIColor redColor];
Depends what exactly are you going to change
searchBar.barTintColor
searchBar.tintColor
Other items you are able customise thought some extra code

Is it possible to remove UISearchBar's tint image?

i have to hide searchbar tint.
i write foll0wing code
UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 15, 270, 15)];
searchBar.delegate = self;
searchBar.barStyle = UISegmentedControlStylePlain;
searchBar.placeholder =#"search";
searchBar.tintColor=[UIColor clearColor];
[imageview addSubview:searchBar];
here search bar add on imageview. i want to hide tint side but bydeaflt it comes .i also chage the color to tint color but it not work.is anybody have idea of removing or hide tint on searchbar .Thanks
Hope this helps.
[searchBar setBackgroundImage:[UIImage new]];
Will also work in iOS 7.1
I have used this code. It completely removed the tint around the search text bar.
for (id v in [self.search subviews]) {
if (![v isKindOfClass:NSClassFromString(#"UISearchBarTextField")]) {
[v setAlpha:0.0f];
[v setHidden:YES];
}
else {
[v setBackgroundColor:[UIColor clearColor]];
}
}
Here's how I customized the UISearchBar
if ([[[searchBar subviews] objectAtIndex:0] isKindOfClass:[UIImageView class]]){
[[[searchBar subviews] objectAtIndex:0] removeFromSuperview];
}
This removes the background bar style.
You can then add any custom subview you want.
Try this
[searchBar setTranslucent:YES];
Also, don't set the tintColor when using translucent

iphone - Search button on a UISearchBar

I have a search functionality using UISearchBar that occurs on-the-fly, so I think it would be more obvious to replace that "Search" button on the keyboard with "Done".
Is there a way to do that?
thanks
You can change the keyboardType property of your UISearchBar object. However, there is not a way to change the returnKeyType directly. You may be able to filter down and change it manually. Check the documentation for UISearchBar and see if you can find returnKeyType as that is what you are looking for.
I accomplish it this way:
// -- Basic UISearchBar setup.
self.theSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,0,320,38)];
[self.theSearchBar setDelegate:self];
[self.view addSubview:self.theSearchBar];
// -- Customize the returnKeyType of the search bar's nested UITextField.
UITextField *searchBarTextField = [[self.theSearchBar subviews] objectAtIndex:1];
searchBarTextField.returnKeyType = UIReturnKeyGo;
Hope that is helpful. This approach (i.e. grabbing a subview by index) may break in the future, but it works fine for now.
for (UIView *view in _searchBar.subviews){
if ([view isKindOfClass:[UITextField class] ]) {
UITextField *searchTf = (UITextField *)view;
searchTf.returnKeyType = UIReturnKeyDone;
}
}
This is working for iOS 6
UITextField *searchBarTextField = [[searchBarObj subviews] objectAtIndex:1];
searchBarTextField.returnKeyType = UIReturnKeyDefault;
[searchBarTextField setEnablesReturnKeyAutomatically:NO];
This is working for iOS 7
for (UIView *subview in self.searchBar.subviews)
{
for (UIView *subSubview in subview.subviews)
{
if ([subSubview conformsToProtocol:#protocol(UITextInputTraits)])
{
UITextField *textField = (UITextField *)subSubview;
[textField setKeyboardAppearance: UIKeyboardAppearanceAlert];
textField.returnKeyType = UIReturnKeyDone;
break;
}
}
}
Don't rely on it being the second subview, use isKindOfClass: method to check. It will be more iOS update proof that way.
for (UIView *subview in self.theSearchBar.subviews) {
if ([subview isKindOfClass:[UITextField class]]) {
[(UITextField *)subview setReturnKeyType:UIReturnKeyGo];
break;
}
}