我已经在表视图中实现了一个UISearchbar,几乎所有内容都在工作外:当我输入文本然后按键盘上的搜索按钮时,键盘消失了,搜索结果是表中唯一显示的项目,文本停留在UISearchbar中,但是“取消”按钮被禁用。

我一直在尝试将我的列表与Apple Contacts应用程序的功能接近,当您按该应用程序中的搜索时,它不会禁用CANCEL按钮。

当我在Uisearchbar标头文件中查看时,我注意到_searchbarflags struct下的AutodisableCancelbutton的标志,但它是私有的。

设置Uisearchbar时,我是否缺少一些东西?

有帮助吗?

解决方案

我找到了一个解决方案。您可以使用此循环循环在搜索栏的子视图上循环,并在键盘上按下搜索按钮时启用它。

for (UIView *possibleButton in searchBar.subviews)
{
    if ([possibleButton isKindOfClass:[UIButton class]])
    {
        UIButton *cancelButton = (UIButton*)possibleButton;
        cancelButton.enabled = YES;
        break;
    }
}

其他提示

我不得不进行一些调整以使其在ios7中为我工作

- (void)enableCancelButton:(UISearchBar *)searchBar
{
    for (UIView *view in searchBar.subviews)
    {
        for (id subview in view.subviews)
        {
            if ( [subview isKindOfClass:[UIButton class]] )
            {
                [subview setEnabled:YES];
                NSLog(@"enableCancelButton");
                return;
            }
        }
    }
}

有两种方法可以轻松实现这一目标

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    //  The small and dirty
    [(UIButton*)[searchBar valueForKey:@"_cancelButton"] setEnabled:YES];

    // The long and safe
     UIButton *cancelButton = [searchBar valueForKey:@"_cancelButton"];
    if ([cancelButton respondsToSelector:@selector(setEnabled:)]) {
         cancelButton.enabled = YES;
    }
}

您应该使用第二个,如果Apple会在后台更改它,它将不会崩溃您的应用程序。

顺便说一句,我将其从iOS 4.0到8.2进行了测试,并且没有更改,也没有任何问题在商店批准的应用程序中使用它。

这就是使我在iOS 6上使用的原因:

searchBar.showsCancelButton = YES;
searchBar.showsScopeBar = YES;
[searchBar sizeToFit];
[searchBar setShowsCancelButton:YES animated:YES];

这是我的解决方案,适用于所有版本的iOS的所有情况。

即,由于用户拖动滚动视图,因此其他解决方案在键盘被解雇时无法处理。

- (void)enableCancelButton:(UIView *)view {
    if ([view isKindOfClass:[UIButton class]]) {
        [(UIButton *)view setEnabled:YES];
    } else {
        for (UIView *subview in view.subviews) {
            [self enableCancelButton:subview];
        }
    }
}

// This will handle whenever the text field is resigned non-programatically
// (IE, because it's set to resign when the scroll view is dragged in your storyboard.)
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
    [self performSelector:@selector(enableCancelButton:) withObject:searchBar afterDelay:0.001];
}

// Also follow up every [searchBar resignFirstResponder];
// with [self enableCancelButton:searchBar];

这些答案根本没有对我有用。我是针对iOS 7的。但是我找到了答案。

我正在尝试的是Twitter iOS应用程序。如果您单击“时间表”选项卡中的放大镜,则 UISearchBar 出现带有取消按钮,显示键盘和最新搜索屏幕。滚动最新的搜索屏幕,并隐藏键盘,但它可以保持取消按钮激活。

这是我的工作代码:

UIView *searchBarSubview = self.searchBar.subviews[0];
NSArray *subviewCache = [searchBarSubview valueForKeyPath:@"subviewCache"];
if ([subviewCache[2] respondsToSelector:@selector(setEnabled:)]) {
    [subviewCache[2] setValue:@YES forKeyPath:@"enabled"];
}

我通过在我的桌子视图上设置一个断点到达了这个解决方案 scrollViewWillBeginDragging:. 。我看着我的 UISearchBar 并露出了其子视图。它总是只有一个 UIView (我的变量 searchBarSubview).

enter image description here

然后 UIView 持有 NSArraysubviewCache 我注意到最后一个元素是第三个元素类型 UINavigationButton, ,不是在公共API中。因此,我着手使用键值编码。我检查了是否 UINavigationButton 回应 setEnabled:, ,幸运的是,确实如此。所以我将财产设置为 @YES. 。事实证明 UINavigationButton 取消按钮。

如果苹果决定更改A的实现,这一定会破裂 UISearchBar的内脏,但是到底是什么。它目前有效。

按照 我的回答在这里, ,将其放在您的搜索栏代表中:

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{   
    dispatch_async(dispatch_get_main_queue(), ^{
        __block __weak void (^weakEnsureCancelButtonRemainsEnabled)(UIView *);
        void (^ensureCancelButtonRemainsEnabled)(UIView *);
        weakEnsureCancelButtonRemainsEnabled = ensureCancelButtonRemainsEnabled = ^(UIView *view) {
            for (UIView *subview in view.subviews) {
                if ([subview isKindOfClass:[UIControl class]]) {
                    [(UIControl *)subview setEnabled:YES];
                }
                weakEnsureCancelButtonRemainsEnabled(subview);
            }
        };

        ensureCancelButtonRemainsEnabled(searchBar);
    });
}

对于Monotouch或Xamarin IOS,我有以下C#解决方案为iOS 7和iOS 8:

foreach(UIView view in searchBar.Subviews)
{
    foreach(var subview in view.Subviews)
    {
        //Console.WriteLine(subview.GetType());
        if(subview.GetType() == typeof(UIButton))
        {
            if(subview.RespondsToSelector(new Selector("setEnabled:")))
            {
                UIButton cancelButton = (UIButton)subview;
                cancelButton.Enabled = true;
                Console.WriteLine("enabledCancelButton");
                return;
            }
        }
    }
}

这个答案是基于 大卫·道格拉斯(David Douglas) 解决方案。

一个更完整的答案:

  • 自iOS 7以来,搜索栏下还有一个附加的子视图
  • 启用取消按钮的好地方 searchBarTextDidEndEditing

.

extension MyController: UISearchBarDelegate {
  public func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
    DispatchQueue.main.async {
    // you need that since the disabling will
    // happen after searchBarTextDidEndEditing is called
      searchBar.subviews.forEach({ view in
        view.subviews.forEach({ subview in
          // ios 7+
          if let cancelButton = subview as? UIButton {
            cancelButton.isEnabled = true
            cancelButton.isUserInteractionEnabled = true
            return
          }
        })
        // ios 7-
        if let cancelButton = subview as? UIButton {
          cancelButton.isEnabled = true
          cancelButton.isUserInteractionEnabled = true
          return
        }
      })
    }
  }
}

这是一个Swift 3解决方案,可利用扩展程序来轻松获取取消按钮:

extension UISearchBar {
    var cancelButton: UIButton? {
        for subView1 in subviews {
            for subView2 in subView1.subviews {
                if let cancelButton = subView2 as? UIButton {
                    return cancelButton
                }
            }
        }
        return nil
    }
}

现在用于使用:

class MyTableViewController : UITableViewController, UISearchBarDelegate {

    var searchBar = UISearchBar()

    func viewDidLoad() {
        super.viewDidLoad()
        searchBar.delegate = self
        tableView.tableHeaderView = searchBar
    }

    func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
        DispatchQueue.main.async {
            if let cancelButton = searchBar.cancelButton {
                cancelButton.isEnabled = true
                cancelButton.isUserInteractionEnabled = true
            }
        }
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top