Tôi có một ứng dụng có chế độ xem có các trường văn bản từ đầu chế độ xem đến cuối chế độ xem. Tôi cần nó để di chuyển khi chỉnh sửa các lĩnh vực dưới cùng để các lĩnh vực sẽ được nhìn thấy, nhưng nó không có vẻ hoạt động chính xác.Bàn phím Di chuyển trên Trường Văn bản Hoạt động - Cuộn xuống Chế độ Ngắm?
Sau Apple docs, tôi đặt tất cả mã đó vào chương trình của mình (Liệt kê 4-1, 4-2) và thêm các cửa hàng scrollView
và activeField
vào tệp tiêu đề của tôi và liên kết chúng với IB.
Sự cố xảy ra ngay khi tôi nhấp vào trường văn bản, TẤT CẢ các trường văn bản sẽ không hiển thị cho đến khi tôi loại bỏ bàn phím. Chúng cuộn xuống rất xa (một lần nữa, đủ xa để không thể nhìn thấy các trường nào).
Có ai biết vấn đề đó có thể gây ra không?
Tôi đang đặt mã ở đây từ Apple Documents để bạn có thể xem chính xác mã tôi đang sử dụng mà không phải nhấp vào.
//my .h
IBOutlet UIScrollView *scrollView;
IBOutlet UITextField *activeField;
//.m
// Call this method somewhere in your view controller setup code.
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
// If active text field is hidden by keyboard, scroll it so it's visible
// Your application might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, activeField.frame.origin)) {
CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y-kbSize.height);
[scrollView setContentOffset:scrollPoint animated:YES];
}
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
activeField = textField;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
activeField = nil;
}
tôi chỉnh sửa copy-paste của mã của Apple ra khỏi bài viết của bạn vì nó là nghi ngờ rằng SO có sự cho phép của Apple để tái tạo nó. Tôi cũng đã gắn cờ cho một người kiểm duyệt để xem xét - Tôi không chắc chắn chính sách chính thức là gì hoặc nên như thế nào. –
@Josh: Lượng nhỏ mã đó có thể là OK, cho mục đích ["Sử dụng hợp pháp",] (http://en.wikipedia.org/wiki/Fair_use). –
@Robert: Ok, cảm ơn. James, tôi đoán điều đó có nghĩa là bạn nên đưa nó trở lại nếu bạn chọn. Xin lỗi vì phiền. –