开发者

UITextView doesn't not resize when keyboard appear if loaded from a tab bar cotroller

开发者 https://www.devze.com 2023-02-04 17:35 出处:网络
I have a simple view controller (SecondViewController) used to manage a UITextview (I\'m building a simple editor)

I have a simple view controller (SecondViewController) used to manage a UITextview (I'm building a simple editor) this is the code of the SecondViewController.h


    @interface SecondViewController : UIViewController {
 IBOutlet UITextView *textView;
}
@property (nonatomic,retain) IBOutlet UITextView *textView;
@end
and this is the SecondViewController.m

// // EditorViewController.m // Editor // // Created by elio d'antoni on 13/01/11. // Copyright 2011 none. All rights reserved. // @implementation SecondViewController @synthesize textView;

/* // Implement loadView to create a view hierarchy programmatically, without using a nib. - (void)loadView { } */

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"uiViewBg.png"]]; textView.layer.borderWidth=1; textView.layer.cornerRadius=5; textView.layer.borderColor=[[UIColor darkGrayColor] CGColor]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillAppear:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillDisappear:) name:UIKeyboardWillHideNotification object:nil];

}

-(void) matchAnimationTo:(NSDictionary *) userInfo { NSLog(@"match animation method"); [UIView setAnimationDuration:[[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]]; [UIView setAnimationCurve:[[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]]; }

-(CGFloat) keyboardEndingFrameHeight:(NSDictionary *) userInfo { NSLog(@"keyboardEndingFrameHeight method");

CGRect keyboardEndingUncorrectedFrame = 

[[ userInfo objectForKey:UIKeyboardFrameEndUserInfoKey ] CGRectValue]; CGRect keyboardEndingFrame = [self.view convertRect:keyboardEndingUncorrectedFrame fromView:nil]; return keyboardEndingFrame.size.height; }

-(CGRect) adjustFrameHeightBy:(CGFloat) change multipliedBy:(NSInteger) direction { NSLog(@"adjust method");

return CGRectMake(20, 
                  57, 
                  self.textView.frame.size.width, 
                  self.textView.frame.size.height + change * direction);

} -(void)keyboardWillAppear:(NSNotification *)notification { NSLog(@"keyboard appear"); [UIView beginAnimations:nil context:NULL]; [self matchAnimationTo:[notification userInfo]]; self.textView.frame = [self adjustFrameHeightBy:[self keyboardEndingFrameHeight: [notification userInfo]] multipliedBy:-1]; [UIView commitAnimations]; } -(void)keyboardWillDisappear:(NSNotification *) notification { NSLog(@"keyboard disappear"); [UIView beginAnimations:nil context:NULL]; [self matchAnimationTo:[notification userInfo]]; self.textView.frame = [self adjustFrameHeightBy:[self 开发者_如何学JAVAkeyboardEndingFrameHeight: [notification userInfo]] multipliedBy:1]; [UIView commitAnimations]; }

// Override to allow orientations other than the default portrait orientation. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return YES; }

  • (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. }

  • (void)viewDidUnload { // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; }

  • (void)dealloc { [super dealloc]; }

@end

the problem is that if load the view controller from a tab bar controller the textView doesn't resize when the keyboard appear, but the SAME code works if loaded as a single view based app. I hope I was clear enough. I used the tabBar template provided by xcode no modifications.


Your code looks right, did notice you are not releasing "textView" in dealloc and "viewDidUnload" but this should make any difference.

I'd be checking the notications are received and everthing is wired up ie textView is not nil

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号