-1

(上から見た図) と(ViewController下から見た図) があります。ユーザーが編集を開始したときに、ビューの一番上に移動したい。ユーザーがすべてから編集を開始するときは問題ありませんが、ユーザーが最初に編集したいときに(キーボードを非表示にせずに)機能しません。は隠されていますが、フレームを変更しないでください。使用しようとしましたが、キーボードがポップアップしたときにのみ呼び出されます。UITextFieldUITextViewUITextViewUITextViewUITextFieldUITextViewUITextFieldUITextViewUIKeyboardDidShowNotification

問題を再現するコード: ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UITextField *titleTF;
@property (weak, nonatomic) IBOutlet UITextView *bodyTV;
@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()
@end

@implementation ViewController
-(void)textViewDidBeginEditing:(UITextView *)textView {
    self.titleTF.hidden=YES;
    CGRect newFrame=CGRectMake(20, 20, textView.frame.size.width, 100);
    textView.frame=newFrame;
}
@end

アプリを実行して UITextView をクリックすると、アプリは次のよう になります

アプリを再起動します。最初に UITextField をクリックし、次に UITextView をクリックします。アプリは次のようになります: http://imageshack.us/photo/my-images/843/66433184.png UITextField は非表示ですが、UITextView はフレームを変更しませんでした。

4

1 に答える 1

1

TextView の編集が始まるとビューを上に移動し、編集が終了するとビューを下に移動します。

この2つの方法を入れるだけで、

-(void) textViewDidBeginEditing:(UITextView *)textView{
    CGRect frame = self.view.frame;
    frame.origin.y = -(textView.frame.origin.y + textView.frame.size.height);
    [UIView animateWithDuration:0.3f animations:^{
        self.view.frame = frame;
    }];
}

-(void) textViewDidEndEditing:(UITextView *)textView{
    CGRect frame = self.view.frame;
    frame.origin.y = 0;
    [UIView animateWithDuration:0.3f animations:^{
        self.view.frame = frame;
    }];
}
于 2013-05-05T12:30:36.293 に答える