여러 경우에 iPhone 키보드 상단에 도구 모음을 추가하고 싶습니다 (예 : 양식 요소를 탐색 할 때 iPhone Safari에서와 같이).
현재는 상수를 사용하여 툴바의 사각형을 지정하고 있지만 인터페이스의 다른 요소 (화면 상단의 툴바 및 탐색 모음)가 유동적이기 때문에 인터페이스를 약간 변경할 때마다 툴바가 정렬되지 않습니다.
현재보기와 관련하여 키보드의 위치를 프로그래밍 방식으로 결정하는 방법이 있습니까?
답변
iOS 3.2부터이 효과를 얻을 수있는 새로운 방법이 있습니다.
UITextFields및 UITextViews이 inputAccessoryView자동으로 위에 표시하고 키보드와 애니메이션 모든보기에 설정할 수있는 속성을.
사용하는 뷰는 다른 뷰 계층 구조에 있지 않아야하며 일부 수퍼 뷰에 추가해서는 안됩니다.이 작업은 자동으로 수행됩니다.
답변
그래서 기본적으로:
init 메소드에서 :
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(keyboardWillShow:) name: UIKeyboardWillShowNotification object:nil];
[nc addObserver:self selector:@selector(keyboardWillHide:) name: UIKeyboardWillHideNotification object:nil];
그런 다음 위에 언급 된 방법으로 막대의 위치를 조정합니다.
-(void) keyboardWillShow:(NSNotification *) note
{
CGRect r = bar.frame, t;
[[note.userInfo valueForKey:UIKeyboardBoundsUserInfoKey] getValue: &t];
r.origin.y -= t.size.height;
bar.frame = r;
}
감싸서 위치 변경을 애니메이션으로 만들어 예쁘게 만들 수 있습니다.
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
//...
[UIView commitAnimations];
답변
이것은 tonklon 의 기존 답변을 기반으로합니다 . 오른쪽에 “완료”버튼과 함께 키보드 상단에 반투명 검정색 도구 모음을 표시하는 코드 스 니펫을 추가하고 있습니다.
UIToolbar *toolbar = [[[UIToolbar alloc] init] autorelease];
[toolbar setBarStyle:UIBarStyleBlackTranslucent];
[toolbar sizeToFit];
UIBarButtonItem *flexButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *doneButton =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(resignKeyboard)];
NSArray *itemsArray = [NSArray arrayWithObjects:flexButton, doneButton, nil];
[flexButton release];
[doneButton release];
[toolbar setItems:itemsArray];
[aTextField setInputAccessoryView:toolbar];
그리고 -resignKeyboard다음과 같이 보입니다.
-(void)resignKeyboard {
[aTextField resignFirstResponder];
}
누군가에게 도움이되기를 바랍니다.
답변
키보드 알림 (예 :)에 등록 UIKeyboardWillShowNotification UIKeyboardWillHideNotification하면 수신되는 알림에 userInfodict ( UIKeyboardBoundsUserInfoKey) 의 키보드 경계가 포함됩니다 .
참고 항목 UIWindow클래스 참조하십시오.
답변
3.0 이상 userInfo에서는 알림 사전 에서 애니메이션 기간과 곡선을 가져올 수 있습니다 .
예를 들어, 키보드 공간을 만들기 위해 뷰의 크기를 애니메이션하려면에 등록 UIKeyboardWillShowNotification하고 다음과 같은 작업을 수행하십시오.
- (void)keyboardWillShow:(NSNotification *)notification
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:[[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]];
[UIView setAnimationDuration:[[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]];
CGRect frame = self.view.frame;
frame.size.height -= [[[notification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue].size.height;
self.view.frame = frame;
[UIView commitAnimations];
}
에 대해 유사한 애니메이션을 수행 UIKeyboardWillHideNotification합니다.
답변
이 메서드를 만들고 ViewWillLoad에서 호출합니다.
- (void) keyboardToolbarSetup
{
if(self.keyboardToolbar==nil)
{
self.keyboardToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(anyAction)];
UIBarButtonItem *extraSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(anyOtherAction)];
NSArray *toolbarButtons = [[NSArray alloc]initWithObjects:cancelButton,extraSpace,doneButton, nil];
[self.keyboardToolbar setItems:toolbarButtons];
self.myTextView.inputAccessoryView=self.keyboardToolbar;
}
}
답변
키보드보기의 크기를 가져 오는 방법 (AFAIK)이 없습니다. 그러나 적어도 지금까지 모든 iPhone 버전에서 일정합니다.
도구 모음 위치를보기 하단에서 오프셋으로 계산하고보기 크기를 고려하면 탐색 모음이 있는지 여부에 대해 걱정할 필요가 없습니다.
예
#define KEYBOARD_HEIGHT 240 // example - can't remember the exact size
#define TOOLBAR_HEIGHT 30
toolBarRect.origin.y = viewRect.size.height - KEYBOARD_HEIGHT - TOOLBAR_HEIGHT;
// move toolbar either directly or with an animation
정의 대신 쉽게 만들 수 있습니다. keyboardHeight 키보드가 표시되는지 여부에 따라 크기를 반환 함수를 만들고이 도구 모음 위치를 레이아웃을 재구성하는 별도의 함수로 이동할 수 있습니다.
또한 navbar 설정에 따라 뷰의 크기가로드되고 표시 될 때 변경 될 수 있으므로이 위치 지정을 수행하는 위치에 따라 달라질 수 있습니다. 나는 그것을하기에 가장 좋은 장소는 viewWillAppear 일 것이라고 믿습니다.
