UISearchBar清除背景颜色或设置背景图像[iphone sdk] [重复](UISearchBar: clear background color or set background image [duplicate])
这个问题在这里已经有一个答案:
如何在iOS 17的答案 上更改UISearchBar组件的内部背景颜色如何设置背景图片或清除背景的搜索栏,如笔记应用程序?
This question already has an answer here:
How to change inside background color of UISearchBar component on iOS 21 answersHow can set the background image, or clear the background, of a search bar, like the note application?
最满意答案
我刚刚提出了一个非常好的解决方案。 您必须覆盖UISearchBar,然后隐藏“背景”和“段控制”层。 然后画背景。
@ .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希望这可以帮助!
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.
@ .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) @endHope this helps!
更多推荐
发布评论