an0
an0

Reputation: 17530

How to resize view according to keyboard show/hide if keyboard is already shown

In view A, I have a text field. I tap a control to push in a new view controller B. View B only has a text view whose frame should be adjusted to accommodate the keyboard, and I always set the text view to be the first responder and the keyboard should show immediately when view B is pushed in.

I do the usual keyboard notification handling in view controller B. So if the keyboard is not shown before view B is pushed, there is no problem. However, if the text field in view A is the first responder and the keyboard is already shown when I push in view controller B, I won't receive UIKeyboardWillShowNotification/UIKeyboardDidShowNotification. Any suggestion to handle this situation?

Upvotes: 2

Views: 1074

Answers (6)

Walt Sellers
Walt Sellers

Reputation: 3939

You could monitor the keyboard status in some central place in your app, like the appDelegate, and have all the components get the current keyboard information from it.

Upvotes: 0

Jorge Vicente Mendoza
Jorge Vicente Mendoza

Reputation: 736

Hi to resize the view according to the keyboard i use something like this

- (void) animateView: (BOOL) up
{
    CGRect rect = self.view.frame;

    if(up)
    {
        float movement = (up ? 264 : 416);
        rect.size.height = movement;
        self.view.frame = rect;
    }
    else
    {
        rect.size.height = 480;
        self.view.frame = rect;
    }
}

the BOOL variable up is used to indicate if the keyboard is showed or not

hope this helps!

regards, Jorge

Upvotes: 0

Saikat Dey
Saikat Dey

Reputation: 11

You should try UITouch.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
  // Detect touch anywhere
  UITouch *touch = [touches anyObject];

  // Get the point that was touched
  CGPoint point = [touch locationInView:self.view];
  NSLog(@"pointx: %f pointy:%f", point.x, point.y);

  // Check if the point touched is within these rectangular bounds
  if (CGRectContainsPoint(CGRectMake(5, 5, 40, 130), point))
  {
    //do something...
  } 
}

Upvotes: 0

an0
an0

Reputation: 17530

To resign first responder then become first responder again is not a good solution that I dropped immediately the first time I thought about it. Forcing the keyboard to hide and show up again immediately when the keyboard is already there is just not the right thing to do — it is a waste and breaks the smooth transition.

Sending keyboard status from view A to view B by breaking the encapsulation of B is neither an acceptable thing to do in software engineering.

So I believe it deserves a bug report and I did one — #11205521.

Upvotes: 0

anerevol
anerevol

Reputation: 34

You can pass the keyboard status info to viewController B ... This is not a good way, but I've tried some other ways like call [textView resignFirstResponder] in viewWillDisappear of viewController A , all of them're not work.

Upvotes: 0

tangqiaoboy
tangqiaoboy

Reputation: 1476

I haven't met this problem before, but I have a suggestion. Maybe in View B, you can let your textView resignFirstResponder and then becomeFirstResponder to force the keyboard notification comes out. Sample below:

[textView resignFirstResponder];
[textView becomeFirstResponder];

And there is another walk around way, maybe you can resignFirstResponder in View A before pushing, and then becomeFirstResponder after View B is viewDidAppear.

Hope it helps.

Upvotes: 1

Related Questions