example bar ios uisearchbar

ios - example - UISearchBar: color de fondo claro o establecer imagen de fondo



uisearchbardelegate (13)

Acabo de encontrar una solución que funciona muy bien. Debe anular la barra UISearch y luego ocultar las capas de Control de fondo y Segmento. Luego dibuja el fondo.

@ .m

#import "UISearchBar.h" #import <QuartzCore/QuartzCore.h> @implementation UISearchBar(CustomBackground) - (id)init { for ( UIView * subview in self.subviews ) { if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground") ] ) subview.alpha = 0.0; if ([subview isKindOfClass:NSClassFromString(@"UISegmentedControl") ] ) subview.alpha = 0.0; } return self; } + (UIImage *) bgImagePortrait { static UIImage *image = nil; if (image == nil) image = [[UIImage imageNamed:@"UISearchBarBack.png"] retain ]; return image; } + (UIImage *) bgImageLandscape { static UIImage *image = nil; if (image == nil) image = [[UIImage imageNamed:@"UISearchBarBack.png"] retain]; return image; } - (void) drawLayer:(CALayer *)layer inContext:(CGContextRef)contenxt { if ([self isMemberOfClass:[UISearchBar class]] == NO) return; UIImage * image = ( self.frame.size.width > 320 ) ? [UISearchBar bgImageLandscape ] : [UISearchBar bgImagePortrait ]; for ( UIView * subview in self.subviews ) { if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground") ] ) subview.alpha = 0.0; if ([subview isKindOfClass:NSClassFromString(@"UISegmentedControl") ] ) subview.alpha = 0.0; } CGContextTranslateCTM( contenxt , 0 , image.size.height ); CGContextScaleCTM( contenxt, 1.0, -1.0 ); CGContextDrawImage( contenxt , CGRectMake( 0 , 0 , image.size.width , image.size.height ), image.CGImage ); } @end

@ .h

#import <Foundation/Foundation.h> #import <QuartzCore/QuartzCore.h> @interface UISearchBar(CustomBackground) @end

¡Espero que esto ayude!

Esta pregunta ya tiene una respuesta aquí:

¿Cómo puede configurar la imagen de fondo, o borrar el fondo, de una barra de búsqueda, como la aplicación de notas?


Cómo configurar el color de fondo en UISearchBar:

#import <QuartzCore/QuartzCore.h>

Luego haga una conexión de salida a la barra de búsqueda (por ejemplo, objSearchbar ) y use estas líneas:

for (UIView *subview in self.objSearchbar.subviews) { if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) { [subview removeFromSuperview]; break; } } self.tweetSearchbar.layer.backgroundColor = [UIColor blueColor].CGColor;


Establecer imagen de fondo

UIImageView *backgroundView = [[UIImageView alloc] initWithFrame:searchBar.bounds]; backgroundView.image = [UIImage imageNamed:@"xxxx.png"]; [searchBar insertSubview:backgroundView atIndex:1]; // at index 1 but not 0 [backgroundView release];


Esto funcionó para mí (ios4.2 +)

// Change the search bar background -(void)viewWillAppear:(BOOL)animated { for (UIView *subview in self.searchBar.subviews) { if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) { UIView *bg = [[UIView alloc] initWithFrame:subview.frame]; bg.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"search_bar_bg"]]; [self.searchBar insertSubview:bg aboveSubview:subview]; [subview removeFromSuperview]; break; } } }



Prefiero establecer el alfa en 0 para que pueda ocultarlo / mostrarlo a pedido.

// Hide [[self.theSearchBar.subviews objectAtIndex:0] setAlpha:0.0]; // Show [[self.theSearchBar.subviews objectAtIndex:0] setAlpha:1.0];


Tuve problemas con la respuesta anterior. Utilicé lo siguiente.

for (UIView *subview in searchBar.subviews) { if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) { [subview removeFromSuperview]; break; } }


Un camino a prueba de futuro:

[searchBar setBackgroundImage:[UIImage new]]; [searchBar setTranslucent:YES];


Un trazador de líneas:

[[self.theSearchBar.subviews objectAtIndex:0] removeFromSuperview];


Usted tiene dos preguntas aquí:
1.UISearchBar color de fondo claro:
Mira mi respuesta aqui

2.Configure la imagen de fondo Solución :( Si está en iOS 5.0 + )

[[UISearchBar appearance]setSearchFieldBackgroundImage:[navBarGradImage resizableImageWithCapInsets:inset2] forState:UIControlStateNormal];

NOTA: También puede intentar usar una imagen transparente como fondo.

Espero que esto ayude.


con iOS8 sdks apple movió la vista "UISearchBarBackground" un nivel más profundo, por lo que tendrá que ver las subvistas de las vistas de los niños como se muestra a continuación.

for (UIView *subview in searchBar.subviews) { for(UIView* grandSonView in subview.subviews){ if ([grandSonView isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) { grandSonView.alpha = 0.0f; }else if([grandSonView isKindOfClass:NSClassFromString(@"UISearchBarTextField")] ){ NSLog(@"Keep textfiedld bkg color"); }else{ grandSonView.alpha = 0.0f; } }//for cacheViews }//subviews


mj_ ​​tiene la respuesta que usé. Solo iba a comentar como tal pero todavía no puedo. Así que solo publicaré mi propia respuesta con mi implementación donde agrego una barra de búsqueda en la parte superior de una vista de tabla con un BG semitransparente.

DLog(@" Add search bar to table view"); //could be a UIImageView to display an image..? bg = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 45)] autorelease]; bg.backgroundColor = [UIColor blackColor]; UISearchBar *sb = [[[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 290, 45)] autorelease]; sb.barStyle = UIBarStyleBlackTranslucent; sb.showsCancelButton = NO; sb.autocorrectionType = UITextAutocorrectionTypeNo; sb.autocapitalizationType = UITextAutocapitalizationTypeNone; sb.delegate = self; [bg addSubview:sb]; table.tableHeaderView = bg; for (UIView *subview in sb.subviews) { if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) { [subview removeFromSuperview]; break; } }


searchBar.searchBarStyle = UISearchBarStyleMinimal;