UISearchBar在开始编辑时会拉伸文本 [英] UISearchBar stretches text when it begins editing

查看:135
本文介绍了UISearchBar在开始编辑时会拉伸文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在UINavigationBar的标题视图中添加了一个UISearchBar实例。当已经设置了文本并且搜索栏开始编辑时,它会调整其内容的大小以允许取消按钮的空间,但是,生成的动画会拉伸文本,如下面的gif所示

I have an instance of a UISearchBar added to the title view of a UINavigationBar. When there is text already set and the search bar starts editing it resizes its contents to allow space for the Cancel button, although, the resulting animation stretches the text, as showed in the gif below

是否可以采取任何措施来避免此缺陷效果?我试图删除文本,然后稍后再添加它,虽然它有效,但它不是一个优雅的解决方案。

Is there anything that can be done to avoid this defect effect? I have tried to remove the text and then to add it back a few moments later, although it works, it is not an elegant solution.

更新

根据@ Paruru的回答,我试图预测取消的动画按钮,看起来不错。我做的是强制在 searchBarShouldBeginEditing上强制显示取消按钮:

Based on @Paruru's answer I tried to anticipate the animation of the Cancel button and it doesn't look bad. What I did is that I force the presentation of the Cancel button on searchBarShouldBeginEditing:

extension SearchViewController: UISearchBarDelegate {

    func searchBarShouldBeginEditing(searchBar: UISearchBar) -> Bool {
        if searchBar.text?.isEmpty == false {
            // This avoids the text being stretched by the UISearchBar.
            searchBar.setShowsCancelButton(true, animated: true)
        }
        return true
    }

}

最终结果是我想要实现的,没有文本被拉伸的动画。我认为这是一种解决方法,所以我会等待其他答案,因为这段代码可能不会成为未来的证据。

The end result is what I want to achieve, the animation without the text being stretched. I consider this to be a workaround, and so I'll wait for other answers as this code might not be future proof.

推荐答案

您的更新解决方案效果很好,但是当没有文本时出现取消按钮时,它会阻止搜索占位符被动画化为左侧。检查 searchBar.text 恢复动画:

Your updated solution works well, except it stops the "Search" placeholder from being animated to the left when the Cancel button appears while there is no text. Checking searchBar.text restores the animation:

func searchBarShouldBeginEditing(searchBar: UISearchBar) -> Bool {
    // This avoids the text being stretched by the UISearchBar.
    if searchBar.text?.isEmpty == false {
        searchBar.setShowsCancelButton(true, animated: true)
    }
    return true
}

我怀疑这可能只是 Minimal <$的问题c $ c> UISearchBarStyle 。

这篇关于UISearchBar在开始编辑时会拉伸文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆