2013-05-05 63 views
-1

我有ViewControllerUITextField(俯視圖)和UITextView(仰視圖)。我想在用戶開始編輯時將UITextView移動到視圖的頂部。 當用戶從UITextView開始編輯時,一切都很好,但是當用戶首先要編輯UITextField,然後UITextView(沒有隱藏鍵盤)時,它不起作用。 UITextField已隱藏,但UITextView不會更改其框架。 我試圖使用UIKeyboardDidShowNotification,但只有在彈出鍵盤時才調用它。更改UITextView框架

碼來重現問題: 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的,應用程序將是這樣的: http://imageshack.us/photo/my-images/694/32568213.png 現在一切都很好(UITextField被隱藏,UITextView被移動並調整大小)。

再次啓動應用程序。首先點擊UITextField,然後點擊UITextView。應用看起來像這樣: http://imageshack.us/photo/my-images/843/66433184.png UITextField是隱藏的,但UITextView沒有改變他的框架。

+0

請問這個方法火,當你開始編輯?你試過NSLog嗎? – 2013-05-05 12:01:42

+0

是的,它的工作。它總是隱藏titleTF。 – 2013-05-05 12:03:16

+0

你在這個視圖控制器中只有一個textView嗎? – 2013-05-05 12:19:19

回答

1

當TextView編輯開始時向上移動視圖, 編輯結束時向下移動視圖。

只要把上述兩種方法,

-(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; 
    }]; 
} 
+0

工作正確嗎? – 2013-05-05 12:36:37

+0

不是我想要的。它會移動根父級,但是我的UITextView不是完全可見的,因爲它大於屏幕高度 - 鍵盤高度。我想調整UITextView的大小以填充屏幕高度(鍵盤左側)。 – 2013-05-05 12:48:52