Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I just came up with a solution that works really well. You have to override the UISearchBar and then hide both the Background and Segment Control layers. Then Draw the background.</p> <p>@ .m</p> <pre><code>#import "UISearchBar.h" #import &lt;QuartzCore/QuartzCore.h&gt; @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 &gt; 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 </code></pre> <p>@ .h</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; #import &lt;QuartzCore/QuartzCore.h&gt; @interface UISearchBar(CustomBackground) @end </code></pre> <p>Hope this helps!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload