我是 iOS 开发的新手,我需要自定义 UISearchBar 外观方面的帮助。
这就是我想要做的事情
首先,如何摆脱周围的灰色条,然后如何更改白色文本字段的背景。
另外,我创建了一个扩展 UISearchBar 并将其应用于搜索栏的新类,但我不知道调用哪个方法来初始化它,比如
- (void) viewDidLoad {}
在 ViewController 类中。在 UISearchBar 类中放置我的自定义代码的位置。
Best Answer-推荐答案 strong>
这是一个包含一些选项的搜索栏,更改其中一些值以根据需要调整它,如果你愿意,可以像模板一样使用它:
UISearchBar * _searchBar = [[UISearchBar alloc] init];
[_searchBar setPlaceholder"Search..."];
[_searchBar setTranslucent:TRUE];
[_searchBar setBarStyle:UIBarStyleDefault];
[_searchBar setSearchBarStyle:UISearchBarStyleProminent];
[_searchBar setDelegate:self];
[_searchBar setBarTintColor:[UIColor whiteColor]];
[_searchBar setTintColor:[UIColor redColor]];
[_searchBar.layer setBorderColor:[UIColor clearColor].CGColor];
[_searchBar setTranslatesAutoresizingMaskIntoConstraints:FALSE];
[_searchBar setBackgroundImage:[UIImage new]];
[_searchBar setBackgroundColor:[UIColor whiteColor]];
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setDefaultTextAttributes{
NSFontAttributeName:fontMagicForRegularNSHFont,
NSForegroundColorAttributeName:[UIColor purpleColor]
}];
[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitleTextAttributes{
NSForegroundColorAttributeName:[UIColor purpleColor]
} forState:UIControlStateNormal];
[[UISearchBar appearance] setSearchFieldBackgroundImage:[UIImage imageNamed"text_box"] forState:UIControlStateNormal];
UIImage* image = [UIImage imageNamed"searchy"];
[[UIImageView appearanceWhenContainedIn:[UISearchBar class], nil] setBounds:CGRectMake(0, 0, image.size.width, image.size.height)];
[[UISearchBar appearance] setImage:[UIImage imageNamed"exitred"] forSearchBarIcon:UISearchBarIconClear state:UIControlStateNormal];
[[UISearchBar appearance] setImage:[UIImage imageNamed"exitred"] forSearchBarIcon:UISearchBarIconClear state:UIControlStateHighlighted];
[[UISearchBar appearance] setImage:[UIImage imageNamed"searchy"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
[[UISearchBar appearance] setImage:[UIImage imageNamed"searchy"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateHighlighted];
这是第二个问题的答案:
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setBackgroundColor:[UIColor purpleColor]];
不要遍历 subview ,这是一种无知的方法,当 Apple 自由地让您使用他们的 API 来使用 UIKit 更改 UITextField 的背景颜色时。您会在堆栈上看到大多数类似事情的答案都声称使用通过 subview 的迭代,但这些都是失败的答案,也是 StackOverflow 充满无用垃圾的另一个原因。
另外,这里来自 Apple Docs,实际上来自 UIAppearance.h 的头文件
/* 要自定义包含在容器类实例中的类实例或层次结构中的实例的外观,请使用 +appearanceWhenContainedIn: 作为适当的外观代理。例如:
[[UINavigationBar appearanceWhenContainedIn:[UISplitViewController class], nil] setBarTintColor:myColor];
[[UINavigationBar appearanceWhenContainedIn:[UITabBarController class], [UISplitViewController class], nil] setBarTintColor:myTabbedNavBarColor];
*/
希望这会有所帮助,事实上,它会有所帮助,如果您按照上面的说明操作,您将不必再担心自定义,一切都会变得 super 简单。祝你好运!
要点行动:
https://gist.github.com/anonymous/b38bfd23130981d02bca
https://gist.github.com/anonymous/2358875a896895df3e5b
要使用这样的子类,你必须创建一个没有 StoryBaord 的 UIViewController,然后这样做:
CCCUSTHomeViewController.m
#import "EXAMPSubViewWithSearchBar.h"
#import "CCCUSTHomeViewController.h"
@interface CCCUSTHomeViewController ()
@end
@implementation CCCUSTHomeViewController
- (id)initWithNibNameNSString *)nibNameOrNil bundleNSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
}
return self;
}
-(void)loadView
{
[self setView:[EXAMPSubViewWithSearchBar new]];
}
-(EXAMPSubViewWithSearchBar*)contentView
{
return (id)[self view];
}
@end
然后是 UIViewController 的标题:
#import <UIKit/UIKit.h>
@interface CCCUSTHomeViewController : UIViewController
@end
关于ios - UISearchBar 自定义类和外观,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/31879496/
|