gpt4 book ai didi

iphone UISearchBar 完成按钮始终启用

转载 作者:行者123 更新时间:2023-12-03 18:19:28 27 4
gpt4 key购买 nike

我有一个带有 UISearchBarUIViewController。我已将“搜索”按钮替换为“完成”按钮。

但是,当点击搜索栏时,“完成”按钮最初禁用。在输入任何字符之前都会发生这种情况。

我想要做的是让这个“完成”按钮始终启用,这样,如果我点击它,我可以立即关闭键盘。

有什么帮助吗?我们将不胜感激。

我的UIViewController

-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar  
{
return YES;
}

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
}

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if (searchBar.text.length == 0)
{
//[self fixOrientation];
[searchBar resignFirstResponder];
}
else
{
NSLog(@"typed");
}
}


-(void)searchBarTextDidBeginEditing:(UISearchBar *)theSearchBar
{
NSLog(@"began"); // this executes as soon as i tap on the searchbar, so I'm guessing this is the place to put whatever solution is available
}

最佳答案

您可以通过循环 UISearchBar 中的 subview 直到找到文本字段来解决此问题。只需将“enablesReturnKeyAutomatically”设置为“否”即可。顺便说一句,以下代码对于设置键盘类型也很有用。

  // loop around subviews of UISearchBar
for (UIView *searchBarSubview in [searchBar subviews]) {
if ([searchBarSubview conformsToProtocol:@protocol(UITextInputTraits)]) {
@try {
// set style of keyboard
[(UITextField *)searchBarSubview setKeyboardAppearance:UIKeyboardAppearanceAlert];

// always force return key to be enabled
[(UITextField *)searchBarSubview setEnablesReturnKeyAutomatically:NO];
}
@catch (NSException * e) {
// ignore exception
}
}
}

关于iphone UISearchBar 完成按钮始终启用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4728338/

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