2012-02-17 27 views
5

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

3

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; 
} 
0

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:

http://www.idev101.com/code/User_Interface/sizes.html

+2

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ỵ)”. –

3

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.

2

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];  
} 
+0

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

+0

Cảm ơn bạn đã tip –

+0

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

0

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]; 
} 
Các vấn đề liên quan