自定义搜索结果按钮在 UISearchBar 中具有奇怪的行为

发布于 2024-11-18 07:45:15 字数 2596 浏览 2 评论 0原文

我对 UISearchBar 进行了子类化,以便按以下方式将自定义图像设置为搜索结果按钮(.m 文件):

#import "CustomUISearchBar.h"

#define RGB(r, g, b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1]
#define RGBA(r, g, b, a) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a]

@implementation GipUISearchBar

- (void)layoutSubviews 
{    
    UITextField *searchField = nil;    
UIView *backGround = nil;
UIButton *cancelButton = nil;
NSUInteger numViews = [self.subviews count];    
for(int i = 0; i < numViews; i++) 
{       
    if([[self.subviews objectAtIndex:i] isKindOfClass:[UITextField class]]) 
    {           
        searchField = [self.subviews objectAtIndex:i];       
    }
    else if ([[self.subviews objectAtIndex:i] isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) 
    {       
        backGround = [self.subviews objectAtIndex:i];  
    }       
    else if ([[self.subviews objectAtIndex:i] isKindOfClass:[UIButton class]]) 
    {       
        cancelButton = [self.subviews objectAtIndex:i];                     [cancelButton setTintColor:RGB(22.0,38.0,111.0)];

    }
}    

if(!(searchField == nil)) 
{        
    searchField.layer.cornerRadius=15.0f;
    searchField.layer.masksToBounds=YES;
    searchField.layer.borderColor = [RGB(26.0,141.0,223.0)CGColor];
    searchField.layer.borderWidth = 2.0f;       

    UIImage *glassImage = [UIImage imageNamed: @"search_icon.png"];    
    UIImageView *iView = [[UIImageView alloc] initWithImage:glassImage];    
    searchField.leftView = iView;    
    [iView release];

    UIImage *historyImage = [UIImage imageNamed:@"history_button.png"];
    UIButton *historyButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [historyButton setBackgroundImage:historyImage forState:UIControlStateNormal];
    [historyButton setBackgroundImage:historyImage forState:UIControlStateSelected];
    [historyButton setBackgroundImage:historyImage forState:UIControlStateDisabled];
    [historyButton setBackgroundImage:historyImage forState:UIControlStateHighlighted];

    historyButton.frame = CGRectMake(0, 
                                     0, 
                                     historyImage.size.width-3, 
                                     historyImage.size.height-3);
    historyButton.layer.borderColor = [RGBA(0.0,0.0,0.0,0.0)CGColor];
    historyButton.layer.borderWidth = 0.0f;

    searchField.rightView = historyButton;
}

if (!(backGround == nil))
    [backGround removeFromSuperview];



[super layoutSubviews]; 
}

问题是当我触摸搜索栏内部并调整大小时,我看到该按钮从左向右移动。我怎样才能禁用这种行为?

I subclassed UISearchBar in order to set custom image to search results button the following way (.m file):

#import "CustomUISearchBar.h"

#define RGB(r, g, b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1]
#define RGBA(r, g, b, a) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a]

@implementation GipUISearchBar

- (void)layoutSubviews 
{    
    UITextField *searchField = nil;    
UIView *backGround = nil;
UIButton *cancelButton = nil;
NSUInteger numViews = [self.subviews count];    
for(int i = 0; i < numViews; i++) 
{       
    if([[self.subviews objectAtIndex:i] isKindOfClass:[UITextField class]]) 
    {           
        searchField = [self.subviews objectAtIndex:i];       
    }
    else if ([[self.subviews objectAtIndex:i] isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) 
    {       
        backGround = [self.subviews objectAtIndex:i];  
    }       
    else if ([[self.subviews objectAtIndex:i] isKindOfClass:[UIButton class]]) 
    {       
        cancelButton = [self.subviews objectAtIndex:i];                     [cancelButton setTintColor:RGB(22.0,38.0,111.0)];

    }
}    

if(!(searchField == nil)) 
{        
    searchField.layer.cornerRadius=15.0f;
    searchField.layer.masksToBounds=YES;
    searchField.layer.borderColor = [RGB(26.0,141.0,223.0)CGColor];
    searchField.layer.borderWidth = 2.0f;       

    UIImage *glassImage = [UIImage imageNamed: @"search_icon.png"];    
    UIImageView *iView = [[UIImageView alloc] initWithImage:glassImage];    
    searchField.leftView = iView;    
    [iView release];

    UIImage *historyImage = [UIImage imageNamed:@"history_button.png"];
    UIButton *historyButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [historyButton setBackgroundImage:historyImage forState:UIControlStateNormal];
    [historyButton setBackgroundImage:historyImage forState:UIControlStateSelected];
    [historyButton setBackgroundImage:historyImage forState:UIControlStateDisabled];
    [historyButton setBackgroundImage:historyImage forState:UIControlStateHighlighted];

    historyButton.frame = CGRectMake(0, 
                                     0, 
                                     historyImage.size.width-3, 
                                     historyImage.size.height-3);
    historyButton.layer.borderColor = [RGBA(0.0,0.0,0.0,0.0)CGColor];
    historyButton.layer.borderWidth = 0.0f;

    searchField.rightView = historyButton;
}

if (!(backGround == nil))
    [backGround removeFromSuperview];



[super layoutSubviews]; 
}

The problem is when i touch inside search bar and it resizes i see this button being moved from left to right. How can i disable this behaviour?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如何视而不见 2024-11-25 07:45:15

尝试在下面几行发表评论。

[super layoutSubviews];

因为您似乎通过继承 UISearchBar 来覆盖它的布局。

try commenting below lines.

[super layoutSubviews];

Because it seems that you are overriding layout of UISearchBar by inheriting it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文