ios - 如何设置UISearchBar的光标位置?

标签 ios uisearchbar

是否可以在 UISearchBar 中设置光标。我知道 UITextField 可以实现,但我想知道 UISearchBar 是否可以实现。

对于文本字段,它是:

    [textField setSelectedTextRange:...];

最佳答案

这不是最好的解决方案。但这仍然是解决方案。

这是 UISearchBar.h 的一部分:

NS_CLASS_AVAILABLE_IOS(2_0) @interface UISearchBar : UIView { 
  @private
    UITextField            *_searchField;

使用 KVC,您可以从搜索栏中获取文本字段:

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

然后处理文本字段:

[textField setSelectedTextRange:...]; 

关于ios - 如何设置UISearchBar的光标位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18851526/

相关文章:

swift - 为什么 UISearchBar 在 Swift 中跳下并覆盖/重叠第一行?

iphone - 寻找一种更好的方法来隐藏 View 顶部的搜索栏

swift - 搜索字段和表格 View - 仅显示用户名大写的用户

ios - 带有 AVPlayer iOS 的 Chrome-cast

objective-c - 在 Objective-c 中绘图 (iOS)

ios - iAds : Displaying own ads when didFailToReceiveAdWithError

ios - 如何处理 MPMoviePlayerViewController 中的下一个和上一个按钮?

ios - 仅使用约束初始化自定义 UIView (IOS)

ios - UISearchBar 没有响应

ios - UISearchBar 委托(delegate)不起作用