3 回答

TA貢獻1860條經(jīng)驗 獲得超9個贊
從iOS 3.2開始,有一種新方法可以實現(xiàn)此效果:
UITextFields
并UITextViews
具有一個inputAccessoryView
屬性,您可以將其設(shè)置為任何視圖,該屬性會自動顯示在上方并使用鍵盤進行動畫處理。
請注意,您使用的視圖既不應(yīng)位于其他視圖層次中,也不應(yīng)將其添加到某些超級視圖中,這是為您完成的。

TA貢獻1851條經(jīng)驗 獲得超3個贊
所以基本上:
在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];
然后使用上面提到的方法來調(diào)整條的位置:
-(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];

TA貢獻1836條經(jīng)驗 獲得超4個贊
這基于tonklon的現(xiàn)有答案 -我只是添加一個代碼段,該代碼段在鍵盤頂部顯示一個半透明的黑色工具欄,并在右側(cè)顯示一個“完成”按鈕:
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];
}
希望能對某人有所幫助。
- 3 回答
- 0 關(guān)注
- 689 瀏覽
添加回答
舉報