UISearchBar / 키보드 검색 버튼 제목 변경
UISearchBar 컨트롤에서 키보드의 검색 키 제목을 완료로 변경하는 방법이 있습니까?
tablesearchbar라는 검색 표시 줄의 경우 :
// Set the return key and keyboard appearance of the search bar
for (UIView *searchBarSubview in [tableSearchBar subviews]) {
if ([searchBarSubview conformsToProtocol:@protocol(UITextInputTraits)]) {
@try {
[(UITextField *)searchBarSubview setReturnKeyType:UIReturnKeyDone];
[(UITextField *)searchBarSubview setKeyboardAppearance:UIKeyboardAppearanceAlert];
}
@catch (NSException * e) {
// ignore exception
}
}
}
적어도 iOS 8의 경우 간단히 :
[self.searchBar setReturnKeyType:UIReturnKeyDone];
iOS 7 베타 5부터 Run Loop의 답변이 작동하지 않았지만 다음과 같이 작동했습니다.
for(UIView *subView in [searchBar subviews]) {
if([subView conformsToProtocol:@protocol(UITextInputTraits)]) {
[(UITextField *)subView setReturnKeyType: UIReturnKeyDone];
} else {
for(UIView *subSubView in [subView subviews]) {
if([subSubView conformsToProtocol:@protocol(UITextInputTraits)]) {
[(UITextField *)subSubView setReturnKeyType: UIReturnKeyDone];
}
}
}
}
한 가지 더 유용한 힌트는 Run Loop 코드 ( "@try") 섹션에 있습니다.
이렇게하면 텍스트 필드가 비어있을 때 "완료"버튼이 활성화됩니다.
UITextField *tf = (UITextField *)searchBarSubview;
tf.enablesReturnKeyAutomatically = NO;
Swift가 UISearchBar의 리턴 키를 변경하려면
searchBar.returnKeyType = UIReturnKeyType.done
사용 가능한 열거 형은 다음과 같습니다.
public enum UIReturnKeyType : Int {
case default
case go
case google
case join
case next
case route
case search
case send
case yahoo
case done
case emergencyCall
@available(iOS 9.0, *)
case continue
}
알림! searchBar가 첫 번째 응답자로 남아 있으면 returnKeyType을 변경 한 후 키보드를 닫고 다시 팝업하여 변경 사항을 확인해야합니다.
search.resignFirstResponder()
searchBar.returnKeyType = UIReturnKeyType.Done
search.becomeFirstResponder()
선택적 메서드가있는 프로토콜이므로 try-catching 대신 각 메서드를 개별적으로 테스트해야합니다.
for (UIView *searchBarSubview in searchBar.subviews)
{
if ([searchBarSubview conformsToProtocol:@protocol(UITextInputTraits)])
{
// keyboard appearance
if ([searchBarSubview respondsToSelector:@selector(setKeyboardAppearance:)])
[(id<UITextInputTraits>)searchBarSubview setKeyboardAppearance:UIKeyboardAppearanceAlert];
// return key
if ([searchBarSubview respondsToSelector:@selector(setReturnKeyType:)])
[(id<UITextInputTraits>)searchBarSubview setReturnKeyType:UIReturnKeyDone];
// return key disabled when empty text
if ([searchBarSubview respondsToSelector:@selector(setEnablesReturnKeyAutomatically:)])
[(id<UITextInputTraits>)searchBarSubview setEnablesReturnKeyAutomatically:NO];
// breaking the loop when we are done
break;
}
}
This will work for iOS <= 6. For iOS >= 7, you need to loop in searchBar.subviews[0].subviews
.
Since the Alert-style keyboards are semi-transparent, I can see my view behind it. It doesn't look very good since I have multiple elements behind the keyboard that makes it hard for the keys to stand out. I wanted an all-black keyboard.
So I animated a black UIImageView into position behind the keyboard when text is edited. This gives the appearance of an all-black keyboard.
- (void)textFieldDidBeginEditing:(UITextField *)textField {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.25];
blackBoxForKeyboard.frame = CGRectMake(0, 377, 320, 216);
[UIView commitAnimations];
}
I tried all the solutions shown here, and none of them worked for my UISearchBar (xcode5 compiling for iOS7). I ended up with this recursive function which worked for me:
- (void)fixSearchBarKeyboard:(UIView*)searchBarOrSubView {
if([searchBarOrSubView conformsToProtocol:@protocol(UITextInputTraits)]) {
if ([searchBarOrSubView respondsToSelector:@selector(setKeyboardAppearance:)])
[(id<UITextInputTraits>)searchBarOrSubView setKeyboardAppearance:UIKeyboardAppearanceAlert];
if ([searchBarOrSubView respondsToSelector:@selector(setReturnKeyType:)])
[(id<UITextInputTraits>)searchBarOrSubView setReturnKeyType:UIReturnKeyDone];
if ([searchBarOrSubView respondsToSelector:@selector(setEnablesReturnKeyAutomatically:)])
[(id<UITextInputTraits>)searchBarOrSubView setEnablesReturnKeyAutomatically:NO];
}
for(UIView *subView in [searchBarOrSubView subviews]) {
[self fixSearchBarKeyboard:subView];
}
}
I then called it like so:
_searchBar = [[UISearchBar alloc] init];
[self fixSearchBarKeyboard:_searchBar];
Just for covering all the iOS versions:
NSArray *subviews = [[[UIDevice currentDevice] systemVersion] floatValue] < 7 ? _searchBar.subviews : _searchBar.subviews[0].subviews;
for (UIView *subview in subviews)
{
if ([subview conformsToProtocol:@protocol(UITextInputTraits)])
{
UITextField *textField = (UITextField *)subview;
[textField setKeyboardAppearance: UIKeyboardAppearanceAlert];
textField.returnKeyType = UIReturnKeyDone;
break;
}
}
ReferenceURL : https://stackoverflow.com/questions/2705865/change-uisearchbar-keyboard-search-button-title
'Programing' 카테고리의 다른 글
PHP에서 CSV 파일을 업로드하고 구문 분석하는 방법 (0) | 2021.01.09 |
---|---|
iOS 앱 아이콘에서 배지 제거 (0) | 2021.01.09 |
Kubernetes는 예기치 않은 배포 SchemaError를 만듭니다. (0) | 2021.01.09 |
UIImage가있는 Swift 플레이 그라운드 (0) | 2021.01.09 |
루비에서 배열을 같은 부분으로 나누기 (0) | 2021.01.09 |