Tôi muốn tạo chế độ xem, như hộp thoại chia sẻ của ứng dụng Facebook hoặc Twitter, ví dụ: chỉ có UITextView và bàn phím vĩnh viễn. Tôi có thể xem cách bắt đầu bằng bàn phím có thể nhìn thấy từ this answer, nhưng tôi không chắc chắn cách kích thước UITextView để vừa với chính xác trên bàn phím. Nếu không, văn bản có thể bị ẩn dưới bàn phím khó xử.Làm cách nào để tạo UITextView khớp chính xác trên bàn phím?
Trả lời
Tôi tìm thấy một mẫu mã hữu ích trong tài liệu của Apple cho việc này: https://developer.apple.com/library/ios/#samplecode/KeyboardAccessory/Introduction/Intro.html
Dưới đây là đoạn code tôi đã kết thúc với. Tôi không lo lắng về việc ẩn bàn phím, vì theo quan điểm của tôi, bàn phím không bao giờ nên ẩn.
- (void)viewWillAppear:(BOOL)flag
{
[super viewWillAppear:flag];
// Listen for the keyboard to show up so we can get its height
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
// Set focus on the text view to display the keyboard immediately
[self.textView becomeFirstResponder];
}
- (void)keyboardWillShow:(NSNotification *)notification
{
/*
Reduce the size of the text view so that it's not obscured by the keyboard.
*/
NSDictionary *userInfo = [notification userInfo];
// Get the origin of the keyboard when it's displayed.
NSValue* keyboardFrame = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
// Get the top of the keyboard as the y coordinate of its origin in self's view's
// coordinate system. The bottom of the text view's frame should align with the
// top of the keyboard's final position.
CGRect keyboardRect = [keyboardFrame CGRectValue];
keyboardRect = [self.view convertRect:keyboardRect fromView:nil];
// Set the text view's frame height as the distance from the top of the view bounds
// to the top of the keyboard
CGFloat keyboardTop = keyboardRect.origin.y;
CGRect newTextViewFrame = self.view.bounds;
newTextViewFrame.size.height = keyboardTop - self.view.bounds.origin.y;
self.textView.frame = newTextViewFrame;
}
Tôi tìm thấy trang này luôn khá hữu ích - nó cho thấy tất cả các kích thước quan trọng, vì vậy nó rất dễ dàng để tính toán kích thước bạn cần:
Bạn có thể đăng ký để UIKeyboardWillShowNotification
. Thông báo bao gồm khung bàn phím, vì vậy bạn có thể chỉnh kích thước của mình để vừa với không gian còn lại.
Bạn có thể lấy kích thước bàn phím như vậy:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardIsUp:) name:UIKeyboardDidShowNotification object:nil];
- (void)keyboardIsUp:(NSNotification *)notification{
CGSize keyboardSize = [self.view convertRect:[[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue] toView:nil].size;
NSLog(@"%f", keyboardSize.height);
}
[EDIT] Chế độ phong cảnh
Tôi đã thử nó trên IPAS Simulator và nó trở lại 264 ở chế độ dọc cho một bàn phím QWERTY nhưng khi bạn khởi động ứng dụng hoặc xoay sang chế độ ngang, nó trả về 1024. Vì vậy, bạn có thể cần phải hỏi chiều rộng thay vì chiều cao ở chế độ ngang ...
[EDIT]
Nhờ comment cướp mayoff của không có vấn đề với chế độ phong cảnh nữa
[EDIT]
Đây không phải là cách tốt nhất để làm việc đó, nhưng điều đó đưa ra một ý tưởng. Tôi sẽ xem xét lại nó sau này
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
CGSize size = [self.view convertRect:self.view.frame toView:nil].size;
CGFloat width = size.width;
CGFloat height = 40;
CGFloat x = 0;
CGFloat y = size.height+40;
aboveKBView = [[[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)] autorelease];
[aboveKBView setBackgroundColor:[UIColor greenColor]];
[self.view addSubview:aboveKBView];
}
- (void)keyboardWillHide:(NSNotification *)notification{
[aboveKBView setHidden:YES];
}
- (void)keyboardWillShow:(NSNotification *)notification{
NSLog(@"keyboardIsUp");
CGSize keyboardSize = [self.view convertRect:[[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue] toView:nil].size;
CGSize size = [self.view convertRect:self.view.frame toView:nil].size;
CGFloat width = size.width;
CGFloat height = 40;
CGFloat x = 0;
CGFloat y = size.height-(keyboardSize.height+height);
[aboveKBView setFrame:CGRectMake(x, y, width, height)];
[aboveKBView setHidden:NO];
}
Khung trong thông báo nằm trong tọa độ chung. Bạn cần phải chuyển đổi nó sang hệ tọa độ thích hợp bằng cách sử dụng một cái gì đó như '[myView convertRect: keyboardFrame fromView: nil]'. –
Cảm ơn bạn đã tip –
Điều đó làm việc để nhận được kích thước, nhưng tôi vẫn gặp sự cố khi đặt UITextView ở đúng độ cao. Trong đoạn code bên dưới frame.size.heigh là 367 và keyboardHeight là 216, nhưng vì một số lý do khi tôi gọi setFrame có khoảng cách giữa chế độ xem văn bản và bàn phím. FYI, tôi đang thiết lập người quan sát và gọi '[self.textView trở thànhFirstResponder]' trong 'viewWillAppear'. –
Hãy thử điều này
trong viewDidLoad:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
và thêm các phương pháp này.
- (void) keyboardWillShow: (NSNotification*) aNotification;
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
CGRect rect = [[self view] frame];
rect.origin.y -= 60;
[[self view] setFrame: rect];
[UIView commitAnimations];
}
- (void) keyboardWillHide: (NSNotification*) aNotification;
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
CGRect rect = [[self view] frame];
rect.origin.y += 60;
[[self view] setFrame: rect];
[UIView commitAnimations];
}
- 1. Làm thế nào để ẩn bàn phím - của - UITextView iPhone - bằng cách trả lại chính
- 2. Nhận nhân vật chính xác trên sự kiện bàn phím
- 3. Làm thế nào để vô hiệu hóa bàn phím trên một UITextView?
- 4. Cách tạo la bàn chính xác trên android
- 5. Làm cách nào để hiển thị bàn phím theo mặc định trong UITextView?
- 6. Làm thế nào để có được UITextView di chuyển đúng cách khi bàn phím hiển thị
- 7. Làm cách nào để tôi có thể bật hoặc tắt phím Quay lại theo cách thủ công trên bàn phím?
- 8. Thay đổi phím trả về thành phím đã hoàn tất trên Bàn phím iPhone (UITextView)
- 9. Cách di chuyển UITextView khi bàn phím hiện diện
- 10. Làm cách nào để loại bỏ bàn phím trên UIWebView?
- 11. Làm cách nào để Regex chỉ khớp với các kết quả khớp chính xác?
- 12. UIModalPresentationFormSheet trên iPad. Cách điều chỉnh chiều cao UITextView khi bàn phím xuất hiện
- 13. Làm thế nào để vô hiệu hóa tự động hoàn thành trong bàn phím iPhone UITextView
- 14. Cách tạo bàn phím ảo trên màn hình cho Android
- 15. Cách tạo bàn phím tùy chỉnh
- 16. Phím đã hoàn tất trong ứng dụng iPhone đóng bàn phím (với UITextView)
- 17. Làm thế nào để chơi âm thanh bấm bàn phím trong bàn phím tùy chỉnh?
- 18. Làm cách nào để phát hiện bàn phím libgdx
- 19. Android - Nhận bấm phím trên bàn phím
- 20. Làm cách nào để tạo chuỗi để khớp với regex?
- 21. ThreeJS: Tạo hình nền khớp chính xác với cửa sổ?
- 22. Làm thế nào để tạo ra chìa khóa riêng trên bàn phím mềm
- 23. cách thay đổi nền chính của bất kỳ phím nào trong bàn phím ảo Android
- 24. Làm cách nào để buộc bàn phím hiển thị/ẩn?
- 25. Làm cách nào để ghi đè ngắt bàn phím? (Python)
- 26. Làm cách nào để chặn báo chí dài trên UITextView?
- 27. Làm thế nào để tôi có được bàn tay trên bàn phím Dvorak?
- 28. Các biểu tượng phím trên bàn phím
- 29. Làm thế nào để thay đổi màu nền của bàn phím cho bàn phím mềm Android?
- 30. Cách viết bàn phím ảo trên màn hình
Không phải tất cả bàn phím đều có cùng kích thước. Ví dụ: “Tiếng Trung - Giản thể (đột quỵ)”. –