UISearchBar清除背景颜色或设置背景图像[iphone sdk] [重复](UISearchBar: clear background color or set background imag

编程入门 行业动态 更新时间:2024-10-27 01:22:50
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 answers

How 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) @end

Hope this helps!

更多推荐

本文发布于:2023-08-02 15:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1378635.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:背景   图像   颜色   sdk   iphone

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!