Ios7 无法在ios 7中的搜索栏文本字段中设置rightview或leftview
目的: 我想在搜索栏的文本字段中将右视图设置为标签 以下代码在ios 6中可以很好地执行相同的操作:Ios7 无法在ios 7中的搜索栏文本字段中设置rightview或leftview,ios7,uisearchbar,textfield,rightview,Ios7,Uisearchbar,Textfield,Rightview,目的: 我想在搜索栏的文本字段中将右视图设置为标签 以下代码在ios 6中可以很好地执行相同的操作: UISearchBar *search = [[UISearchBar alloc] init]; [search setTintColor:[UIColor grayColor]]; search.frame = CGRectMake(0, 150, 320,50); search.delegate = self; UITextField *searchField=[search.subv
UISearchBar *search = [[UISearchBar alloc] init];
[search setTintColor:[UIColor grayColor]];
search.frame = CGRectMake(0, 150, 320,50);
search.delegate = self;
UITextField *searchField=[search.subviews objectAtIndex:1];//Changed this line in ios 7
searchField.backgroundColor=[UIColor redColor];
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
label.text=@"Hello";
label.textColor=[UIColor greenColor];
searchField.rightView = label;
searchField.rightViewMode = UITextFieldViewModeAlways;
[self addSubView:search];
当我在ios 7中运行相同的代码(xcode 7.0.2完全更新)时,它给了我一个错误,在谷歌搜索后,我发现搜索栏到文本字段的层次结构已经改变,所以我将代码修改为:
将上述代码中的注释行替换为:
UITextField *searchField=[((UIView *)[search.subviews objectAtIndex:0]).subviews lastObject];
这样做之后,它没有给我任何错误(还通过打印textfield的描述检查了日志,一切正常),当我运行它时,它给textfield以红色背景,但rightView的标签没有显示给我。现在,有谁能帮我展示一下这个rightview。首先,你的方法不太好,而且无法跨iOS版本进行维护。 我认为在您将自定义视图设置为UISearchBarTextField的rightView属性之后,将初始化它的right视图。如果在ViewDidDisplay方法中设置searchField.rightView,您将看到正确的结果:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
UITextField *searchField=[((UIView *)[_searchBar.subviews objectAtIndex:0]).subviews lastObject];
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
label.text=@"Hello";
label.textColor=[UIColor greenColor];
searchField.rightView = label;
searchField.rightViewMode = UITextFieldViewModeAlways;
}
我已经在iOS 7模拟器上对它进行了测试,它可以正常工作。如果你想通过编程实现这一点,那么你必须在.h文件中声明
UISearchBar
对象
UISearchBar *searchBar;
在.m文件中,您可以编写如下代码:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
searchBar = [[UISearchBar alloc] init];
[searchBar setTintColor:[UIColor grayColor]];
searchBar.frame = CGRectMake(0, 150, 320,50);
searchBar.delegate = self;
[self.view addSubview:searchBar];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
UITextField *searchField;
if ([[[UIDevice currentDevice] systemVersion] floatValue]<7.0)
searchField=[searchBar.subviews objectAtIndex:1];
else
searchField=[((UIView *)[searchBar.subviews objectAtIndex:0]).subviews lastObject];
searchField.backgroundColor=[UIColor redColor];
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
label.text=@"Hello";
label.textColor=[UIColor greenColor];
searchField.rightView = label;
searchField.rightViewMode = UITextFieldViewModeAlways;
}
-(void)viewDidLoad
{
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置。
searchBar=[[UISearchBar alloc]init];
[搜索栏setTintColor:[UIColor grayColor]];
searchBar.frame=CGRectMake(0,150320,50);
searchBar.delegate=self;
[self.view addSubview:searchBar];
}
-(无效)视图显示:(BOOL)动画{
[超级视图显示:动画];
UITextField*搜索字段;
如果([[[UIDevice currentDevice]systemVersion]floatValue]您必须首先获取文本字段。
无需索引子视图即可获取文本字段或栏按钮
您可以直接获取文本字段和栏按钮
迅速地
let textField = searchBar.valueForKey("searchField") as? UITextField
let cancelBarButton = searchBar.valueForKey("cancelBarButtonItem") as? UIBarButtonItem
在Objective-C中
UITextField *textField = [searchBar valueForKey:@"searchField"]:
UIBarButtonItem *cancelBarButton = [searchBar valueForKey:@"cancelBarButtonItem"]:
然后可以使用设置左视图和右视图
textField.leftView = yourView
textField.rightView = yourView
很好,如果我在xib中创建searchbar并完成这一切,这对我来说是可行的,但是如果我尝试使用代码创建searchbar,然后尝试放置rightView,那么它就不起作用了,你能建议我如何通过编程来实现吗?问题实际上是iOS7中视图的层次结构是针对一些视图的,如UISearchBar、UIAlertView等在iOS的早期版本中工作的视图自定义代码在iOS7中不起作用。如果我只想在一个地方以任何方法完成所有这些工作,我不能这样做吗?是否有必要在ViewDidDisplay或任何其他方法中进行此自定义最好在ViewDidDisplay中进行自定义,因为如果您进行初始化和customization就位后,定制代码将在UISearchbar的某些内部设置之前执行,因此您将无法获得所需的确切结果。