How to re-size UITextView when keyboard shown with iOS 7

后端 未结 8 1409
耶瑟儿~
耶瑟儿~ 2020-12-05 03:34

I have a view controller which contains a full-screen UITextView. When the keyboard is shown I would like to resize the text view so that it is not hidden under

8条回答
  •  攒了一身酷
    2020-12-05 04:16

    Following on is working for me :

    .h file

    @interface ViewController : UIViewController  {
    
        UITextView *textView ;
    
    }
    
    @property(nonatomic,strong)IBOutlet UITextView *textView;
    
    @end
    

    .m file

    @implementation ViewController
    @synthesize textView;
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    
        CGRect textViewFrame = CGRectMake(20.0f, 20.0f, 280.0f, 424.0f);
        //UITextView *textView = [[UITextView alloc] initWithFrame:textViewFrame];
        textView.frame = textViewFrame;
        textView.delegate = self;
        textView.returnKeyType = UIReturnKeyDone;
        textView.backgroundColor = [UIColor greenColor];
        textView.textColor = [UIColor blackColor];
        [self.view addSubview:textView];
    
    }
    - (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
        NSLog(@"textViewShouldBeginEditing:");
        return YES;
    }
    - (void)textViewDidBeginEditing:(UITextView *)textView1 {
        NSLog(@"textViewDidBeginEditing:");
       CGRect textViewFrame = CGRectMake(20.0f, 20.0f, 280.0f, 224.0f);
    
        textView1.frame = textViewFrame;
    
    }
    - (BOOL)textViewShouldEndEditing:(UITextView *)textView{
        NSLog(@"textViewShouldEndEditing:");
           return YES;
    }
    - (void)textViewDidEndEditing:(UITextView *)textView{
        NSLog(@"textViewDidEndEditing:");
    }
    - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
           return YES;
    }
    
    - (void)textViewDidChange:(UITextView *)textView{
        NSLog(@"textViewDidChange:");
    }
    
    - (void)textViewDidChangeSelection:(UITextView *)textView{
        NSLog(@"textViewDidChangeSelection:");
    }
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
        NSLog(@"touchesBegan:withEvent:");
        CGRect textViewFrame = CGRectMake(20.0f, 20.0f, 280.0f, 424.0f);
    
        textView.frame = textViewFrame;
        [self.view endEditing:YES];
        [super touchesBegan:touches withEvent:event];
    }
    @end
    

提交回复
热议问题