0

有谁知道如何在 iOS7 中更改工具栏的位置?我的工具栏上有一个文本字段,在编辑期间出现键盘时需要查看该文本字段。

在 iOS6 中,我只是为工具栏框架设置了动画并更改了工具栏的位置,但它似乎在 iOS7 中不起作用。有没有办法在 iOS7 中做到这一点?

- (void)textFieldDidBeginEditing:(UITextField *)textField
{   
     [UIView animateWithDuration:0.25
                           delay:0.0
                         options:UIViewAnimationOptionCurveEaseIn
                      animations:^{
                           [self.navigationController.toolbar 
                                  setFrame:
                             CGRectMake(0, self.view.bounds.size.height - 172, 320, 44)];
                      }
                      completion:nil];
}
4

1 回答 1

0

I figured it out. I added another button to the animation and then tried changing the toolbar frame in the completion block instead and it worked! The other button is just there to resignFirstResponder by pressing outside the keyboard. I have no idea why it worked this way but I am not complaining:

- (void)textFieldDidBeginEditing:(UITextField *)textField
{   
    self.done = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 0)];
    self.done.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    [self.done  addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationController.view addSubview:self.done];

    [UIView animateWithDuration:0.25
                          delay:0.0
                        options:UIViewAnimationOptionCurveEaseIn
                     animations:^{
                         [self.done setFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - 172 + 20)];
                     }
                     completion:^(BOOL finished) {
                         [self.navigationController.toolbar setFrame:CGRectMake(0, self.view.bounds.size.height - 172 + 20, 320, 44)];
                     }
     ];
}

Another thing I noticed is that I couldn't change the translucent property of my tool bar. When I tried to implement:

self.navigationController.toolbar.translucent = YES;

or when I tried to implement:

self.navigationController.toolbar.barStyle = UIBarStyleBlackTranslucent;

It caused the animation completion block code to not work. I'm not sure if this is a bug or what?

于 2013-09-27T20:17:14.553 回答