2017-02-09 30 views
5

因此,通过谷歌和SO长期搜索后,我还没有找到类似的问题。有些人靠近但不喜欢我的。工具栏是多任务时的动画

手头问题: 在我的应用程序(基于WKWebView)中,如果显示键盘,并且我双击home并切换到显示键盘的另一个应用程序,我的应用程序键盘被隐藏。没什么大不了。所以当我再次多任务时,在我的应用程序所需的快照中,键盘消失了,但我上面的自定义工具栏似乎仍保留在下面没有键盘的位置。我点击我的应用程序,然后动画起来,然后回到屏幕底部。

我试过的: 我知道[self.view endEditing:YES],resigningFirstResponder和所有关闭键盘的方法。尝试通过viewWillDisappear,applicationWillResignActive和applicationDidEnterBackground进行放置和混合。这些都不会处理我的工具栏问题。

这就是我如何在屏幕上和屏幕之外动画我的键盘,同时保持工具栏可见。

- (void)keyboardWillShow:(NSNotification*)notification 
{ 
    NSDictionary* info = [notification userInfo]; 
    NSNumber *durationValue = info[UIKeyboardAnimationDurationUserInfoKey]; 
    NSNumber *curveValue = info[UIKeyboardAnimationCurveUserInfoKey]; 
    NSValue *endFrame = info[UIKeyboardFrameEndUserInfoKey]; 

    [UIView animateWithDuration:durationValue.doubleValue 
          delay:0 
         options:(curveValue.intValue << 16) 
        animations:^{ 
         self.toolBar.frame = CGRectMake(0, 
                 [endFrame CGRectValue].origin.y - self.toolBar.bounds.size.height+44, 
                 self.toolBar.bounds.size.width, 
                 self.toolBar.bounds.size.height); 
        } 
        completion:nil]; 
} 

- (void)keyboardWillHide:(NSNotification*)notification 
{ 
    NSDictionary* info = [notification userInfo]; 
    NSNumber *durationValue = info[UIKeyboardAnimationDurationUserInfoKey]; 
    NSNumber *curveValue = info[UIKeyboardAnimationCurveUserInfoKey]; 
    NSValue *endFrame = info[UIKeyboardFrameEndUserInfoKey]; 

    [UIView animateWithDuration:durationValue.doubleValue 
          delay:0 
         options:(curveValue.intValue << 16) 
        animations:^{ 
         self.toolBar.frame = CGRectMake(0, 
                 [endFrame CGRectValue].origin.y - self.toolBar.bounds.size.height, 
                 self.toolBar.bounds.size.width, 
                 self.toolBar.bounds.size.height); 
        } 
        completion:nil]; 
} 

有没有人有线索或想法?

我的应用程序再次是基于WKWebView,所以直接调用或指向textviews,试图集中在web视图是毫无意义的。

如果有人需要更多的代码,请让我知道。 任何帮助,将不胜感激。谢谢。

修订18年1月20日

所以这里是有关在何处以及如何我的工具栏被初始化和加载所需的代码。 h。

h。

UIToolbar *toolBar; 
@property (nonatomic, strong) UIToolbar *toolBar; 

m。

@synthesize toolBar; 

- (void)viewDidLoad { 
    //Toolbar setup 
    CGRect toolFrame, remain; 
    CGRectDivide(self.view.bounds, &toolFrame, &remain, 48, CGRectMaxYEdge); 
    self.toolBar = [[UIToolbar alloc] initWithFrame:toolFrame]; 
    [self.toolBar setBarStyle:UIBarStyleBlackTranslucent]; 
    [self.toolBar setClipsToBounds:YES]; 
    [self.toolBar setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin]; 
    [self.view addSubview:self.toolBar]; 
} 

这就是它,然后其他一些代码调用刷新它,当我切换按钮,其他什么都被绑定到这个工具栏。

这不会被调用,除非我点击一个开关或需要在用户更改背景时刷新它,在另一个视图中处理NSUserDefaults。

-(void)viewWillLayoutSubviews{ 
    [self.currentScrollView layoutIfNeeded]; 
    [self.toolBar layoutIfNeeded]; 
    [self.view layoutSubviews]; 
} 
+0

这仍然是我的应用程序一直存在的问题。 – ChrisOSX

+0

你在哪里创建了工具栏,以及如何? – ChikabuZ

+0

一旦应用程序加载,它就会在我的主视图(viewDidLoad)中创建。我使用属性在h文件中创建了它,然后在m文件中进行合成并将其添加到子视图中。 – ChrisOSX

回答

0

下面是对我有用的东西。

在我AppDelegate.m我简单地说endEditing:

- (void)applicationWillResignActive:(UIApplication *)application { 
    [self.window endEditing:YES]; 
} 

现在在我的keyboardWillShow通知:

- (void)keyboardWillShow:(NSNotification*)notification 
{ 
    /////New added method////// 
    if([UIApplication sharedApplication].applicationState != UIApplicationStateActive){ 
     return; 
    } 


    NSDictionary* info = [notification userInfo]; 
    NSNumber *durationValue = info[UIKeyboardAnimationDurationUserInfoKey]; 
    NSNumber *curveValue = info[UIKeyboardAnimationCurveUserInfoKey]; 
    NSValue *endFrame = info[UIKeyboardFrameEndUserInfoKey]; 

    [UIView animateWithDuration:durationValue.doubleValue 
          delay:0 
         options:(curveValue.integerValue << 16) 
        animations:^{ 
         self.toolBar.frame = CGRectMake(0, 
                 [endFrame CGRectValue].origin.y - self.toolBar.bounds.size.height+44, 
                 self.toolBar.bounds.size.width, 
                 self.toolBar.bounds.size.height); 
        } 
        completion:nil];  
} 

就是这样,这么简单的东西毕竟。只需隐藏键盘,就像我之前做的一样,并检查该应用是否处于活动状态。

现在,当我双击家中,键盘隐藏。如果我转到其他应用程序,如消息,绘制键盘,再次双击,回到我的应用程序,键盘从未移动。解决我的问题。