ios 自动布局和“按下时隐藏底栏”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28724255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 04:59:27  来源:igfitidea点击:

Auto Layout and "Hide bottom bar when pushed"

iosobjective-cstoryboarduitabbarcontrollerautolayout

提问by Eyal

My app's (simplified) structure is this:

我的应用程序的(简化)结构是这样的:

UITabBarControllerwith one UINavigationControllerholding a UITableViewControlleras root view controller.

UITabBarController一个UINavigationController持有一个UITableViewController作为根视图控制器。

when tapping one of the table view controller cells, I push a regular UIViewController(lets call it VC) end hiding the bottom tab bar. (using the "Hide bottom bar when pushed" flag)

当点击表视图控制器单元格之一时,我推一个常规UIViewController(我们称之为 VC)端隐藏底部标签栏。(使用“按下时隐藏底部栏”标志)

In storyboard I added a regular UIViewsubclass to VC that look like a bottom bar, and I use Auto Layout to pin it to the bottom of the VC view.

在故事板中,我UIView向 VC添加了一个看起来像底部栏的常规子类,并且我使用自动布局将其固定到 VC 视图的底部。

The problem
when I push VC it takes a second for this view to pin to the bottom, it looks like auto layout pin it to the bottom as if the tab bar is not hidden and after a sec it recognise that the tab bar is hidden and moves it to the real bottom of the view.


当我按下 VC 时的问题,这个视图需要一秒钟才能固定到底部,它看起来像自动布局将它固定到底部,就好像标签栏没有隐藏一样,一秒钟后它识别出标签栏是隐藏的,并且将其移动到视图的真正底部。

I know its not the best explanation, so I added a very simple projectto demonstrate the issue.

我知道这不是最好的解释,所以我添加了一个非常简单的项目来演示这个问题。

回答by Earl Grey

The problem is with this specific constraint which is between the view and the top of the bottom layout guide.

问题在于此特定约束位于视图和底部布局指南的顶部之间。

enter image description here

在此处输入图片说明

Select the constraint and edit its "Second Item" property

选择约束并编辑其“第二项”属性

enter image description here

在此处输入图片说明

Here you need to choose bottom

这里需要选择底部

enter image description here

在此处输入图片说明

Once you have that, the pink view is not influenced by layout guide anymore. The layout guide seem to acknowledge that the tab bar is hidden only after the root view of the pushed view controller is in the bounds of main screen and this happens only when the animation is finished.

一旦你有了它,粉红色的视图就不再受布局指南的影响了。布局指南似乎承认只有在推送的视图控制器的根视图位于主屏幕的边界后才会隐藏标签栏,并且只有在动画完成时才会发生这种情况。

And that is the reason the view hierarchy needs to be laid out again which causes the unwanted animation.

这就是视图层次结构需要再次布局的原因,这会导致不需要的动画。

回答by Vincenzo

The accepted answer did not work for me (the option was not available). However I have found another solution. (based on Hide Bottom Bar When Pushed through Autolayout)

接受的答案对我不起作用(该选项不可用)。但是我找到了另一个解决方案。(基于通过自动布局推送时隐藏底部栏

Select booth the view and the object to align (in my case btnShare) and add a new alignment constraint (Bottom Edges).

选择要对齐的视图和对象(在我的例子中是 btnShare)并添加一个新的对齐约束(底部边缘)。

enter image description hereenter image description here

在此处输入图片说明在此处输入图片说明

回答by Onik IV

Hi In storyboard select Tab bar (Is Tab Bar Controller Scene > Tab Bar Controller > Tab Bar ), in the attribute inspector, uncheck Translucent box. This action fix your problem. (But there are many things, "Hide bottom bar when pushed" is to toolbar).

嗨,在故事板中选择 Tab bar(Is Tab Bar Controller Scene > Tab Bar Controller > Tab Bar),在属性检查器中,取消选中 Translucent 框。此操作可解决您的问题。(但有很多东西,“按下时隐藏底栏”是工具栏)。

回答by Zaporozhchenko Oleksandr

IF you can't select bottomlayoutguide bottom in your Xcode 7+

如果您无法在 Xcode 7+ 中选择底部布局指南底部

like therejust do the following: open your storyboard in source code editor

喜欢那里只需执行以下操作:在源代码编辑器中打开您的故事板

open in editorsearch your controller's identifier

在编辑器中打开搜索控制器的标识符

search in source filefind <layoutGuides>, type <bottom>, copy it id

在源文件中搜索查找<layoutGuides>、输入<bottom>、复制 ID

find layoutGuidessearch by id change attribute from top to bottom

找到布局指南按id从上到下更改属性搜索

find and replaceenjoy.

查找和替换请享用。

回答by Arash

Select your "Navigation Controller" and in "Attribute Inspector" remove the checkmark from "Under Bottom Bars".

选择您的“导航控制器”并在“属性检查器”中从“底部栏”中删除复选标记。

回答by Mário Carvalho

In the storyboard, Go to the View Controller that you want to hide the tab bar, click on Attribute Inspectorand select Hide bottom bar when pushed. Check image bellow.

在故事板中,转到要隐藏选项卡栏的视图控制器,单击属性检查器并选择Hide bottom bar when push。检查下面的图像。

storyboard example

故事板示例

回答by rdelmar

If you want the tab bar to be hidden, you can add this code to your controller,

如果要隐藏标签栏,可以将此代码添加到控制器中,

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}

You will also have to put that code (but passing NO) into the controller where you want the tab bar to be visible. You should also deselect the "Hide bottom bar when pushed" box in IB.

您还必须将该代码(但传递 NO)放入您希望选项卡栏可见的控制器中。您还应该取消选中 IB 中的“按下时隐藏底栏”框。

After Edit:

编辑后:

You'll get a better animation if, in the first controller, you animate the alpha value of the non-hidden tab bar from 0 to 1 over a short time. This looks good if you go back with the back button. If you want to use the swipe back, you would have to do something more complicated involving the interactivePopGestureRecognizer.

如果在第一个控制器中将非隐藏标签栏的 alpha 值在短时间内从 0 设置为 1,您将获得更好的动画效果。如果您使用后退按钮返回,这看起来不错。如果您想使用回扫,您将不得不做一些涉及interactivePopGestureRecognizer 的更复杂的事情。

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
    self.tabBarController.tabBar.alpha = 0.0;
    [UIView animateWithDuration:.4 animations:^{
        self.tabBarController.tabBar.alpha = 1.0;
    }];
}

回答by Breno Morais

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
    self.tabBarController.tabBar.alpha = 0.0;
    [UIView animateWithDuration:.3 animations:^{
        self.tabBarController.tabBar.alpha = 5.0;
    }];
}

回答by taitanxiami

set UINavigationBar Translucent with NO.

将 UINavigationBar Translucent 设置为 NO。

like this: self.navigationController.navigationBar.translucent = NO;

像这样: self.navigationController.navigationBar.translucent = NO;

回答by Karthik Rao

Try pinning bottom of your view to bottom of superview and not bottom-layout

尝试将视图的底部固定到超级视图的底部而不是底部布局