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.
'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