gpt4 book ai didi

iphone - UISearchBar 文本字段中的自定义清除按钮

转载 作者:可可西里 更新时间:2023-11-01 03:37:53 24 4
gpt4 key购买 nike

我试过:

UITextField *searchtextfield = [searchBar.subviews objectAtIndex:1];
UIButton *cButton = [UIButton buttonWithType:UIButtonTypeCustom];
cButton.frame = CGRectMake(0, 0, 20 , 20);
cButton.backgroundColor = [UIColor clearColor];
[cButton setImage:[UIImage imageNamed:@"x-button"] forState:UIControlStateNormal];//your button image.
cButton.contentMode = UIViewContentModeScaleToFill;
[cButton addTarget:self action:@selector(xButtonPressed) forControlEvents:UIControlEventTouchUpInside];//This is the custom event
[searchtextfield setRightView:cButton];
[searchtextfield setRightViewMode:UITextFieldViewModeWhileEditing];

但是效果不是很好...它显示自定义按钮,但是当您键入内容时,旧按钮又回来了...

我该怎么做?

最佳答案

如果你想在 UISearchBar 中设置一个自定义清除按钮试试这个:

[[UISearchBar appearance] setImage:[UIImage imageNamed:@"MyClearButton.png"] forSearchBarIcon:UISearchBarIconClear state:UIControlStateNormal];

不要忘记为 UIControlStateHighlighted 设置图像

[[UISearchBar appearance] setImage:[UIImage imageNamed:@"HighlightedClearButton.png"] forSearchBarIcon:UISearchBarIconClear state:UIControlStateHighlighted];

关于iphone - UISearchBar 文本字段中的自定义清除按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13862050/

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