如果从选项卡栏控制器加载,当键盘出现时,UITextView 不会调整大小

发布于 2024-10-12 08:01:05 字数 3429 浏览 2 评论 0原文

我有一个简单的视图控制器(SecondViewController)用于管理 UITextview (我正在构建一个简单的编辑器) 这是 SecondViewController.h 的代码


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

// // EditorViewController.m // 编辑器 // // 由 Elio d'antoni 于 2011 年 1 月 13 日创建。 // 版权所有 2011 无。版权所有。 // @实现SecondViewController @合成textView;

/* // 实现 loadView 以编程方式创建视图层次结构,而不使用 nib。 - (void)loadView { } */

// 实现 viewDidLoad 以在加载视图后执行其他设置(通常是从笔尖加载)。 - (void)viewDidLoad { [超级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(keyboardWillAppear:) 名称:UIKeyboardWillShowNotification 对象:nil]; [[NSNotificationCenter defaultCenter] addObserver:self 选择器:@selector(keyboardWillDisappear:) 名称:UIKeyboardWillHideNotification 对象:nil];

}

-(void) matchAnimationTo:(NSDictionary *) userInfo { NSLog(@"匹配动画方法"); [UIView setAnimationDuration:[[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]]; [UIView setAnimationCurve:[[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]]; }

-(CGFloat) KeyboardEndingFrameHeight:(NSDictionary *) userInfo { NSLog(@"keyboardEndingFrameHeight 方法");

CGRect keyboardEndingUncorrectedFrame = 

[[ userInfo objectForKey:UIKeyboardFrameEndUserInfoKey ] CG矩形值]; CGRect 键盘结束帧 = [self.view ConvertRect:keyboardEndingUn CorrectedFrame fromView:nil]; 返回keyboardEndingFrame.size.height; }

-(CGRect) adjustmentFrameHeightBy:(CGFloat) 改变 乘法:(NSInteger) 方向 { NSLog(@"调整方法");

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

} -(void)keyboardWillAppear:(NSNotification *)通知{ NSLog(@"键盘出现"); [UIView beginAnimations:nil 上下文:NULL]; [self matchAnimationTo:[通知用户信息]]; self.textView.frame = [自我调整框架高度:[自我键盘结束框架高度: [通知用户信息]] 乘以:-1]; [UIView提交动画]; } -(void)keyboardWillDisappear:(NSNotification *) 通知 { NSLog(@"键盘消失"); [UIView beginAnimations:nil 上下文:NULL]; [self matchAnimationTo:[通知用户信息]]; self.textView.frame = [自我调整框架高度:[自我键盘结束框架高度: [通知用户信息]] 乘以:1]; [UIView提交动画]; }

// 重写以允许默认纵向方向以外的方向。 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 返回是; }

  • (void)didReceiveMemoryWarning { // 如果视图没有父视图,则释放该视图。 [超级didReceiveMemoryWarning];

    // 释放所有未使用的缓存数据、图像等。 }

  • (void)viewDidUnload { // 释放主视图中所有保留的子视图。 // 例如 self.myOutlet = nil; }

  • (void)dealloc { [超级释放]; }

@结尾

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.

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 keyboardEndingFrameHeight:
[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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倥絔 2024-10-19 08:01:05

您的代码看起来是正确的,确实注意到您没有在 dealloc 和“viewDidUnload”中释放“textView”,但这应该会产生任何影响。

我会检查是否收到通知以及所有内容是否已连接,即 textView 不为零

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文