xcode 自定义搜索栏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36542549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:42:54  来源:igfitidea点击:

Customize search bar

iosobjective-cxcodeuisearchbar

提问by iDeveloper

I am designing a search bar. I need the search bar to look like one in the image. Tried few ways but no changes. Help much appreciated.

我正在设计一个搜索栏。我需要搜索栏看起来像图像中的一个。尝试了几种方法,但没有改变。非常感谢帮助。

screen shot

截屏

[Edited] See the image after using Richard's code it looks like this. I want the gray color to be clear.

[已编辑] 使用 Richard 的代码后查看图像,它看起来像这样。我希望灰色清晰。

screen shot

截屏

回答by Mahendra

self.searchBaris an IBOutlet. You can also create it dynamically.

self.searchBar是一个IBOutlet。您也可以动态创建它。

self.searchBar.layer.borderWidth = 2.0;
self.searchBar.layer.borderColor = [UIColor brownColor].CGColor;
self.searchBar.layer.cornerRadius = 15.0;
self.searchBar.barTintColor = [UIColor colorWithRed:255/255.0 green:246/255.0 blue:241/255.0 alpha:1.0];
self.searchBar.backgroundColor = [UIColor clearColor];

UITextField *textField = [self.searchBar valueForKey:@"_searchField"];
textField.textColor = [UIColor brownColor];
textField.placeholder = @"Search";
textField.leftViewMode = UITextFieldViewModeNever; //hiding left view
textField.backgroundColor = [UIColor colorWithRed:255/255.0 green:246/255.0 blue:241/255.0 alpha:1.0];
textField.font = [UIFont systemFontOfSize:18.0];
[textField setValue:[UIColor brownColor] forKeyPath:@"_placeholderLabel.textColor"];

UIImageView *imgview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 30)];
imgview.image = [UIImage imageNamed:@"searchIcon.png"]; //you need to set search icon for textfield's righ view

textField.rightView = imgview;
textField.rightViewMode = UITextFieldViewModeAlways;

Output :

输出 :

enter image description here

在此处输入图片说明

回答by Rohit Khandelwal

You can use image to change UISearchBarappearance-

您可以使用图像来改变UISearchBar外观 -

[[UISearchBar appearance] setBackgroundColor:[UIColor clearColor]];   
[[UISearchBar appearance] setBackgroundImage:[UIImage imageNamed:@"searchBG.png"]];
[[UISearchBar appearance] setSearchFieldBackgroundImage:[UIImage imageNamed:@"search.png"] forState:UIControlStateNormal];

回答by UnRewa

You can use some custom solutions (it's better for customizing) For example SSSearchBaror try find some similar

您可以使用一些自定义解决方案(最好是自定义)例如SSSearchBar或尝试找到一些类似的

回答by m.alqadi

if let textfield = searchBar.value(forKey: "searchField") as? UITextField {
                textfield.textColor = UIColor.blue
                if let backgroundview = textfield.subviews.first {
                    // Background color
                    backgroundview.backgroundColor = UIColor.white
                    // Rounded corner
                    backgroundview.layer.cornerRadius = 14;
                    backgroundview.clipsToBounds = true;
                }
            }