【问题标题】:Disable UISearchBar search icon and placeholder text animation on becomeFirstResponder在 becomeFirstResponder 上禁用 UISearchBar 搜索图标和占位符文本动画
【发布时间】:2013-11-18 01:33:26
【问题描述】:

我有一个 UISearchBar,我想在用户点击按钮时显示它。在 buttonPress 方法中,我创建了 searchBar 并将其添加为子视图,然后调用 [searchBar becomeFirstResponder]。如果我取出这个 becomeFirstResponder 调用,搜索图标和占位符文本会出现在 textField 的中心。

然后,当搜索栏成为第一响应者时,两者都以动画方式左对齐。

因为我连续执行这两个操作,所以我得到了一个奇怪的动画,其中图标和占位符从 (0,0) 开始动画。

如何禁用此动画,以便我可以简单地将第二张图片添加到我的视图中?

编辑:

我让占位符文本通过使用正确显示

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    [searchBar setPlaceholder:@"Type Here to Search"];
}

我可以使用[searchBar setPositionAdjustment:UIOffsetMake(x, y) forSearchBarIcon:UISearchBarIconSearch]; 移动搜索图标,但更改仍会应用到动画中。

【问题讨论】:

    标签: ios animation uisearchbar placeholder becomefirstresponder


    【解决方案1】:

    这是一个 hack,但如果没有它你就活不下去。

    编辑:如果你也想要占位符文本,你可以这样做。

    最初将占位符文本设置为@" ",然后

    [self.searchBar setText:@"Place holder"];
    
    [self.searchBar setPlaceholder:@"Place holder"];
    
    [[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor grayColor]];
    

    在它成为第一响应者之后,

    [self.searchBar setText:@""];
    

    【讨论】:

    • 为什么要设置占位符文本,使用文本
    • 我需要占位符文本,因此当用户开始输入时,它会消失并替换为输入的字符。对于搜索图标,我可以使用 [setPositionAdjustment:forSearchBarIcon] 将其动画到指定位置,但它仍然使用动画。
    • 占位符文本将居中对齐。我认为你无法改变这一点
    • 不,占位符文本在 textField 处于非活动状态时居中对齐,然后在成为 firstResponder 时动画到左对齐,然后当用户按下某个键时,它就会消失。然后它会在 textField 为空时重新出现。
    【解决方案2】:

    它对我有用(仅在 ios7 上测试)

    @interface SearchBar : UISearchBar
    @end
    
    @implementation SearchBar
    - (void)layoutSubviews
    {
        [super layoutSubviews];
    
        UITextField *textField = (UITextField *)[self firstSubviewOfClass:[UITextField class]];
        UIImageView *imageView = (UIImageView *)[textField firstSubviewMatchingBlock:^BOOL(UIView *obj) {
            return [obj isKindOfClass:[UIImageView class]] && obj.layer.animationKeys.count;
        }];
        [imageView.layer removeAllAnimations];
        UILabel *label = (UILabel *)[self firstSubviewOfClass:[UILabel class]];
        [label.layer removeAllAnimations];
    }
    @end
    

    【讨论】:

      【解决方案3】:

      占位符末尾的一定数量的空格为我解决了这个问题。请注意,它应该是准确的空格数 - 不能少也不能多。

      【讨论】:

        【解决方案4】:

        使用此代码隐藏搜索栏的图标:-

        [searchBar setImage:[UIImage imageNamed:@"searchIcon.jpg"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
        

        使用 searchIcon.jpg 名称获取透明图像并隐藏您的图标 它为我工作

        [searchBar setText:@""];
        

        【讨论】:

          【解决方案5】:

          @user3429963 绝对是 looking 在正确的方向。这对我有用:

          searchBar.becomeFirstResponder()
          searchBar.removeLayerAnimationsRecursively()
          

          其中removeLayerAnimationsRecursively() 是一个从视图层及其子视图层递归删除动画的函数:

          extension UIView {
          
            func removeLayerAnimationsRecursively() {
              layer.removeAllAnimations()
              subviews.forEach { $0.removeLayerAnimationsRecursively() }
            }
          }
          

          【讨论】:

            【解决方案6】:
            override func viewDidAppear(_ animated: Bool) {
                super.viewDidAppear(animated)
                UIView.setAnimationsEnabled(false)
                self.searchController?.isActive = true
                self.searchController?.searchBar.becomeFirstResponder()
            }
            
            func didPresentSearchController(_ searchController: UISearchController) {
                searchController.searchBar.becomeFirstResponder()
                UIView.setAnimationsEnabled(true)
            }
            

            【讨论】:

              猜你喜欢
              • 2018-06-07
              • 1970-01-01
              • 1970-01-01
              • 2014-04-05
              • 1970-01-01
              • 2013-06-27
              • 2018-10-07
              • 2015-06-05
              • 2011-07-29
              相关资源
              最近更新 更多