English 中文(简体)
• 如何改变UISearch Bar,以案文取代案文领域内的搜索一线
原标题:how to change the UISearchBar to replace the search icon inside the text field with text

我有UISearch Bar,我想删除搜索标记(magnifier icon),并改写案文,而不是文本。

最佳回答

页: 1 改变Icon的宽度,使其适应你们的需要:

#import <UIKit/UIKit.h>

@interface SearchBoxExperimentsViewController : UIViewController {  
    IBOutlet UISearchBar *searchBar;
}
@end 

#import "SearchBoxExperimentsViewController.h"
@interface SearchBoxExperimentsViewController (Private)
- (void)setSearchIconToFavicon;
@end

@implementation SearchBoxExperimentsViewController
- (void)viewDidLoad 
{  
    [self setSearchIconToFavicon];  
    [super viewDidLoad];
}

#pragma mark Private
- (void)setSearchIconToFavicon 
{  
    // Really a UISearchBarTextField, but the header is private.  
    UITextField *searchField = nil;  
    for (UIView *subview in searchBar.subviews) {    
        if ([subview isKindOfClass:[UITextField class]]) {      
            searchField = (UITextField *)subview;      
            break;    
        }  
    }    
    if (searchField) {      
        UIImage *image = [UIImage imageNamed: @"favicon.png"];   
        UIImageView *iView = [[UIImageView alloc] initWithImage:image];    
        searchField.leftView = iView;    
        [iView release];  
    }  
}
@end 

它为我工作:

问题回答

你们可以使用UIAppearation方法

页: 1

UISearchBar.appearance().setImage(UIImage(named: "image_name"), for: .search, state: .normal)

目标c

[[UISearchBar appearance] setImage:[UIImage imageNamed:@"image_name"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal]; 

略微多一点获得核准后,API的友好之处是,使用普通的UIText Field,使用以下代码在UIText Field添加一个标签(如果你想要清除放大玻璃,你会吸引到搜索箱中去吗?)

UILabel *searchFieldLeftLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, 32, 15)];
searchFieldLeftLabel.text = @"find:";
searchFieldLeftLabel.placeholder = @"e.g. wings, Boathouse";
searchFieldLeftLabel.font = [UIFont systemFontOfSize:14];
searchFieldLeftLabel.textColor = [UIColor grayColor];
self.searchFieldLeftLabel.leftView = locationSearchFieldLeftLabel;
[searchFieldLeftLabel release];

其结果是一个 n色的方框,它看着:

“entergraph

And when you start typing text, it replaces the placeholder, but the label remains:

“enterography

。 消除UISearchbar左边的形象。 没有任何公共宣传工具来改变或拆除放大器,而你只是使用UIText油田。





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签