gpt4 book ai didi

ios - 在 becomeFirstResponder 上禁用 UISearchBar 搜索图标和占位符文本动画

转载 作者:可可西里 更新时间:2023-11-01 03:19:47 25 4
gpt4 key购买 nike

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

enter image description here

然后,当搜索栏成为第一响应者时,两者都设置为左对齐。

enter image description here

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

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

编辑:

我通过使用获得了占位符文本以正确显示

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

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

最佳答案

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

searchBar.becomeFirstResponder()
searchBar.removeLayerAnimationsRecursively()

其中 removeLayerAnimationsRecursively() 是一个从 View 层及其 subview 层递归地移除动画的函数:

extension UIView {

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

关于ios - 在 becomeFirstResponder 上禁用 UISearchBar 搜索图标和占位符文本动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19818431/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com