【问题标题】:UITextField from UISearchBar in iOS 7.1iOS 7.1 中来自 UISearchBar 的 UITextField
【发布时间】:2014-04-25 07:34:01
【问题描述】:

我正在访问 searchbar 的文本字段。这一直在变化

对于 iOS 6,我正在使用

    textField = [searchbar.subviews objectAtIndex:1];

它在 iOS 7 中发生了变化,有一个视图,其中有一个子视图,我们可以从中获取 textField ,所以我正在为 iOS 7 或更高版本的 iOS 使用以下代码

    UIView *searchbarview = [searchbar.subviews objectAtIndex:0];
    NSLog(@"searchbarview.subviews Array is %@",searchbarview.subviews.description);
    textField = (UITextField *)[searchbarview.subviews objectAtIndex:1];

现在从 iOS 7.1 开始,searchbarview 只有一个子视图,即 UISearchBarBackground,这与 iOS 7.0 不同,它有两个子视图,所以即使我使用 for 循环遍历所有子视图,我也无法获取 textField

谁能帮我解决这个问题 谢谢,

编辑 这是 searchbarview.subviews 返回的数组日志,您可以看到它只返回 1 个对象,即 UISearchBarBackground,所以我想知道 UITextField 对象在哪里

searchbarview.subviews Array is (
"<UISearchBarBackground: 0xe6bc9d0; frame = (0 0; 768 50); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0xe6bcb20>>"
)

【问题讨论】:

  • 为什么要访问 UITextField?
  • 我想改变textfield的文本对齐方式
  • 你能记录UISearchBarBackground对象的子视图吗?
  • 你可以在viewWillAppear试试你的代码!也许在显示搜索栏之前尚未分配 textField-object 。我在创建 UISearchBar 后尝试了我的代码,并遇到了像你这样的问题。

标签: ios objective-c uitextfield uisearchbar


【解决方案1】:

使用此代码从UISearchBar 获取UITextField

    //First add the following macro:
    #define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

    //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    //Then get the text field object:
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
    {
        for (id object in [[[yourSearchBar subviews] objectAtIndex:0] subviews])
        {
            if ([object isKindOfClass:[UITextField class]])
            {
                NSLog(@"text field found");
                UITextField *textField = (UITextField *)object;
                //do some actions
                break;
            }
        }
    }
    else
    {
        for (id object in [yourSearchBar subviews])
        {
            if ([object isKindOfClass:[UITextField class]])
            {
                NSLog(@"text field found");
                UITextField *textField = (UITextField *)object;
                //do some actions
                break;
            }
        }
    }

【讨论】:

  • 感谢您的回答,但这里的问题是它没有进入 If 条件 if ([object isKindOfClass:[UITextField class]]) 条件,因为在获得的数组中没有这样的子视图
  • @Bhumit 我刚刚在 iOS 7.1 上对其进行了测试,它正在寻找文本字段。你在 if 语句里面放了 log 吗?
  • 是的,已经试过这里是日志 searchbar.subviews Array is (">" ) 你只能看到 1 个对象
  • @Bhumit 实际上从 iOS 7 开始,搜索栏上唯一的子视图是 UIView,它包含文本字段。这很奇怪。你能显示你的日志代码吗?
  • 是的对我来说也很奇怪,我已经编辑了我的日志问题代码是 NSLog(@"searchbarview.subviews Array is %@",searchbarview.subviews.description);
【解决方案2】:

我已经在 iOS 7.1 的模拟器上进行了测试。我看到 UISearchBar 的结构是这样的:

================================================

UISearchBar

++ UIView

++++ UISearchBarBackground

++++ UISearchBarTextField(这是 UITextField 对象)

++++++++ _UISearchBarSearchFieldBackgroundView

++++++++ UIImageView

================================================

所以在 iOS 7.1 上,你可以像这样得到 UITextField:

UIView *subviews = [searchbar.subviews lastObject];
UITextField *textView = (id)[subviews.subviews objectAtIndex:1];

================================================

更新

如果您的 UISearchBar 位于 UIViewController 中,请在方法 viewDidLoadviewWillAppear 中尝试您的代码!

一创建UISearchBar就不要寻找UITextField(在UISearchBar已经出现在屏幕上之后再寻找)

【讨论】:

  • 感谢这里的回答 subviews.subview 为我提供了 1 个视图的数组,即 UISearchBarBackground
  • 你能解释更多吗?你什么时候调用这些代码行?当 searchBar 刚刚分配或 searchBar 显示在屏幕上时?
  • 您确实是对的,我在 viewWillAppear 中尝试过并得到了 textField 。非常感谢您的帮助:)
【解决方案3】:

如果要获取UISearchBarUITextField,请使用以下代码行:

UITextField *textField = [searchBar valueForKey:@"_searchField"];

希望对您有所帮助! =)

【讨论】:

  • 密钥是私有的,应用程序可能会被拒绝。
【解决方案4】:

建议的答案使用私钥 - 将被 Apple 拒绝 - 或使用 NSArray,您无法确定这将在未来持续存在。如果你想改变文本的颜色、属性等,使用appearanceWhenContainedIn:

    [[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextAlignment:NSTextAlignmentCenter];

【讨论】:

    【解决方案5】:

    我创建了一个可以在任何版本的 iOS 中工作的递归方法

    - (UITextField *) findTextFieldFromControl:(UIView *) view
    {
        for (UIView *subview in view.subviews)
        {
            if ([subview isKindOfClass:[UITextField class]])
            {
                return (UITextField *)subview;
            }
            else if ([subview.subviews count] > 0)
            {
                UIView *view = [self findTextFieldFromControl:subview];
                if (view) {
                    return (UITextField *)view;
                }
            }
        }
        return nil;
    }
    

    你可以调用这个方法

    UITextField *searchBarTextField = [self findTextFieldFromControl:self.placeSearchBar];
    

    【讨论】:

    • Dint 适用于 iOS 9 ... Apple 可能再次对其进行了更改。修改 - (UITextField *) findTextFieldFromControl:(UIView *) view { int subViewIndex = 0; for (UIView *subview in view.subviews) { subViewIndex++; if ([subview isKindOfClass:[UITextField class]]) { return (UITextField *)subview; } else if (subViewIndex 0) { subViewIndex = 0;返回 [self findTextFieldFromControl:subview]; } } 返回零; }
    【解决方案6】:

    您必须先获取文本字段。
    不幸的是,文本字段是一个私有属性。

    您可以循环和索引子视图以获取文本字段私有属性,
    也可以直接使用私有属性名获取文本字段。

    两种方式都是一样的,你访问的是私有属性。

    1。循环

    for subview in searchBar.subviews {
        for view in subview.subviews {
            if let searchField = view as? UITextField {
                // do something with the search field
            }
        } 
    }
    

    2。带钥匙

    let textField = searchBar.valueForKey("searchField") as? UITextField
    let cancelBarButton = searchBar.valueForKey("cancelBarButtonItem") as? UIBarButtonItem
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-01
      • 2014-04-16
      • 2014-07-07
      • 2018-04-20
      • 2013-10-14
      • 2015-10-13
      • 2023-03-26
      • 1970-01-01
      相关资源
      最近更新 更多