现在的位置: 首页 > 综合 > 正文

ios5 中文键盘高度变高覆盖现有ui问题的解决方案(获取键盘高度的方法)

2019年10月02日 ⁄ 综合 ⁄ 共 5040字 ⁄ 字号 评论关闭

ios5之前,iphone上的键盘的高度是固定为216.0px高的,中文汉字的选择框是悬浮的,所以不少应用都将此高度来标注键盘的高度.
  可是在ios5中,键盘布局变了,尤其是中文输入时,中文汉字选择框就固定在键盘上方,这样就使得原本与键盘紧密贴合的界面视图被中文汉字选择框给覆盖住了。一方面影响了界面的美观,另一方面,如果被覆盖的部分就是文本输入框的话,用户就无法看到输入的内容了。因此这个问题就必须得解决了。
解决方法:

  其实在一开始使用216.0px这个固定值来标注键盘的高度就是错误的。因为在ios3.2以后的系统中,苹果就提供了键盘使用的api以及demo程序——“KeyboardAccessory”。

另外在UICatalog中也有关于虚拟键盘覆盖UI的问题的解决方案。

  处理键盘事件的正确方法是这样的:(包括获取键盘的高度以及键盘弹出和消失动画的时间)

下面是关于处理虚拟键盘覆盖UITextView的解决方法:

#import "TextFieldViewController.h"

@implementation TextFieldViewController
@synthesize textView;

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [textView release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle


// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
    UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 20, 320, 460)] autorelease];
    self.view = view;
    [view release];
    
    self.title = @"TextView";
}

- (void)setUpTextView
{
    UITextView *aTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
    self.textView = aTextView;
    [aTextView release];
    
    textView.editable = YES;
    textView.multipleTouchEnabled = YES;
    textView.showsHorizontalScrollIndicator = NO;
    textView.backgroundColor = [UIColor purpleColor];
    textView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    textView.scrollsToTop = NO;
    textView.delegate = self;
    
    [self.view addSubview:textView];
    [textView release];
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [self setUpTextView];
    
    [super viewDidLoad];
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.

    self.textView = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
#ifdef __IPHONE_5_0
    float version = [[[UIDevice currentDevice] systemVersion] floatValue];
    if (version >= 5.0) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillChangeFrameNotification object:nil];
    }
#endif
}

- (void)viewWillDisappear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
#ifdef __IPHONE_5_0
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];
#endif
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark -
#pragma mark Responding to keyboard events

- (void)resizeTextViewWithKeyboardHeight:(CGFloat)keyboardHeight withDuration:(NSTimeInterval)duration
{
    CGRect newTextViewFrame = self.view.frame;
    newTextViewFrame.size.height = newTextViewFrame.size.height - keyboardHeight;
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:duration];
    textView.frame = newTextViewFrame;
    [UIView commitAnimations];
    
}

- (void)keyboardWillShow:(NSNotification *)notification
{
    /*
     Reduce the size of the text view so that it's not obscured by the keyboard.
     Animate the resize so that it's in sync with the appearance of the keyboard.
     */
    
    NSDictionary *userInfo = [notification userInfo];
    
    //Get the height of the keyboard
    CGRect keyboardRect = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
        
    //Get the duration of the animation
    NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSTimeInterval animationDuration;
    [animationDurationValue getValue:&animationDuration];
    
    //Animate the resize of the text view's frame in sync with the keyboard's appearance.
    [self resizeTextViewWithKeyboardHeight:keyboardRect.size.height withDuration:animationDuration];
}

- (void)keyboardWillHide:(NSNotification *)notification
{
    NSDictionary *userInfo = [notification userInfo];
    
    CGRect keyboardRect = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
    NSValue *aniamtionDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSTimeInterval animationDuration;
    [aniamtionDurationValue getValue:&animationDuration];
    
    /*
     Restore the size of the text view (fill self's view).
     Animate the resize so that it's in sync with the disappearance of the keyboard.
     */
    [self resizeTextViewWithKeyboardHeight:-keyboardRect.size.height withDuration:animationDuration];
}

#pragma mark -

- (void)saveAction:(id)sender
{
    [self.textView resignFirstResponder];
    self.navigationItem.rightBarButtonItem = nil;
}

#pragma mark -
#pragma mark TextView delegate Methods

- (void)textViewDidBeginEditing:(UITextView *)textView
{
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(saveAction:)];
    self.navigationItem.rightBarButtonItem = item;
    [item release];
}

- (void)textViewDidEndEditing:(UITextView *)aTextView
{
    [aTextView resignFirstResponder];
}

抱歉!评论已关闭.