lucgian841
lucgian841

Reputation: 2002

Add UITextField above keyboard iOS

In my app I'm trying to add a UITextField on a accessoryView. It should work as follow:

  1. I press on a UITextField in the user interface of app
  2. The keyboard pop up and cover my UITextField on the user interface
  3. To edit text I wanted to show a UITextField in an accessoryView just above the keyboard

I looked on the web but I found only stuff to add button on an accessoryView, I hope you can help me to find a solution to add an UITextField on an accessoryView.

Thank you

UPDATE

This is what I mean:

enter image description here

I hope you understand better my question, I'm not looking for a solution that should user scroll view or something, I don't want to change the interface of my app...

Upvotes: 5

Views: 4777

Answers (4)

nswamy
nswamy

Reputation: 1051

This is what i have done in my apps

  1. Add observers to UIKeyboardWillHideNotification & UIKeyboardWillShowNotification

  2. When u review notification resize your view keyboardFrame

    double duration = [[notification userInfo][UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    int curve = [[notification userInfo][UIKeyboardAnimationCurveUserInfoKey] intValue];
    
    NSValue *value = [notification userInfo][UIKeyboardFrameEndUserInfoKey];
    CGRect rawFrame      = [value CGRectValue];
    CGRect keyboardFrame = [self.view convertRect:rawFrame fromView:nil];
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:duration];
    [UIView setAnimationCurve:curve];
    

    // Resize your View

    [UIView commitAnimations]

Upvotes: 0

Prabhat
Prabhat

Reputation: 96

in .h

UIView *customtoolbar;

in .m At viewdidload add thi code

customtoolbar=[[UIView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height+50, 320, 50)];`

after this add this methods

 -(BOOL)textFieldShouldReturn:(UITextField *)textField{

    [self pressdone];

    return YES;

}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{

        //216 is keyboard default height in portrait(162 in landscape)
    [UIView animateWithDuration:0.7 animations:^{
    [self addtoolbar:CGRectMake(0, self.view.frame.size.height-216-50, 320, 50)];

    }];
    return YES;
}
-(UIView *)addtoolbar:(CGRect )frame{

    customtoolbar.frame=frame;
    customtoolbar.backgroundColor=[UIColor darkGrayColor];

    //give new frame to your textfield
    txtfld.frame=CGRectMake(5,10, 220, 30);
    [customtoolbar addSubview:txtfld];

    UIButton *done=[UIButton buttonWithType:UIButtonTypeCustom];
    done.frame=CGRectMake(235,10, 60, 30);
    [done setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [done setTitle:@"Done" forState:UIControlStateNormal];
    [done addTarget:self  action:@selector(pressdone) forControlEvents:UIControlEventTouchUpInside];
    [customtoolbar addSubview:done];
    [self.view addSubview:customtoolbar];

    return customtoolbar;

}
-(void)pressdone{

    [self addtoolbar:CGRectMake(0, self.view.frame.size.height+50, 320, 50)];

    //set there orignal frame of your textfield
    txtfld.frame=CGRectMake(95, 170, 123, 37);
    [self.view addSubview:txtfld];
    [txtfld resignFirstResponder];
}

Upvotes: 1

Jay
Jay

Reputation: 864

Add a view and then on top of it, please put a textfield and try. Below is the code.

UIView * container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
[container setBackgroundColor:[UIColor redColor]];
UITextField * newTextfield = [[UITextField alloc] initWithFrame:CGRectMake(50, 0, 100, 44)];
[newTextfield setBackgroundColor:[UIColor yellowColor]];
[container addSubview:newTextfield];


self.textField.inputAccessoryView = container;

This contains some colors, just to distinguish and identify the views. Change and format according to your needs. It will work. :)

Upvotes: 1

Prabhat
Prabhat

Reputation: 96

you can use scroll view for this in your app & set content offset

- (void)textFieldDidBeginEditing:(UITextField *)textField {

self.scroll.contentOffset = CGPointMake(0, textField.frame.origin.y);

}

i hope this is relevent to your problem

Upvotes: 0

Related Questions