开发者

iPhone: How to fix inputAccessoryView to View?

I have a toolbar which I need to use when editing text, and when not.

In previous apps, I've moved the toolbar manually (listening for notifications, etc.)

But I want to use inputAccessoryView... so in my viewDidLoad, I do

for (/*myTextFields*/) {
   textField.inputAccessoryView = keyboardToolbar;
}
[self.view addSubView:keyboar开发者_StackOverflowdToolbar];

Which works fine, the toolbar appears, I click on a text field, toolbar slides up - all good. But when I then hide the keyboard, inputAccessoryView drags my toolbar off the screen. Is there any way to tell the inputAcessoryView where it's fixed location is? - Or do I have to go back to my old way of listening for notifications etc...?


I solved this by listening for Notifications and moving the toolbar up... oh well.

Something like this does the job:

- (void)viewWillAppear:(BOOL)animated 
{
    [super viewWillAppear:animated];
    /* Listen for keyboard */
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification *)notification 
{
    [keyboardToolbar setItems:itemSetFull animated:YES];
    /* Move the toolbar to above the keyboard */
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    CGRect frame = self.keyboardToolbar.frame;
    frame.origin.y = self.view.frame.size.height - 210.0;
    self.keyboardToolbar.frame = frame;
    [UIView commitAnimations];
}

- (void)keyboardWillHide:(NSNotification *)notification 
{
    [keyboardToolbar setItems:itemSetSmall animated:YES];
    /* Move the toolbar back to bottom of the screen */
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    CGRect frame = self.keyboardToolbar.frame;
    frame.origin.y = self.view.frame.size.height - frame.size.height;
    self.keyboardToolbar.frame = frame;
    [UIView commitAnimations];
}

I guess input accessory view is literally just meant for something stuck on top of the keyboard :)


I've figured it out recently, and it seems few people have. So, I would like to direct you to this answer: https://stackoverflow.com/a/24855095/299711, which I will just copy below:

Assign your UIToolbar to a property in your view controller:

@property (strong, nonatomic) UIToolbar *inputAccessoryToolbar;

In your top view controller, add these methods:

- (BOOL)canBecomeFirstResponder{

    return YES;

}

- (UIView *)inputAccessoryView{

    return self.inputAccessoryToolbar;

}

And then (optionally, as it usually shouldn't be necessary), whenever the keyboard gets hidden, just call:

[self becomeFirstResponder];

That way, your inputAccessoryToolbar will be both your view controller's and your text view's input accessory view.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜