iPhone 키보드 위에 도구 모음을 프로그래밍 방식으로 정렬
예를 들어 양식 요소를 탐색할 때 iPhone Safari에서와 같이 iPhone 키보드의 맨 위에 도구 모음을 추가하려는 경우가 있습니다.
현재 저는 툴바의 직사각형을 상수로 지정하고 있지만, 인터페이스의 다른 요소(화면 상단의 툴바 및 탐색 막대)가 유동적이기 때문에 사소한 인터페이스 변경을 할 때마다 툴바가 정렬되지 않습니다.
현재 보기와 관련하여 키보드의 위치를 프로그래밍 방식으로 결정하는 방법이 있습니까?
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등, 당신이 받는 통지는 키보드의 경계를 포함할 것입니다.userInfo받아쓰기(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).그러나 적어도 지금까지 모든 아이폰 버전에서 일정합니다.
도구 모음 위치를 뷰 하단에서 간격띄우기로 계산하고 뷰 크기를 고려하는 경우에는 탐색 모음이 있는지 여부를 걱정할 필요가 없습니다.
예.
#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키보드가 표시되는지 여부에 따라 크기를 반환하는 함수이며, 이 도구 모음 위치를 레이아웃을 재구성하는 별도의 함수로 이동합니다.
또한 이 위치 설정을 수행하는 위치에 따라 보기 크기가 로드되는 것과 표시되는 것 사이에서 변경될 수 있습니다.Will Appear 보기가 가장 좋을 것 같습니다.
언급URL : https://stackoverflow.com/questions/158574/programmatically-align-a-toolbar-on-top-of-the-iphone-keyboard
'programing' 카테고리의 다른 글
| C# on load 이벤트에서 div 태그 내에 HTML을 동적으로 추가할 수 있습니까? (0) | 2023.08.25 |
|---|---|
| Node.js에서 next()를 사용하고 next()를 반환할 때 (0) | 2023.08.25 |
| jQuery가 있는 파일 업로드 진행률 표시줄 (0) | 2023.08.25 |
| Node.js console.log 대 console.info (0) | 2023.08.25 |
| 자바스크립트에서 TCP 소켓을 통해 통신하려면 어떻게 해야 합니까? (0) | 2023.08.25 |