【问题标题】:Customizing UISearch bar自定义 UISearch 栏
【发布时间】:2013-08-29 22:46:22
【问题描述】:

我正在尝试像这样自定义我的搜索栏

到目前为止,我已经做到了这一点

现在我需要知道如何在右侧添加图像,塑造角落和改变图像的背景。到目前为止,这是我的代码。我可以在 UITextField 的帮助下做到这一点,但我想使用 UISearchBar 做到这一点。有什么帮助吗??

   - (void)setSearchIconToFavicon 
    {  
        // commented are the things I tried out 
        UITextField *searchField = nil;  
        for (UIView *subview in searchBar.subviews)
        {    
            if ([subview isKindOfClass:[UITextField class]]) 
            {      
                searchField = (UITextField *)subview;      
                break;    
            }  
        }    
        if (searchField) 
        {      
            UIImage *image = [UIImage imageNamed: @"search_btn.png"];   
            UIImageView *iView = [[UIImageView alloc] initWithImage:image]; 
            //UIImage *image2 = [UIImage imageNamed: @"search_next_btn.png"];   
            //UIImageView *iView2 = [[UIImageView alloc] initWithImage:image2];  
            searchField.leftView = iView;    
            //searchField.rightView=iView2;
            [[self.searchBar.subviews objectAtIndex:0] removeFromSuperview];
            searchField.textColor = [UIColor grayColor];
        }  
  }

【问题讨论】:

    标签: ios objective-c uisearchbar uisearchbardelegate


    【解决方案1】:

    您可以替换书签图像,并在必要时调整其偏移量。像这样..

    [self.searchDisplayController.searchBar setImage:[UIImage imageNamed:@"yourImage"] forSearchBarIcon:UISearchBarIconBookmark state:UIControlStateNormal];
    [self.searchDisplayController.searchBar setPositionAdjustment:UIOffsetMake(0, 0) forSearchBarIcon:UISearchBarIconBookmark];
    

    并且可以在委托方法中处理按钮事件:

    - (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar
    

    希望你明白我的意思。

    【讨论】:

    • 我尝试用我在问题中发布的代码添加你的代码,但没有回应
    【解决方案2】:

    如果您的目标应用程序>=5.0,您可以使用这行代码来更改搜索栏的背景图片。

    [[UISearchBar appearance] setSearchFieldBackgroundImage:[UIImage imageNamed:@"searchbar.png"]forState:UIControlStateNormal];
    

    【讨论】:

    • 它将背景设置为与图片完全一样,背景也出现在我不想要的“按名称或点代码查找”文本后面
    猜你喜欢
    • 2011-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-11
    • 1970-01-01
    • 2012-03-12
    • 1970-01-01
    相关资源
    最近更新 更多