Cách di chuyển UITextView Lên và xuống khi bạn bắt đầu nhập một số giá trị. Giống như trong TextField, chúng ta sử dụng phương thức ủy nhiệm của nó. Phải làm gì trong trường hợp UITextView?Cách di chuyển UITextView khi bàn phím hiện diện
Trả lời
Trong khi chỉnh sửa hoàn chỉnh View sẽ di chuyển lên và sau khi chỉnh sửa done sẽ di chuyển xuống ...
- (void)textViewDidBeginEditing:(UITextView *)textView
{
[self animateTextView: YES];
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
[self animateTextView:NO];
}
- (void) animateTextView:(BOOL) up
{
const int movementDistance =heightKeyboard; // tweak as needed
const float movementDuration = 0.3f; // tweak as needed
int movement= movement = (up ? -movementDistance : movementDistance);
NSLog(@"%d",movement);
[UIView beginAnimations: @"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.view.frame = CGRectOffset(self.inputView.frame, 0, movement);
[UIView commitAnimations];
}
Tôi hy vọng điều này sẽ giúp bạn ...
Hum, bạn nên sử dụng khối cho hoạt ảnh, phương pháp của bạn không được khuyến khích vì iOS4 – Beuj
Tôi gặp rất nhiều vấn đề với mã này. giải quyết nó bằng cách thay thế 'self.inputView.frame' bằng 'self.view.frame'. – Rajesh
Khi bạn không thể sử dụng giải pháp với chế độ xem cuộn, đây là cách dễ nhất và tốt nhất –
#define kOFFSET_FOR_KEYBOARD 80.0
-(void)keyboardWillShow {
// Animate the current view out of the way
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
else if (self.view.frame.origin.y < 0)
{
[self setViewMovedUp:NO];
}
}
-(void)keyboardWillHide {
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
else if (self.view.frame.origin.y < 0)
{
[self setViewMovedUp:NO];
}
}
-(void)textFieldDidBeginEditing:(UITextField *)sender
{
if ([sender isEqual:mailTf])
{
//move the main view, so that the keyboard does not hide it.
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
}
}
//method to move the view up/down whenever the keyboard is shown/dismissed
-(void)setViewMovedUp:(BOOL)movedUp
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3]; // if you want to slide up the view
CGRect rect = self.view.frame;
if (movedUp)
{
// 1. move the view's origin up so that the text field that will be hidden come above the keyboard
// 2. increase the size of the view so that the area behind the keyboard is covered up.
rect.origin.y -= kOFFSET_FOR_KEYBOARD;
rect.size.height += kOFFSET_FOR_KEYBOARD;
}
else
{
// revert back to the normal state.
rect.origin.y += kOFFSET_FOR_KEYBOARD;
rect.size.height -= kOFFSET_FOR_KEYBOARD;
}
self.view.frame = rect;
[UIView commitAnimations];
}
- (void)viewWillAppear:(BOOL)animated
{
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
// unregister for keyboard notifications while not visible.
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
thử MÃ này .....
tôi đã làm bằng cách thay đổi khung.
-(void)textViewDidBeginEditing:(UITextView *)textView{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:viewMsg cache:YES];
viewMsg.frame = CGRectMake(10, 50, 300, 200);
[UIView commitAnimations];
NSLog(@"Started editing target!");
}
-(void)textViewDidEndEditing:(UITextView *)textView
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:viewMsg cache:YES];
viewMsg.frame = CGRectMake(10, 150, 300, 200);
[UIView commitAnimations];
}
Đây là mã mẫu sẽ tự động xử lý bàn phím cho bạn. Keyboard avoiding
Nếu bạn đang sử dụng TableView thì bảng của bạn phải là một lớp con của TPKeyboardAvoidingTableView
và nếu bạn đang sử dụng scrollview thì scrollview của bạn phải là một phân lớp của TPKeyboardAvoidingScrollView
. Và thư viện này sẽ tự động xử lý bàn phím cho bạn.
Tôi đề nghị bạn hãy xem this guide
cụ thể tại mục: Di chuyển nội dung nằm dưới bàn phím. Tôi đã sử dụng phương pháp này thành công nhiều lần.
- 1. Làm thế nào để có được UITextView di chuyển đúng cách khi bàn phím hiển thị
- 2. Ngăn UIWebView di chuyển khi bàn phím xuất hiện
- 3. Di chuyển UIScrollView khi bàn phím đi vào vị trí
- 4. UIModalPresentationFormSheet trên iPad. Cách điều chỉnh chiều cao UITextView khi bàn phím xuất hiện
- 5. Giao diện người dùng di chuyển lên trên khi bàn phím mở
- 6. Cách ẩn bàn phím khi nhập phím
- 7. iOS: Tắt hoạt ảnh UITableView khi bàn phím hiển thị
- 8. Tìm hiểu khi con trỏ di chuyển (UITextView)
- 9. Làm cách nào để phát hiện bàn phím libgdx
- 10. di chuyển lên xem văn bản chỉ khi bàn phím ảo mở trên android
- 11. Thay đổi phím trả về thành phím đã hoàn tất trên Bàn phím iPhone (UITextView)
- 12. Cách di chuyển đề xuất sửa UITextView phía trên văn bản
- 13. bố trí của màn hình di chuyển lên khi bàn phím được hiển thị
- 14. Phím đã hoàn tất trong ứng dụng iPhone đóng bàn phím (với UITextView)
- 15. Tắt di chuyển xuống khi nhấn phím cách trên firefox
- 16. Làm thế nào để ẩn bàn phím - của - UITextView iPhone - bằng cách trả lại chính
- 17. ActionBar đang ẩn khi bàn phím xuất hiện
- 18. Mở bàn phím mềm khi nhấn phím
- 19. UITextView bên trong UIScrollView bên trong Popover không hiển thị đầy đủ khi bàn phím được hiển thị
- 20. Làm thế nào để phát hiện sự hiện diện của bàn phím phần cứng?
- 21. Di chuyển la bàn bản đồ google
- 22. Làm cách nào để hiển thị bàn phím theo mặc định trong UITextView?
- 23. Bàn phím UITextField không xuất hiện
- 24. Cách di chuyển điều khiển bằng bàn phím chỉ trong Interface Builder
- 25. Làm cách nào để tạo UITextView khớp chính xác trên bàn phím?
- 26. phát hiện bàn phím iPad Ẩn nút
- 27. kiện Jquery Listbox Thay đổi không cháy trên bàn phím di chuyển
- 28. iOS - UITextView tắt di chuyển nhưng cho phép chạm
- 29. Phát hiện và tác động lên các phím hướng bàn phím trong Java
- 30. Phát hiện xem các phím có sẵn trên bàn phím ảo trên thiết bị di động hay không
'textFieldDidBecomeActive'> di chuyển trường văn bản cho phù hợp. –
http://stackoverflow.com/questions/1126726/how-to-make-a-uitextfield-move-up-when-keyboard-is-present vui lòng xem liên kết này –
Tìm kiếm khó sử dụng? Ngay cả khi nó được, nhìn vào thanh bên: nó cho bạn thấy các câu hỏi liên quan. – Abizern