0

我是 iPhone 的初学者。我有一个UITextView在我看来。当我开始打字时,键盘会弹出并覆盖文本视图。所以,我的问题是:当我开始打字时如何向上滑动文本视图?我将不胜感激任何建议和示例代码。

4

1 回答 1

0

检查下面的代码。在此代码中将您的文本视图添加到滚动视图

#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;
}
于 2012-05-29T12:08:39.110 回答