ios文本框随键盘提升
#import <UIKit/UIKit.h>@interface ViewController : UIViewController<UITextFieldDelegate>{UIScrollView * _scrollView; IBOutlet UITextField *message; IBOutlet UIScrollView *myScrollView;}@property (retain, nonatomic) IBOutlet UITextField *message;@property (retain, nonatomic) IBOutlet UIScrollView *myScrollView;@end
//// ViewController.m// ChatRoom007//// Created by Bo Xiu on 12-9-14.// Copyright (c) 2012年 Bo Xiu. All rights reserved.//#import "ViewController.h"@interface ViewController ()@end@implementation ViewController@synthesize myScrollView;@synthesize message;- (void)viewDidLoad{ [super viewDidLoad]; UIView *myView = [self bubbleView:@"hello" from:NO]; [self.view addSubview:myView]; message.delegate = self;}//该方法为点击输入文本框要开始输入是调用的代理方法:就是把view上移到能看见文本框的地方- (void)textFieldDidBeginEditing:(UITextField *)textField{ CGFloat keyboardHeight = 216.0f; if (self.view.frame.size.height - keyboardHeight <= textField.frame.origin.y + textField.frame.size.height) { CGFloat y = textField.frame.origin.y - (self.view.frame.size.height - keyboardHeight - textField.frame.size.height - 5); [UIView beginAnimations:@"srcollView" context:nil]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; [UIView setAnimationDuration:0.275f]; self.view.frame = CGRectMake(self.view.frame.origin.x, -y, self.view.frame.size.width, self.view.frame.size.height); [UIView commitAnimations]; }}//该方法为点击虚拟键盘Return,要调用的代理方法:隐藏虚拟键盘- (BOOL)textFieldShouldReturn:(UITextField *)textField{ [textField resignFirstResponder]; return YES;}//该方法为完成输入后要调用的代理方法:虚拟键盘隐藏后,要恢复到之前的文本框地方- (void)textFieldDidEndEditing:(UITextField *)textField{ [UIView beginAnimations:@"srcollView" context:nil]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; [UIView setAnimationDuration:0.275f]; self.view.frame = CGRectMake(self.view.frame.origin.x, 0, self.view.frame.size.width, self.view.frame.size.height); [UIView commitAnimations];}#pragma mark Table view methods- (UIView *)bubbleView:(NSString *)text from:(BOOL)fromSelf {// build single chat bubble cell with given textUIView *returnView = [[UIView alloc] initWithFrame:CGRectZero];returnView.backgroundColor = [UIColor clearColor]; UIImage *bubble = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:fromSelf?@"bubbleSelf":@"bubble" ofType:@"png"]];UIImageView *bubbleImageView = [[UIImageView alloc] initWithImage:[bubble stretchableImageWithLeftCapWidth:21 topCapHeight:14]]; UIFont *font = [UIFont systemFontOfSize:12];CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(150.0f, 1000.0f) lineBreakMode:UILineBreakModeCharacterWrap]; UILabel *bubbleText = [[UILabel alloc] initWithFrame:CGRectMake(21.0f, 14.0f, size.width+10, size.height+10)];bubbleText.backgroundColor = [UIColor clearColor];bubbleText.font = font;bubbleText.numberOfLines = 0;bubbleText.lineBreakMode = UILineBreakModeCharacterWrap;bubbleText.text = text;bubbleImageView.frame = CGRectMake(0.0f, 0.0f, 200.0f, size.height+40.0f);if(fromSelf)returnView.frame = CGRectMake(120.0f, 10.0f, 200.0f, size.height+50.0f);elsereturnView.frame = CGRectMake(0.0f, 10.0f, 200.0f, size.height+50.0f);[returnView addSubview:bubbleImageView];[bubbleImageView release];[returnView addSubview:bubbleText];[bubbleText release]; return [returnView autorelease];}- (void)viewDidUnload{ [message release]; message = nil; [myScrollView release]; myScrollView = nil; [super viewDidUnload]; // Release any retained subviews of the main view.}- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{ if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } else { return YES; }}- (void)dealloc { [message release]; [myScrollView release]; [super dealloc];}@end