gpt4 book ai didi

iphone - 键盘隐藏TabBar

转载 作者:行者123 更新时间:2023-12-03 18:35:07 32 4
gpt4 key购买 nike

我正在使用 TabBar 应用程序。在一个 View 中,有一个 UISearchBar,按下 时,会出现键盘。

问题是键盘隐藏了标签栏。

你知道如何解决吗?

最佳答案

自从提出这个问题以来已经有一段时间了,但为了文档的缘故,这里是这样的:首先订阅NSNotificationCenter来接收键盘通知:

-(void) viewWillAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillToggle:)
name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillToggle:)
name:UIKeyboardWillHideNotification object:nil];
}

不要忘记取消订阅

- (void)viewWillDisappear:(BOOL)animated 
{
[self.view endEditing:YES];
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification object:nil];
}

然后实现通知中心会调用的函数:

- (void) keyboardWillToggle:(NSNotification *)aNotification
{
CGRect frame = [[[self tabBarController] tabBar] frame];
CGRect keyboard = [[aNotification.userInfo valueForKey:@"UIKeyboardFrameEndUserInfoKey"] CGRectValue];
frame.origin.y = keyboard.origin.y - frame.size.height;
[UIView animateWithDuration:[[aNotification.userInfo valueForKey:@"UIKeyboardAnimationDurationUserInfoKey"] floatValue] animations:^
{
[[[self tabBarController] tabBar] setFrame:frame];
}];

这将使 TabBar 按照键盘的速度设置动画并保持在顶部。

关于iphone - 键盘隐藏TabBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5272267/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com