我是iPhone的初学者。我认为我有一个UITextView
。当我开始输入时,键盘会弹出并覆盖文本视图。所以,我的问题是:当我开始输入时如何滑动文本视图?我很感激任何建议和示例代码。
答案 0 :(得分:0)
检查以下代码。 在此代码中将textview添加到scrollview
#import <UIKit/UIKit.h>
@interface HomeViewController : UIViewController <UIScrollViewDelegate,UITextViewDelegate>{
IBOutlet UIScrollView *scrollView;
IBOutlet UITextView *txtView;
}
- (void)scrollViewToCenterOfScreen:(UIView *)theView ;
@end
#import "HomeViewController.h"
@implementation HomeViewController
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
[scrollView addSubview:txtView];
}
- (void)scrollViewToCenterOfScreen:(UIView *)theView {
CGFloat viewCenterY = theView.center.y;
CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
CGFloat availableHeight = applicationFrame.size.height - 200;
// Remove area covered by keyboard
CGFloat z = viewCenterY - availableHeight / 2.0;
if (z < 0) {
z = 0;
}
[scrollView setContentOffset:CGPointMake(0, z) animated:YES];
}
#pragma mark For Making UITextView delegate methods-
-(void)textViewDidBeginEditing:(UITextView *)textView{
[self scrollViewToCenterOfScreen:textView];
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
if (range.length==0) {
if ([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
[scrollView1 setContentOffset:CGPointMake(0, 0) animated:YES];
return NO;
}
}
return YES;
}