gpt4 book ai didi

ios - 搜索栏 subview 不工作,并且没有为搜索栏调用 UITextField 委托(delegate)方法

转载 作者:行者123 更新时间:2023-11-28 20:09:57 25 4
gpt4 key购买 nike

请看我下面的代码:

我的 viewController 中有一个搜索栏

 for (UIView *subView in  self.searchBar.subviews) 
{
if([subView conformsToProtocol:@protocol(UITextInputTraits)])
{
[(UITextField *)subView setDelegate:self];
[(UITextField *)subView setReturnKeyType:UIReturnKeyDefault];
[(UITextField *)subView setBackground:[UIImage imageNamed:@"shopping-Search"]];
}
}

for (UIView *subview in self.searchBar.subviews) {
if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
[subview removeFromSuperview];
break;
}
}

此代码在 iOS 6 中运行良好,但在 iOS 7 中无法运行。

最佳答案

iOS7 中的 searchBar subview 层次结构已更改,请尝试以下操作:

iOS7:

NSArray *searchBarSubViews = [[self.searchBar.subviews objectAtIndex:0] subviews];

iOS6 及之前版本:

NSArray *searchBarSubViews =  self.searchBar.subviews;

试试下面更新的函数:

for (UIView *subView in searchBarSubViews) {
if([subView conformsToProtocol:@protocol(UITextInputTraits)])
{
[(UITextField *)subView setDelegate:self];
[(UITextField *)subView setReturnKeyType:UIReturnKeyDefault];
[(UITextField *)subView setBackground:[UIImage imageNamed:@"shopping-Search"]];
}
}

for (UIView *subview in searchBarSubViews) {
if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
[subview removeFromSuperview];
break;
}
}

关于ios - 搜索栏 subview 不工作,并且没有为搜索栏调用 UITextField 委托(delegate)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20680331/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com