6

Any idea how to get an inputAccessoryView to anchor to the tab bar rather than the bottom of the screen?

I have created a UIViewController and overridden the following methods:

-(BOOL)canBecomeFirstResponder {
    return YES;
}

-(UIView *)inputAccessoryView {

    CGRect frame = CGRectMake(0, 0, self.view.frame.size.width, 44);

    self.keyboardInputAccessoryView =[[BRKeyboardInputBarView alloc] initWithFrame:frame leftButtonTitle:@"Left" andRightButtonTitle:@"Send"];
    [self.keyboardInputAccessoryView setDelegate:self];
    [self.keyboardInputAccessoryView setTranslatesAutoresizingMaskIntoConstraints:NO];
    [self.keyboardInputAccessoryView removeFromSuperview];

    return self.keyboardInputAccessoryView;
}

View controller with inputAccessoryView covering the tab bar

By the looks of it the view controller adds the view to the window rather than the current view controllers view, which would explain its positioning. However if I remove the line:

[self.keyboardInputAccessoryView removeFromSuperview];

I get a crash when I tap in the textview of my accessory view:

The view hierarchy is not prepared for the constraint:<NSLayoutConstraint:0x7fa0c2ca5f80 BRKeyboardInputBarView:0x7fa0c2d6fad0.bottom == UIInputSetContainerView:0x7fa0c295a2c0.bottom>

So I guess what I am asking is what is the correct way to add a keyboard accessory view so that it plays nicely with auto layout and avoids the crash, but also anchors itself to the view and not the window?

darkbreed
  • 151
  • 9
  • I find it odd that the _accessoryView_ is there at all when the keyboard isn't up as this isn't its default behaviour; the input accessory should disappear with the keyboard. I think you would be better off tracking the height of the keyboard and moving up with that whenever firstResponder is called? Something along the lines of UIKeyboardDidShowNotification and the answer from this question: http://stackoverflow.com/questions/25874975/cant-get-correct-value-of-keyboard-height-in-ios8 – SunburstEnzo Jan 19 '16 at 00:13

1 Answers1

0

What you are seeing is the right behaviour.

The results you are seeing is because of the fact that UIViewController is a UIResponder subclass. By overriding the inputAccessoryView and returning an instance of a view, UIViewController will take care of placing that view at the bottom of the screen and animating it appropriately when keyboard appears or disappears.

If you want to add this bar on top of your keyboard, then you need to set the property inputAccessoryView of a textField/textView to your custom view.

Abhinav
  • 37,684
  • 43
  • 191
  • 309